Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DeleteBookHandler | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
15 / 15 |
|
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Library\Application\Command; |
| 11 | |
| 12 | use AppDemo\Library\Domain\BookRepository; |
| 13 | use AppDemo\Library\Domain\Event\BookDeletedEvent; |
| 14 | use Phexium\Application\Command\CommandHandlerInterface; |
| 15 | use Phexium\Plugin\Clock\Port\ClockInterface; |
| 16 | use Phexium\Plugin\EventBus\Port\EventBusInterface; |
| 17 | use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface; |
| 18 | use Phexium\Plugin\Logger\Port\LoggerInterface; |
| 19 | |
| 20 | final 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 | } |