Skip to content

Commit

Permalink
fix(kobo): Better UUID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed May 25, 2024
1 parent a4423b3 commit a1b7c98
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/DataFixtures/BookFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class BookFixture extends Fixture implements DependentFixtureInterface
{
public const BOOK_REFERENCE = 'book-odyssey';
public const ID = 1;
public const UUID = '54c8fb05-cf05-4cb6-9482-bc25fa49fa80';

public function load(ObjectManager $manager): void
{
// https://www.gutenberg.org/ebooks/1727
$book = new Book();
$book->setUuid(self::UUID);
$book->setTitle('The Odyssey');
$book->setAuthors(['Homer']);
$book->setPublishDate(new \DateTimeImmutable('1999-04-01'));
Expand All @@ -28,6 +30,7 @@ public function load(ObjectManager $manager): void
$book->setBookFilename('TheOdysses.epub');
$book->setChecksum(md5($book->getBookFilename()));
$book->setBookPath('');
$book->setPageNumber(30);

$manager->persist($book);
$manager->flush();
Expand Down
10 changes: 7 additions & 3 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,6 @@ public function setAgeCategory(?int $ageCategory): static
return $this;
}

/**
* @throws \Exception
*/
public function getUuid(): string
{
if ($this->uuid === null) {
Expand Down Expand Up @@ -552,4 +549,11 @@ public function removeKoboSyncedBook(KoboSyncedBook $koboSyncedBook): static

return $this;
}

public function setUuid(?string $uuid): self
{
$this->uuid = $uuid;

return $this;
}
}
9 changes: 5 additions & 4 deletions src/Entity/UuidGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

trait UuidGeneratorTrait
{
/**
* @throws \Exception
*/
protected function generateUuid(): string
{
$data = random_bytes(16);
try {
$data = random_bytes(16);
} catch (\Exception $e) {
throw new \RuntimeException('Unable to generate a random UUID', 0, $e);
}

// Set the version (4 for randomly generated UUID)
$data[6] = chr(ord($data[6]) & 0x0F | 0x40);
Expand Down

0 comments on commit a1b7c98

Please sign in to comment.