Skip to content

Commit

Permalink
modernize codebase with readonly classes (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jun 5, 2024
1 parent e77b6f6 commit 21627d0
Show file tree
Hide file tree
Showing 29 changed files with 104 additions and 407 deletions.
28 changes: 2 additions & 26 deletions src/Catalogue/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,14 @@

namespace Akondas\Library\Catalogue;

class Book
final readonly class Book
{
private ISBN $isbn;

private Title $title;

private Author $author;

private function __construct(ISBN $isbn, Title $title, Author $author)
private function __construct(public ISBN $isbn, public Title $title, public Author $author)
{
$this->isbn = $isbn;
$this->title = $title;
$this->author = $author;
}

public static function of(string $isbn, string $title, string $author): self
{
return new self(new ISBN($isbn), new Title($title), new Author($author));
}

public function bookIsbn(): ISBN
{
return $this->isbn;
}

public function title(): Title
{
return $this->title;
}

public function author(): Author
{
return $this->author;
}
}
11 changes: 4 additions & 7 deletions src/Catalogue/BookId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@

use Akondas\Library\Common\UUID;

final class BookId
final readonly class BookId
{
private UUID $bookId;

public function __construct(UUID $bookId)
public function __construct(public UUID $bookId)
{
$this->bookId = $bookId;
}

public function bookId(): UUID
public static function random(): self
{
return $this->bookId;
return new self(UUID::random());
}
}
32 changes: 3 additions & 29 deletions src/Catalogue/BookInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,14 @@

namespace Akondas\Library\Catalogue;

use Akondas\Library\Common\UUID;

class BookInstance
final readonly class BookInstance
{
private ISBN $isbn;

private BookId $bookId;

private BookType $bookType;

private function __construct(ISBN $isbn, BookId $bookId, BookType $bookType)
private function __construct(public ISBN $isbn, public BookId $bookId, public BookType $bookType)
{
$this->isbn = $isbn;
$this->bookId = $bookId;
$this->bookType = $bookType;
}

public static function of(Book $book, BookType $bookType): self
{
return new self($book->bookIsbn(), new BookId(UUID::random()), $bookType);
}

public function bookIsbn(): ISBN
{
return $this->isbn;
}

public function bookId(): BookId
{
return $this->bookId;
}

public function bookType(): BookType
{
return $this->bookType;
return new self($book->isbn, BookId::random(), $bookType);
}
}
47 changes: 11 additions & 36 deletions src/Catalogue/BookInstanceAddedToCatalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,24 @@
use Akondas\Library\Common\Event\DomainEvent;
use Akondas\Library\Common\UUID;

class BookInstanceAddedToCatalogue implements DomainEvent
final readonly class BookInstanceAddedToCatalogue implements DomainEvent
{
private UUID $eventId;

private string $isbn;

private BookType $bookType;

private UUID $bookId;

private \DateTimeImmutable $when;

private function __construct(UUID $eventId, string $isbn, BookType $bookType, UUID $bookId, \DateTimeImmutable $when)
{
$this->eventId = $eventId;
$this->isbn = $isbn;
$this->bookType = $bookType;
$this->bookId = $bookId;
$this->when = $when;
private function __construct(
public UUID $eventId,
public string $isbn,
public BookType $bookType,
public UUID $bookId,
public \DateTimeImmutable $when
) {
}

public static function now(BookInstance $bookInstance): self
{
return new self(
UUID::random(),
$bookInstance->bookIsbn()->isbn(),
$bookInstance->bookType(),
$bookInstance->bookId()->bookId(),
$bookInstance->isbn->isbn,
$bookInstance->bookType,
$bookInstance->bookId->bookId,
new \DateTimeImmutable()
);
}
Expand All @@ -49,21 +39,6 @@ public function aggregateId(): UUID
return $this->bookId;
}

public function isbn(): string
{
return $this->isbn;
}

public function bookType(): BookType
{
return $this->bookType;
}

public function bookId(): UUID
{
return $this->bookId;
}

public function when(): \DateTimeImmutable
{
return $this->when;
Expand Down
9 changes: 2 additions & 7 deletions src/Catalogue/Catalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
use Munus\Control\Option;
use Munus\Control\TryTo;

class Catalogue
final readonly class Catalogue
{
private CatalogueDatabase $database;
private DomainEventPublisher $eventPublisher;

public function __construct(CatalogueDatabase $database, DomainEventPublisher $eventPublisher)
public function __construct(private CatalogueDatabase $database, private DomainEventPublisher $eventPublisher)
{
$this->database = $database;
$this->eventPublisher = $eventPublisher;
}

/**
Expand Down
13 changes: 2 additions & 11 deletions src/Catalogue/ISBN.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@

use Isbn\Isbn as IsbnCode;

class ISBN
final readonly class ISBN
{
private string $isbn;

public function __construct(string $isbn)
public function __construct(public string $isbn)
{
if (!(new IsbnCode())->validation->isbn($isbn)) {
throw new \InvalidArgumentException('Invalid ISBN');
}

$this->isbn = $isbn;
}

public function isbn(): string
{
return $this->isbn;
}
}
13 changes: 2 additions & 11 deletions src/Catalogue/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@

namespace Akondas\Library\Catalogue;

class Title
final readonly class Title
{
private string $title;

public function __construct(string $title)
public function __construct(public string $title)
{
if ($title === '') {
throw new \InvalidArgumentException('Title cannot be empty');
}

$this->title = $title;
}

public function title(): string
{
return $this->title;
}
}
22 changes: 6 additions & 16 deletions src/Lending/Book/Domain/AvailableBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
use Akondas\Library\Common\Aggregate\Version;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;

final class AvailableBook implements Book
final readonly class AvailableBook implements Book
{
private BookId $bookId;
private BookType $bookType;
private LibraryBranchId $libraryBranch;
private Version $version;

public function __construct(BookId $bookId, BookType $bookType, LibraryBranchId $libraryBranch, Version $version)
public function __construct(
public BookId $bookId,
public BookType $bookType,
public LibraryBranchId $libraryBranch,
public Version $version)
{
$this->bookId = $bookId;
$this->bookType = $bookType;
$this->libraryBranch = $libraryBranch;
$this->version = $version;
}

public function bookId(): BookId
Expand All @@ -39,11 +34,6 @@ public function version(): Version
return $this->version;
}

public function libraryBranch(): LibraryBranchId
{
return $this->libraryBranch;
}

public function isRestricted(): bool
{
return $this->bookType === BookType::RESTRICTED;
Expand Down
48 changes: 9 additions & 39 deletions src/Lending/Book/Domain/BookOnHold.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,21 @@
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final class BookOnHold implements Book
final readonly class BookOnHold implements Book
{
private BookId $bookId;

private BookType $bookType;

private LibraryBranchId $libraryBranch;

private PatronId $byPatron;

private \DateTimeImmutable $holdTill;

private Version $version;

/**
* BookOnHold constructor.
*/
public function __construct(BookId $bookId, BookType $bookType, LibraryBranchId $libraryBranch, PatronId $byPatron, \DateTimeImmutable $holdTill, Version $version)
public function __construct(
public BookId $bookId,
public BookType $bookType,
public LibraryBranchId $libraryBranch,
public PatronId $byPatron,
public \DateTimeImmutable $holdTill,
public Version $version)
{
$this->bookId = $bookId;
$this->bookType = $bookType;
$this->libraryBranch = $libraryBranch;
$this->byPatron = $byPatron;
$this->holdTill = $holdTill;
$this->version = $version;
}

public function by(PatronId $patronId): bool
{
return $this->byPatron->patronId()->isEqual($patronId->patronId());
}

public function libraryBranch(): LibraryBranchId
{
return $this->libraryBranch;
return $this->byPatron->patronId->isEqual($patronId->patronId);
}

public function bookId(): BookId
Expand All @@ -61,14 +41,4 @@ public function version(): Version
{
return $this->version;
}

public function byPatron(): PatronId
{
return $this->byPatron;
}

public function holdTill(): \DateTimeImmutable
{
return $this->holdTill;
}
}
12 changes: 2 additions & 10 deletions src/Lending/LibraryBranch/Domain/LibraryBranchId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

use Akondas\Library\Common\UUID;

final class LibraryBranchId
final readonly class LibraryBranchId
{
private UUID $libraryBranchId;

public function __construct(UUID $libraryBranchId)
{
$this->libraryBranchId = $libraryBranchId;
}

public function libraryBranchId(): UUID
public function __construct(public UUID $libraryBranchId)
{
return $this->libraryBranchId;
}
}
19 changes: 2 additions & 17 deletions src/Lending/Patron/Domain/Hold.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,9 @@
use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;

class Hold
final readonly class Hold
{
private BookId $bookId;
private LibraryBranchId $libraryBranchId;

public function __construct(BookId $bookId, LibraryBranchId $libraryBranchId)
{
$this->bookId = $bookId;
$this->libraryBranchId = $libraryBranchId;
}

public function bookId(): BookId
{
return $this->bookId;
}

public function libraryBranchId(): LibraryBranchId
public function __construct(public BookId $bookId, public LibraryBranchId $libraryBranchId)
{
return $this->libraryBranchId;
}
}
Loading

0 comments on commit 21627d0

Please sign in to comment.