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
UpdateBookHandler
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
 __invoke
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\BookStatus;
16use AppDemo\Library\Domain\Event\BookUpdatedEvent;
17use AppDemo\Library\Domain\ISBN;
18use AppDemo\Library\Domain\Title;
19use Phexium\Application\Command\CommandHandlerInterface;
20use Phexium\Plugin\Clock\Port\ClockInterface;
21use Phexium\Plugin\EventBus\Port\EventBusInterface;
22use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
23use Phexium\Plugin\Logger\Port\LoggerInterface;
24
25final readonly class UpdateBookHandler implements CommandHandlerInterface
26{
27    public function __construct(
28        private BookRepository $bookRepository,
29        private EventBusInterface $eventBus,
30        private ClockInterface $clock,
31        private IdGeneratorInterface $idGenerator,
32        private LoggerInterface $logger,
33    ) {}
34
35    public function __invoke(UpdateBookCommand $command): void
36    {
37        $this->logger->info('UpdateBookHandler: Processing command', [
38            'bookId' => $command->id->getValue(),
39        ]);
40
41        $updatedBook = new Book(
42            $command->id,
43            Title::fromString($command->title),
44            Author::fromString($command->author),
45            ISBN::fromString($command->isbn),
46            BookStatus::from($command->status)
47        );
48
49        $this->bookRepository->save($updatedBook);
50
51        $this->eventBus->dispatch(
52            new BookUpdatedEvent(
53                $this->idGenerator->generate(),
54                $this->clock->now(),
55                $updatedBook
56            )
57        );
58
59        $this->logger->info('UpdateBookHandler: Book updated successfully', [
60            'bookId' => $updatedBook->getId()->getValue(),
61        ]);
62    }
63}