Skip to content

Commit

Permalink
Daily sheet model (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jun 12, 2024
1 parent 4b57e11 commit b95c3aa
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Lending/DailySheet/Model/CheckoutsToOverdueSheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\DailySheet\Model;

use Akondas\Library\Lending\Patron\Domain\PatronEvent\OverdueCheckoutRegistered;
use Munus\Collection\GenericList;
use Munus\Collection\Stream;

final readonly class CheckoutsToOverdueSheet
{
/**
* @param GenericList<OverdueCheckout> $checkouts
*/
public function __construct(public GenericList $checkouts)
{
}

/**
* @return Stream<OverdueCheckoutRegistered>
*/
public function toStreamOfEvents(): Stream
{
return $this->checkouts->toStream()->map(fn (OverdueCheckout $oc) => $oc->toEvent());
}

public function count(): int
{
return $this->checkouts->length();
}
}
28 changes: 28 additions & 0 deletions src/Lending/DailySheet/Model/DailySheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\DailySheet\Model;

use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookCheckedOut;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldCanceled;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookPlacedOnHold;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookReturned;

interface DailySheet
{
public function queryForCheckoutsToOverdue(): CheckoutsToOverdueSheet;

public function queryForHoldsToExpireSheet(): HoldsToExpireSheet;

public function handleBookPlacedOnHold(BookPlacedOnHold $event): void;

public function handleBookHoldCanceled(BookHoldCanceled $event): void;

public function handleBookHoldExpired(BookHoldExpired $event): void;

public function handleBookCheckedOut(BookCheckedOut $event): void;

public function handleBookReturned(BookReturned $event): void;
}
22 changes: 22 additions & 0 deletions src/Lending/DailySheet/Model/ExpiredHold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\DailySheet\Model;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class ExpiredHold
{
public function __construct(public BookId $bookId, public PatronId $patronId, public LibraryBranchId $libraryBranchId)
{
}

public function toEvent(): BookHoldExpired
{
return BookHoldExpired::now($this->patronId, $this->bookId, $this->libraryBranchId);
}
}
32 changes: 32 additions & 0 deletions src/Lending/DailySheet/Model/HoldsToExpireSheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\DailySheet\Model;

use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
use Munus\Collection\GenericList;
use Munus\Collection\Stream;

final readonly class HoldsToExpireSheet
{
/**
* @param GenericList<ExpiredHold> $expireHolds
*/
public function __construct(public GenericList $expireHolds)
{
}

/**
* @return Stream<BookHoldExpired>
*/
public function toStreamOfEvents(): Stream
{
return $this->expireHolds->toStream()->map(fn (ExpiredHold $eh) => $eh->toEvent());
}

public function count(): int
{
return $this->expireHolds->length();
}
}
22 changes: 22 additions & 0 deletions src/Lending/DailySheet/Model/OverdueCheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\DailySheet\Model;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent\OverdueCheckoutRegistered;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class OverdueCheckout
{
public function __construct(public BookId $bookId, public PatronId $patronId, public LibraryBranchId $libraryBranchId)
{
}

public function toEvent(): OverdueCheckoutRegistered
{
return OverdueCheckoutRegistered::now($this->patronId, $this->bookId, $this->libraryBranchId);
}
}
54 changes: 54 additions & 0 deletions src/Lending/Patron/Domain/PatronEvent/BookHoldCanceled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class BookHoldCanceled implements PatronEvent
{
private function __construct(
public UUID $eventId,
public \DateTimeImmutable $when,
public PatronId $patronId,
public BookId $bookId,
public LibraryBranchId $libraryBranchId,
) {
}

public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
{
return new self(
UUID::random(),
new \DateTimeImmutable(),
$patronId,
$bookId,
$libraryBranchId,
);
}

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

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

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

public function when(): \DateTimeImmutable
{
return $this->when;
}
}
54 changes: 54 additions & 0 deletions src/Lending/Patron/Domain/PatronEvent/BookHoldExpired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class BookHoldExpired implements PatronEvent
{
public function __construct(
public UUID $eventId,
public \DateTimeImmutable $when,
public PatronId $patronId,
public BookId $bookId,
public LibraryBranchId $libraryBranchId,
) {
}

public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
{
return new self(
UUID::random(),
new \DateTimeImmutable(),
$patronId,
$bookId,
$libraryBranchId,
);
}

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

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

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

public function when(): \DateTimeImmutable
{
return $this->when;
}
}
57 changes: 57 additions & 0 deletions src/Lending/Patron/Domain/PatronEvent/BookReturned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Catalogue\BookType;
use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class BookReturned implements PatronEvent
{
public function __construct(
public UUID $eventId,
public \DateTimeImmutable $when,
public PatronId $patronId,
public BookId $bookId,
public BookType $bookType,
public LibraryBranchId $libraryBranchId,
) {
}

public static function now(PatronId $patronId, BookId $bookId, BookType $bookType, LibraryBranchId $libraryBranchId): self
{
return new self(
UUID::random(),
new \DateTimeImmutable(),
$patronId,
$bookId,
$bookType,
$libraryBranchId,
);
}

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

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

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

public function when(): \DateTimeImmutable
{
return $this->when;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class OverdueCheckoutRegistered implements PatronEvent
{
private function __construct(
public UUID $eventId,
public \DateTimeImmutable $when,
public PatronId $patronId,
public BookId $bookId,
public LibraryBranchId $libraryBranchId,
) {
}

public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
{
return new self(
UUID::random(),
new \DateTimeImmutable(),
$patronId,
$bookId,
$libraryBranchId,
);
}

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

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

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

public function when(): \DateTimeImmutable
{
return $this->when;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Tests\Unit\Lending\DailySheet\Model;

use Akondas\Library\Lending\DailySheet\Model\CheckoutsToOverdueSheet;
use Akondas\Library\Lending\DailySheet\Model\OverdueCheckout;
use Munus\Collection\GenericList;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(CheckoutsToOverdueSheet::class)]
final class CheckoutsToOverdueSheetTest extends TestCase
{
#[Test]
public function it_will_transform_sheet_into_stream_of_OverdueCheckoutRegistered_events(): void
{
$sheet = new CheckoutsToOverdueSheet(GenericList::of(
new OverdueCheckout($bookId = anyBookId(), $patronId = anyPatronId(), $libraryBranchId = anyBranch()),
new OverdueCheckout($anotherBookId = anyBookId(), $anotherPatronId = anyPatronId(), $anotherLibraryBranchId = anyBranch())
));

$events = $sheet->toStreamOfEvents()->toArray();

self::assertTrue($events[0]->bookId->bookId->isEqual($bookId->bookId));
self::assertTrue($events[0]->patronId->patronId->isEqual($patronId->patronId));
self::assertTrue($events[0]->libraryBranchId->libraryBranchId->isEqual($libraryBranchId->libraryBranchId));

self::assertTrue($events[1]->bookId->bookId->isEqual($anotherBookId->bookId));
self::assertTrue($events[1]->patronId->patronId->isEqual($anotherPatronId->patronId));
self::assertTrue($events[1]->libraryBranchId->libraryBranchId->isEqual($anotherLibraryBranchId->libraryBranchId));
}
}
Loading

0 comments on commit b95c3aa

Please sign in to comment.