DeleteItemCommandHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 44
ccs 13
cts 14
cp 0.9286
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 14 3
1
<?php
2
3
namespace App\CommandHandler;
4
5
use App\Command\DeleteItemCommand;
6
use App\Repository\ItemRepository;
7
use Symfony\Component\Messenger\MessageBusInterface;
8
use App\CommandHandler\Exception\ItemNotDeletedException;
9
use Psr\Log\LoggerInterface;
10
use App\Event\ItemDeletedEvent;
11
12
class DeleteItemCommandHandler implements CommandHandlerInterface
13
{
14
    /**
15
     * @var ItemRepository
16
     */
17
    private $repository;    
18
    /**
19
     * @var MessageBusInterface
20
     */
21
    private $eventBus;
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
27
    /**
28
     * @param MessageBusInterface $eventBus
29
     * @param ItemRepository $repository
30
     * @param LoggerInterface $logger
31
     */
32 2
    public function __construct(MessageBusInterface $eventBus, ItemRepository $repository, LoggerInterface $logger)
33
    {
34 2
        $this->eventBus = $eventBus;
35 2
        $this->repository = $repository;
36 2
        $this->logger = $logger;
37 2
    }
38
39
    /**
40
     * @param DeleteItemCommand $command
41
     */
42 2
    public function __invoke(DeleteItemCommand $command)
43
    {
44
        try {
45 2
             $item = $this->repository->getItem($command->getId());   
46
47 1
            if ($item->getLoaned()) {
48
                throw new ItemNotDeletedException('Item has loan record');
49
            }      
50
             
51 1
            $this->repository->delete($item);
52 1
            $this->eventBus->dispatch(new ItemDeletedEvent($command->getId()->toString()));
53 1
        } catch (\Exception $e) {
54 1
            $this->logger->error($e->getMessage());
55 1
            throw new ItemNotDeletedException('Item was not deleted: '.$e->getMessage());
56
        }
57 1
    }
58
59
}
60