Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateBookHandler
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
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
8declare(strict_types=1);
9
10namespace AppDemo\Library\Application\Command;
11
12use AppDemo\Library\Domain\Author;
13use AppDemo\Library\Domain\Book;
14use AppDemo\Library\Domain\BookRepository;
15use AppDemo\Library\Domain\Event\BookCreatedEvent;
16use AppDemo\Library\Domain\ISBN;
17use AppDemo\Library\Domain\Title;
18use Override;
19use Phexium\Application\Command\AbstractCommandHandler;
20use Phexium\Application\Command\CommandHandlerInterface;
21use Phexium\Application\Command\CommandInterface;
22use Phexium\Domain\TypeGuard;
23use Phexium\Plugin\Clock\Port\ClockInterface;
24use Phexium\Plugin\EventBus\Port\EventBusInterface;
25use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
26use Phexium\Plugin\Logger\Port\LoggerInterface;
27
28final 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}