Skip to content

Commit b95c3aa

Browse files
authored
Daily sheet model (#17)
1 parent 4b57e11 commit b95c3aa

11 files changed

+425
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\OverdueCheckoutRegistered;
8+
use Munus\Collection\GenericList;
9+
use Munus\Collection\Stream;
10+
11+
final readonly class CheckoutsToOverdueSheet
12+
{
13+
/**
14+
* @param GenericList<OverdueCheckout> $checkouts
15+
*/
16+
public function __construct(public GenericList $checkouts)
17+
{
18+
}
19+
20+
/**
21+
* @return Stream<OverdueCheckoutRegistered>
22+
*/
23+
public function toStreamOfEvents(): Stream
24+
{
25+
return $this->checkouts->toStream()->map(fn (OverdueCheckout $oc) => $oc->toEvent());
26+
}
27+
28+
public function count(): int
29+
{
30+
return $this->checkouts->length();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookCheckedOut;
8+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldCanceled;
9+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
10+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookPlacedOnHold;
11+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookReturned;
12+
13+
interface DailySheet
14+
{
15+
public function queryForCheckoutsToOverdue(): CheckoutsToOverdueSheet;
16+
17+
public function queryForHoldsToExpireSheet(): HoldsToExpireSheet;
18+
19+
public function handleBookPlacedOnHold(BookPlacedOnHold $event): void;
20+
21+
public function handleBookHoldCanceled(BookHoldCanceled $event): void;
22+
23+
public function handleBookHoldExpired(BookHoldExpired $event): void;
24+
25+
public function handleBookCheckedOut(BookCheckedOut $event): void;
26+
27+
public function handleBookReturned(BookReturned $event): void;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
9+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
10+
use Akondas\Library\Lending\Patron\Domain\PatronId;
11+
12+
final readonly class ExpiredHold
13+
{
14+
public function __construct(public BookId $bookId, public PatronId $patronId, public LibraryBranchId $libraryBranchId)
15+
{
16+
}
17+
18+
public function toEvent(): BookHoldExpired
19+
{
20+
return BookHoldExpired::now($this->patronId, $this->bookId, $this->libraryBranchId);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\BookHoldExpired;
8+
use Munus\Collection\GenericList;
9+
use Munus\Collection\Stream;
10+
11+
final readonly class HoldsToExpireSheet
12+
{
13+
/**
14+
* @param GenericList<ExpiredHold> $expireHolds
15+
*/
16+
public function __construct(public GenericList $expireHolds)
17+
{
18+
}
19+
20+
/**
21+
* @return Stream<BookHoldExpired>
22+
*/
23+
public function toStreamOfEvents(): Stream
24+
{
25+
return $this->expireHolds->toStream()->map(fn (ExpiredHold $eh) => $eh->toEvent());
26+
}
27+
28+
public function count(): int
29+
{
30+
return $this->expireHolds->length();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
9+
use Akondas\Library\Lending\Patron\Domain\PatronEvent\OverdueCheckoutRegistered;
10+
use Akondas\Library\Lending\Patron\Domain\PatronId;
11+
12+
final readonly class OverdueCheckout
13+
{
14+
public function __construct(public BookId $bookId, public PatronId $patronId, public LibraryBranchId $libraryBranchId)
15+
{
16+
}
17+
18+
public function toEvent(): OverdueCheckoutRegistered
19+
{
20+
return OverdueCheckoutRegistered::now($this->patronId, $this->bookId, $this->libraryBranchId);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Common\UUID;
9+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
10+
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
11+
use Akondas\Library\Lending\Patron\Domain\PatronId;
12+
13+
final readonly class BookHoldCanceled implements PatronEvent
14+
{
15+
private function __construct(
16+
public UUID $eventId,
17+
public \DateTimeImmutable $when,
18+
public PatronId $patronId,
19+
public BookId $bookId,
20+
public LibraryBranchId $libraryBranchId,
21+
) {
22+
}
23+
24+
public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
25+
{
26+
return new self(
27+
UUID::random(),
28+
new \DateTimeImmutable(),
29+
$patronId,
30+
$bookId,
31+
$libraryBranchId,
32+
);
33+
}
34+
35+
public function patronId(): PatronId
36+
{
37+
return $this->patronId;
38+
}
39+
40+
public function eventId(): UUID
41+
{
42+
return $this->eventId;
43+
}
44+
45+
public function aggregateId(): UUID
46+
{
47+
return $this->patronId->patronId;
48+
}
49+
50+
public function when(): \DateTimeImmutable
51+
{
52+
return $this->when;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Common\UUID;
9+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
10+
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
11+
use Akondas\Library\Lending\Patron\Domain\PatronId;
12+
13+
final readonly class BookHoldExpired implements PatronEvent
14+
{
15+
public function __construct(
16+
public UUID $eventId,
17+
public \DateTimeImmutable $when,
18+
public PatronId $patronId,
19+
public BookId $bookId,
20+
public LibraryBranchId $libraryBranchId,
21+
) {
22+
}
23+
24+
public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
25+
{
26+
return new self(
27+
UUID::random(),
28+
new \DateTimeImmutable(),
29+
$patronId,
30+
$bookId,
31+
$libraryBranchId,
32+
);
33+
}
34+
35+
public function patronId(): PatronId
36+
{
37+
return $this->patronId;
38+
}
39+
40+
public function aggregateId(): UUID
41+
{
42+
return $this->patronId->patronId;
43+
}
44+
45+
public function eventId(): UUID
46+
{
47+
return $this->eventId;
48+
}
49+
50+
public function when(): \DateTimeImmutable
51+
{
52+
return $this->when;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Catalogue\BookType;
9+
use Akondas\Library\Common\UUID;
10+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
11+
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
12+
use Akondas\Library\Lending\Patron\Domain\PatronId;
13+
14+
final readonly class BookReturned implements PatronEvent
15+
{
16+
public function __construct(
17+
public UUID $eventId,
18+
public \DateTimeImmutable $when,
19+
public PatronId $patronId,
20+
public BookId $bookId,
21+
public BookType $bookType,
22+
public LibraryBranchId $libraryBranchId,
23+
) {
24+
}
25+
26+
public static function now(PatronId $patronId, BookId $bookId, BookType $bookType, LibraryBranchId $libraryBranchId): self
27+
{
28+
return new self(
29+
UUID::random(),
30+
new \DateTimeImmutable(),
31+
$patronId,
32+
$bookId,
33+
$bookType,
34+
$libraryBranchId,
35+
);
36+
}
37+
38+
public function patronId(): PatronId
39+
{
40+
return $this->patronId;
41+
}
42+
43+
public function aggregateId(): UUID
44+
{
45+
return $this->patronId->patronId;
46+
}
47+
48+
public function eventId(): UUID
49+
{
50+
return $this->eventId;
51+
}
52+
53+
public function when(): \DateTimeImmutable
54+
{
55+
return $this->when;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Lending\Patron\Domain\PatronEvent;
6+
7+
use Akondas\Library\Catalogue\BookId;
8+
use Akondas\Library\Common\UUID;
9+
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;
10+
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
11+
use Akondas\Library\Lending\Patron\Domain\PatronId;
12+
13+
final readonly class OverdueCheckoutRegistered implements PatronEvent
14+
{
15+
private function __construct(
16+
public UUID $eventId,
17+
public \DateTimeImmutable $when,
18+
public PatronId $patronId,
19+
public BookId $bookId,
20+
public LibraryBranchId $libraryBranchId,
21+
) {
22+
}
23+
24+
public static function now(PatronId $patronId, BookId $bookId, LibraryBranchId $libraryBranchId): self
25+
{
26+
return new self(
27+
UUID::random(),
28+
new \DateTimeImmutable(),
29+
$patronId,
30+
$bookId,
31+
$libraryBranchId,
32+
);
33+
}
34+
35+
public function patronId(): PatronId
36+
{
37+
return $this->patronId;
38+
}
39+
40+
public function eventId(): UUID
41+
{
42+
return $this->eventId;
43+
}
44+
45+
public function aggregateId(): UUID
46+
{
47+
return $this->patronId->patronId;
48+
}
49+
50+
public function when(): \DateTimeImmutable
51+
{
52+
return $this->when;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akondas\Library\Tests\Unit\Lending\DailySheet\Model;
6+
7+
use Akondas\Library\Lending\DailySheet\Model\CheckoutsToOverdueSheet;
8+
use Akondas\Library\Lending\DailySheet\Model\OverdueCheckout;
9+
use Munus\Collection\GenericList;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
14+
#[CoversClass(CheckoutsToOverdueSheet::class)]
15+
final class CheckoutsToOverdueSheetTest extends TestCase
16+
{
17+
#[Test]
18+
public function it_will_transform_sheet_into_stream_of_OverdueCheckoutRegistered_events(): void
19+
{
20+
$sheet = new CheckoutsToOverdueSheet(GenericList::of(
21+
new OverdueCheckout($bookId = anyBookId(), $patronId = anyPatronId(), $libraryBranchId = anyBranch()),
22+
new OverdueCheckout($anotherBookId = anyBookId(), $anotherPatronId = anyPatronId(), $anotherLibraryBranchId = anyBranch())
23+
));
24+
25+
$events = $sheet->toStreamOfEvents()->toArray();
26+
27+
self::assertTrue($events[0]->bookId->bookId->isEqual($bookId->bookId));
28+
self::assertTrue($events[0]->patronId->patronId->isEqual($patronId->patronId));
29+
self::assertTrue($events[0]->libraryBranchId->libraryBranchId->isEqual($libraryBranchId->libraryBranchId));
30+
31+
self::assertTrue($events[1]->bookId->bookId->isEqual($anotherBookId->bookId));
32+
self::assertTrue($events[1]->patronId->patronId->isEqual($anotherPatronId->patronId));
33+
self::assertTrue($events[1]->libraryBranchId->libraryBranchId->isEqual($anotherLibraryBranchId->libraryBranchId));
34+
}
35+
}

0 commit comments

Comments
 (0)