Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement CheckoutDuration #16

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Lending/Book/Domain/AvailableBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
final readonly class AvailableBook implements Book
{
public function __construct(
public BookId $bookId,
public BookType $bookType,
public BookInformation $bookInformation,
public LibraryBranchId $libraryBranch,
public Version $version)
{
}

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

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

public function version(): Version
Expand All @@ -36,6 +35,6 @@ public function version(): Version

public function isRestricted(): bool
{
return $this->bookType === BookType::RESTRICTED;
return $this->bookInformation->bookType === BookType::RESTRICTED;
}
}
15 changes: 15 additions & 0 deletions src/Lending/Book/Domain/BookInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Book\Domain;

use Akondas\Library\Catalogue\BookId;
use Akondas\Library\Catalogue\BookType;

final readonly class BookInformation
{
public function __construct(public BookId $bookId, public BookType $bookType)
{
}
}
7 changes: 3 additions & 4 deletions src/Lending/Book/Domain/BookOnHold.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
final readonly class BookOnHold implements Book
{
public function __construct(
public BookId $bookId,
public BookType $bookType,
public BookInformation $bookInformation,
public LibraryBranchId $libraryBranch,
public PatronId $byPatron,
public \DateTimeImmutable $holdTill,
Expand All @@ -29,12 +28,12 @@ public function by(PatronId $patronId): bool

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

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

public function version(): Version
Expand Down
27 changes: 27 additions & 0 deletions src/Lending/Patron/Domain/CheckoutDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Lending\Patron\Domain;

final readonly class CheckoutDuration
{
public const int MAX_CHECKOUT_DURATION = 60;

public function __construct(public \DateTimeImmutable $from, public NumberOfDays $noOfDays)
{
if ($this->noOfDays->isGreaterThan(self::MAX_CHECKOUT_DURATION)) {
throw new \InvalidArgumentException(sprintf('Cannot checkout for more than %s days!', self::MAX_CHECKOUT_DURATION));
}
}

public static function maxDuration(): self
{
return new self(new \DateTimeImmutable(), NumberOfDays::of(self::MAX_CHECKOUT_DURATION));
}

public function to(): \DateTimeImmutable
{
return $this->from->modify(sprintf('+%d days', $this->noOfDays->days));
}
}
60 changes: 60 additions & 0 deletions src/Lending/Patron/Domain/PatronEvent/BookCheckedOut.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\CheckoutDuration;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

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

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

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;
}
}
2 changes: 1 addition & 1 deletion src/Lending/Patron/Domain/PatronEvent/BookPlacedOnHold.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final class BookPlacedOnHold implements PatronEvent
final readonly class BookPlacedOnHold implements PatronEvent
{
private function __construct(
public UUID $eventId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

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

use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;

final readonly class MaximumNumberOnHoldsReached implements PatronEvent
{
private function __construct(
public UUID $eventId,
public \DateTimeImmutable $when,
public PatronId $patronId,
public int $numberOfHolds
) {
}

public static function now(PatronId $patronId, int $numberOfHolds): self
{
return new self(
UUID::random(),
new \DateTimeImmutable(),
$patronId,
$numberOfHolds
);
}

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;
}
}
51 changes: 51 additions & 0 deletions src/Lending/Patron/Domain/PatronEvent/PatronCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

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

use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\Patron\Domain\PatronEvent;
use Akondas\Library\Lending\Patron\Domain\PatronId;
use Akondas\Library\Lending\Patron\Domain\PatronType;

final readonly class PatronCreated implements PatronEvent
{
private function __construct(
public UUID $eventId,
public PatronId $patronId,
public \DateTimeImmutable $when,
public PatronType $patronType
) {
}

public static function now(PatronId $patronId, PatronType $patronType): self
{
return new self(
UUID::random(),
$patronId,
new \DateTimeImmutable(),
$patronType
);
}

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;
}
}
5 changes: 3 additions & 2 deletions tests/Fixture/BookFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
use Akondas\Library\Common\Aggregate\Version;
use Akondas\Library\Common\UUID;
use Akondas\Library\Lending\Book\Domain\AvailableBook;
use Akondas\Library\Lending\Book\Domain\BookInformation;
use Akondas\Library\Lending\LibraryBranch\Domain\LibraryBranchId;

function restrictedBook(): AvailableBook
{
return new AvailableBook(anyBookId(), BookType::RESTRICTED, anyBranch(), Version::zero());
return new AvailableBook(new BookInformation(anyBookId(), BookType::RESTRICTED), anyBranch(), Version::zero());
}

function circulatingBook(): AvailableBook
{
return new AvailableBook(anyBookId(), BookType::CIRCULATING, anyBranch(), Version::zero());
return new AvailableBook(new BookInformation(anyBookId(), BookType::CIRCULATING), anyBranch(), Version::zero());
}

function anyBookId(): BookId
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Lending/Patron/Domain/CheckoutDurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Akondas\Library\Tests\Unit\Lending\Patron\Domain;

use Akondas\Library\Lending\Patron\Domain\CheckoutDuration;
use Akondas\Library\Lending\Patron\Domain\NumberOfDays;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(CheckoutDuration::class)]
final class CheckoutDurationTest extends TestCase
{
#[Test]
public function it_will_allow_for_maximum_60_days(): void
{
$this->expectException(\InvalidArgumentException::class);

new CheckoutDuration(new \DateTimeImmutable(), NumberOfDays::of(61));
}

#[Test]
public function it_will_calculate_to_timestamp(): void
{
$checkoutDuration = new CheckoutDuration(new \DateTimeImmutable('2024-06-10 10:10:10'), NumberOfDays::of(60));

self::assertEquals(new \DateTimeImmutable('2024-08-09 10:10:10'), $checkoutDuration->to());
}
}
Loading