Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateBookHandler
100.00% covered (success)
100.00%
23 / 23
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%
22 / 22
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 Override;
20use Phexium\Application\Command\AbstractCommandHandler;
21use Phexium\Application\Command\CommandHandlerInterface;
22use Phexium\Application\Command\CommandInterface;
23use Phexium\Domain\TypeGuard;
24use Phexium\Plugin\Clock\Port\ClockInterface;
25use Phexium\Plugin\EventBus\Port\EventBusInterface;
26use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
27use Phexium\Plugin\Logger\Port\LoggerInterface;
28
29final class UpdateBookHandler extends AbstractCommandHandler implements CommandHandlerInterface
30{
31    public function __construct(
32        private readonly BookRepository $bookRepository,
33        private readonly EventBusInterface $eventBus,
34        private readonly ClockInterface $clock,
35        private readonly IdGeneratorInterface $idGenerator,
36        private readonly LoggerInterface $logger,
37    ) {}
38
39    #[Override]
40    public function handle(CommandInterface $command): void
41    {
42        TypeGuard::that($command)->isInstanceOf(UpdateBookCommand::class);
43
44        $this->logger->info('UpdateBookHandler: Processing command', [
45            'bookId' => $command->id->getValue(),
46        ]);
47
48        $updatedBook = new Book(
49            $command->id,
50            Title::fromString($command->title),
51            Author::fromString($command->author),
52            ISBN::fromString($command->isbn),
53            BookStatus::from($command->status)
54        );
55
56        $this->bookRepository->save($updatedBook);
57
58        $this->eventBus->dispatch(
59            new BookUpdatedEvent(
60                $this->idGenerator->generate(),
61                $this->clock->now(),
62                $updatedBook
63            )
64        );
65
66        $this->logger->info('UpdateBookHandler: Book updated successfully', [
67            'bookId' => $updatedBook->getId()->getValue(),
68        ]);
69    }
70}