Skip to content

Commit

Permalink
use native enum (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jun 3, 2024
1 parent 870fc52 commit 700df5b
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 121 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"php": "^8.2",
"fale/isbn": "^3.1",
"munusphp/munus": "^0.16",
"myclabs/php-enum": "^1.7",
"symfony/uid": "^7.1"
},
"require-dev": {
Expand Down
65 changes: 1 addition & 64 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions src/Catalogue/BookType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@

namespace Akondas\Library\Catalogue;

use MyCLabs\Enum\Enum;

/**
* @extends Enum<string>
*
* @method static BookType restricted()
* @method static BookType circulating()
*
* @psalm-immutable
*/
final class BookType extends Enum
enum BookType: string
{
public const restricted = 'restricted';
public const circulating = 'circulating';
case RESTRICTED = 'restricted';
case CIRCULATING = 'circulating';
}
10 changes: 5 additions & 5 deletions src/Catalogue/Catalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public function getBook(string $isbn): Option
}

/**
* @return TryTo<Result>
* @return TryTo<covariant Result>
*/
public function addBook(string $isbn, string $title, string $author): TryTo
{
return TryTo::run(function () use ($isbn, $title, $author) {
$book = Book::of($isbn, $title, $author);
$this->database->saveBook($book);

return Result::SUCCESS();
return Result::SUCCESS;
});
}

/**
* @return TryTo<Result>
* @return TryTo<covariant Result>
*/
public function addBookInstance(string $isbn, BookType $bookType): TryTo
{
Expand All @@ -51,8 +51,8 @@ public function addBookInstance(string $isbn, BookType $bookType): TryTo
->findByIsbn(new ISBN($isbn))
->map(fn (Book $book): BookInstance => BookInstance::of($book, $bookType))
->map(fn (BookInstance $bookInstance): BookInstance => $this->saveAndPublishEvent($bookInstance))
->map(fn (BookInstance $bookInstance): Result => Result::SUCCESS())
->getOrElse(Result::REJECTION());
->map(fn (BookInstance $bookInstance): Result => Result::SUCCESS)
->getOrElse(Result::REJECTION); // @phpstan-ignore-line
});
}

Expand Down
16 changes: 3 additions & 13 deletions src/Common/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@

namespace Akondas\Library\Common\Result;

use MyCLabs\Enum\Enum;

/**
* @extends Enum<string>
*
* @psalm-immutable
*
* @method static Result SUCCESS()
* @method static Result REJECTION()
*/
class Result extends Enum
enum Result: string
{
public const SUCCESS = 'success';
public const REJECTION = 'rejection';
case SUCCESS = 'success';
case REJECTION = 'rejection';
}
2 changes: 1 addition & 1 deletion src/Lending/Book/Domain/AvailableBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function libraryBranch(): LibraryBranchId

public function isRestricted(): bool
{
return $this->bookType->equals(BookType::restricted());
return $this->bookType === BookType::RESTRICTED;
}
}
2 changes: 1 addition & 1 deletion src/Lending/Patron/Domain/PatronInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function patronType(): PatronType

public function isRegular(): bool
{
return $this->patronType->equals(PatronType::regular());
return $this->patronType === PatronType::REGULAR;
}
}
16 changes: 3 additions & 13 deletions src/Lending/Patron/Domain/PatronType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@

namespace Akondas\Library\Lending\Patron\Domain;

use MyCLabs\Enum\Enum;

/**
* @extends Enum<string>
*
* @method static PatronType regular()
* @method static PatronType researcher()
*
* @psalm-immutable
*/
final class PatronType extends Enum
enum PatronType: string
{
public const regular = 'regular';
public const researcher = 'researcher';
case REGULAR = 'regular';
case RESEARCHER = 'researcher';
}
4 changes: 2 additions & 2 deletions tests/Fixture/BookFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

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

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

function anyBookId(): BookId
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixture/PatronFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
function regularPatron(): Patron
{
return new Patron(
new PatronInformation(anyPatronId(), PatronType::regular()),
new PatronInformation(anyPatronId(), PatronType::REGULAR),
GenericList::of(
new OnlyResearcherPatronsCanPlaceOpenEndedHolds(),
new OnlyResearcherPatronsCanHoldRestrictedBooks()
Expand All @@ -33,7 +33,7 @@ function regularPatron(): Patron
function regularPatronWithHolds(int $numberOfHolds): Patron
{
return new Patron(
new PatronInformation(anyPatronId(), PatronType::regular()),
new PatronInformation(anyPatronId(), PatronType::REGULAR),
GenericList::of(
new OnlyResearcherPatronsCanPlaceOpenEndedHolds(),
new OnlyResearcherPatronsCanHoldRestrictedBooks(),
Expand All @@ -45,13 +45,13 @@ function regularPatronWithHolds(int $numberOfHolds): Patron

function researcherPatronWithPolicy(PatronId $patronId, PlacingOnHoldPolicy $placingOnHoldPolicy): Patron
{
return patronWithPolicy($patronId, PatronType::researcher(), $placingOnHoldPolicy);
return patronWithPolicy($patronId, PatronType::RESEARCHER, $placingOnHoldPolicy);
}

function researcherPatronWithHolds(int $numberOfHolds): Patron
{
return new Patron(
new PatronInformation(anyPatronId(), PatronType::researcher()),
new PatronInformation(anyPatronId(), PatronType::RESEARCHER),
GenericList::of(
new RegularPatronMaximumNumberOfHoldsPolicy()
),
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Catalogue/CatalogueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testPutBookToCatalogue(): void
$result = $this->catalogue->addBook(DDD_ISBN_STR, 'Domain Driven Design', 'Eric Evans');
// then
self::assertTrue($result->isSuccess());
self::assertEquals(Result::SUCCESS(), $result->get());
self::assertSame(Result::SUCCESS, $result->get());
}

public function testPutNewBookInstanceToCatalogue(): void
Expand All @@ -60,10 +60,10 @@ public function testPutNewBookInstanceToCatalogue(): void
->method('publish')
->with(self::isInstanceOf(BookInstanceAddedToCatalogue::class));
// when
$result = $this->catalogue->addBookInstance(DDD_ISBN_STR, BookType::restricted());
$result = $this->catalogue->addBookInstance(DDD_ISBN_STR, BookType::RESTRICTED);
// then
self::assertTrue($result->isSuccess());
self::assertEquals(Result::SUCCESS(), $result->get());
self::assertSame(Result::SUCCESS, $result->get());
}

public function testItFailsOnAddingBookWhenDatabaseFails(): void
Expand All @@ -85,7 +85,7 @@ public function testItFailsOnAddingBookInstanceWhenDatabaseFails(): void
->expects(self::never())
->method('publish');
// when
$result = $this->catalogue->addBookInstance(DDD_ISBN_STR, BookType::restricted());
$result = $this->catalogue->addBookInstance(DDD_ISBN_STR, BookType::RESTRICTED);
// then
self::assertTrue($result->isFailure());
}
Expand Down

0 comments on commit 700df5b

Please sign in to comment.