Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateBookStatusHandler
100.00% covered (success)
100.00%
15 / 15
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
 __invoke
100.00% covered (success)
100.00%
14 / 14
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 Phexium\Application\Command\CommandHandlerInterface;
15use Phexium\Plugin\Logger\Port\LoggerInterface;
16
17final readonly class UpdateBookStatusHandler implements CommandHandlerInterface
18{
19    public function __construct(
20        private BookRepository $bookRepository,
21        private LoggerInterface $logger
22    ) {}
23
24    public function __invoke(UpdateBookStatusCommand $command): void
25    {
26        $this->logger->debug('UpdateBookStatusHandler: Processing command', [
27            'bookId' => $command->bookId->getValue(),
28            'newStatus' => $command->newStatus->value,
29        ]);
30
31        $book = $this->bookRepository->findById($command->bookId);
32
33        match ($command->newStatus) {
34            BookStatus::Borrowed => $book->markAsBorrowed(),
35            BookStatus::Available => $book->markAsAvailable(),
36        };
37
38        $this->bookRepository->save($book);
39
40        $this->logger->debug('UpdateBookStatusHandler: Book status updated successfully', [
41            'bookId' => $book->getId()->getValue(),
42            'newStatus' => $command->newStatus->value,
43        ]);
44    }
45}