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
UpdateBookStatusHandler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
4
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%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
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\BookStatus;
14use Override;
15use Phexium\Application\Command\AbstractCommandHandler;
16use Phexium\Application\Command\CommandHandlerInterface;
17use Phexium\Application\Command\CommandInterface;
18use Phexium\Domain\TypeGuard;
19use Phexium\Plugin\Logger\Port\LoggerInterface;
20
21final class UpdateBookStatusHandler extends AbstractCommandHandler implements CommandHandlerInterface
22{
23    public function __construct(
24        private readonly BookRepository $bookRepository,
25        private readonly LoggerInterface $logger
26    ) {}
27
28    #[Override]
29    public function handle(CommandInterface $command): void
30    {
31        TypeGuard::that($command)->isInstanceOf(UpdateBookStatusCommand::class);
32
33        $this->logger->debug('UpdateBookStatusHandler: Processing command', [
34            'bookId' => $command->bookId->getValue(),
35            'newStatus' => $command->newStatus->value,
36        ]);
37
38        $book = $this->bookRepository->findById($command->bookId);
39
40        match ($command->newStatus) {
41            BookStatus::Borrowed => $book->markAsBorrowed(),
42            BookStatus::Available => $book->markAsAvailable(),
43        };
44
45        $this->bookRepository->save($book);
46
47        $this->logger->debug('UpdateBookStatusHandler: Book status updated successfully', [
48            'bookId' => $book->getId()->getValue(),
49            'newStatus' => $command->newStatus->value,
50        ]);
51    }
52}