Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteBookHandler
100.00% covered (success)
100.00%
16 / 16
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%
15 / 15
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\BookRepository;
13use AppDemo\Library\Domain\Event\BookDeletedEvent;
14use Phexium\Application\Command\CommandHandlerInterface;
15use Phexium\Plugin\Clock\Port\ClockInterface;
16use Phexium\Plugin\EventBus\Port\EventBusInterface;
17use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
18use Phexium\Plugin\Logger\Port\LoggerInterface;
19
20final readonly class DeleteBookHandler implements CommandHandlerInterface
21{
22    public function __construct(
23        private BookRepository $bookRepository,
24        private EventBusInterface $eventBus,
25        private ClockInterface $clock,
26        private IdGeneratorInterface $idGenerator,
27        private LoggerInterface $logger,
28    ) {}
29
30    public function __invoke(DeleteBookCommand $command): void
31    {
32        $this->logger->info('DeleteBookHandler: Processing command', [
33            'bookId' => $command->id->getValue(),
34        ]);
35
36        $existingBook = $this->bookRepository->getById($command->id);
37
38        $this->bookRepository->delete($existingBook);
39
40        $this->eventBus->dispatch(
41            new BookDeletedEvent(
42                $this->idGenerator->generate(),
43                $this->clock->now(),
44                $existingBook
45            )
46        );
47
48        $this->logger->info('DeleteBookHandler: Book deleted successfully', [
49            'bookId' => $existingBook->getId()->getValue(),
50        ]);
51    }
52}