Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateBookHandler
100.00% covered (success)
100.00%
21 / 21
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
 __invoke
100.00% covered (success)
100.00%
20 / 20
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 Phexium\Application\Command\CommandHandlerInterface;
19use Phexium\Plugin\Clock\Port\ClockInterface;
20use Phexium\Plugin\EventBus\Port\EventBusInterface;
21use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
22use Phexium\Plugin\Logger\Port\LoggerInterface;
23
24final readonly class CreateBookHandler implements CommandHandlerInterface
25{
26    public function __construct(
27        private BookRepository $bookRepository,
28        private EventBusInterface $eventBus,
29        private ClockInterface $clock,
30        private IdGeneratorInterface $idGenerator,
31        private LoggerInterface $logger,
32    ) {}
33
34    public function __invoke(CreateBookCommand $command): void
35    {
36        $this->logger->info('CreateBookHandler: Processing command', [
37            'bookId' => $command->id->getValue(),
38        ]);
39
40        $book = new Book(
41            $command->id,
42            Title::fromString($command->title),
43            Author::fromString($command->author),
44            ISBN::fromString($command->isbn)
45        );
46
47        $this->bookRepository->save($book);
48
49        $this->eventBus->dispatch(
50            new BookCreatedEvent(
51                $this->idGenerator->generate(),
52                $this->clock->now(),
53                $book
54            )
55        );
56
57        $this->logger->info('CreateBookHandler: Book created successfully', [
58            'bookId' => $book->getId()->getValue(),
59        ]);
60    }
61}