Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CreateBookHandler | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Library\Application\Command; |
| 11 | |
| 12 | use AppDemo\Library\Domain\Author; |
| 13 | use AppDemo\Library\Domain\Book; |
| 14 | use AppDemo\Library\Domain\BookRepository; |
| 15 | use AppDemo\Library\Domain\Event\BookCreatedEvent; |
| 16 | use AppDemo\Library\Domain\ISBN; |
| 17 | use AppDemo\Library\Domain\Title; |
| 18 | use Override; |
| 19 | use Phexium\Application\Command\AbstractCommandHandler; |
| 20 | use Phexium\Application\Command\CommandHandlerInterface; |
| 21 | use Phexium\Application\Command\CommandInterface; |
| 22 | use Phexium\Domain\TypeGuard; |
| 23 | use Phexium\Plugin\Clock\Port\ClockInterface; |
| 24 | use Phexium\Plugin\EventBus\Port\EventBusInterface; |
| 25 | use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface; |
| 26 | use Phexium\Plugin\Logger\Port\LoggerInterface; |
| 27 | |
| 28 | final class CreateBookHandler extends AbstractCommandHandler implements CommandHandlerInterface |
| 29 | { |
| 30 | public function __construct( |
| 31 | private readonly BookRepository $bookRepository, |
| 32 | private readonly EventBusInterface $eventBus, |
| 33 | private readonly ClockInterface $clock, |
| 34 | private readonly IdGeneratorInterface $idGenerator, |
| 35 | private readonly LoggerInterface $logger, |
| 36 | ) {} |
| 37 | |
| 38 | #[Override] |
| 39 | public function handle(CommandInterface $command): void |
| 40 | { |
| 41 | TypeGuard::that($command)->isInstanceOf(CreateBookCommand::class); |
| 42 | |
| 43 | $this->logger->info('CreateBookHandler: Processing command', [ |
| 44 | 'bookId' => $command->id->getValue(), |
| 45 | ]); |
| 46 | |
| 47 | $book = new Book( |
| 48 | $command->id, |
| 49 | Title::fromString($command->title), |
| 50 | Author::fromString($command->author), |
| 51 | ISBN::fromString($command->isbn) |
| 52 | ); |
| 53 | |
| 54 | $this->bookRepository->save($book); |
| 55 | |
| 56 | $this->eventBus->dispatch( |
| 57 | new BookCreatedEvent( |
| 58 | $this->idGenerator->generate(), |
| 59 | $this->clock->now(), |
| 60 | $book |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | $this->logger->info('CreateBookHandler: Book created successfully', [ |
| 65 | 'bookId' => $book->getId()->getValue(), |
| 66 | ]); |
| 67 | } |
| 68 | } |