Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteBookHandler
100.00% covered (success)
100.00%
17 / 17
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%
16 / 16
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 Override;
15use Phexium\Application\Command\AbstractCommandHandler;
16use Phexium\Application\Command\CommandHandlerInterface;
17use Phexium\Application\Command\CommandInterface;
18use Phexium\Domain\TypeGuard;
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 class DeleteBookHandler extends AbstractCommandHandler implements CommandHandlerInterface
25{
26    public function __construct(
27        private readonly BookRepository $bookRepository,
28        private readonly EventBusInterface $eventBus,
29        private readonly ClockInterface $clock,
30        private readonly IdGeneratorInterface $idGenerator,
31        private readonly LoggerInterface $logger,
32    ) {}
33
34    #[Override]
35    public function handle(CommandInterface $command): void
36    {
37        TypeGuard::that($command)->isInstanceOf(DeleteBookCommand::class);
38
39        $this->logger->info('DeleteBookHandler: Processing command', [
40            'bookId' => $command->id->getValue(),
41        ]);
42
43        $existingBook = $this->bookRepository->getById($command->id);
44
45        $this->bookRepository->delete($existingBook);
46
47        $this->eventBus->dispatch(
48            new BookDeletedEvent(
49                $this->idGenerator->generate(),
50                $this->clock->now(),
51                $existingBook
52            )
53        );
54
55        $this->logger->info('DeleteBookHandler: Book deleted successfully', [
56            'bookId' => $existingBook->getId()->getValue(),
57        ]);
58    }
59}