CreateItemCommandHandler::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 11

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 28
ccs 16
cts 20
cp 0.8
crap 3.072
rs 9.568
1
<?php
2
3
namespace App\CommandHandler;
4
5
use App\Command\CreateItemCommand;
6
use App\Event\ItemCreatedEvent;
7
use App\Entity\Item;
8
use App\Entity\ItemCollection;
9
use App\Repository\ItemRepository;
10
use App\Repository\ItemCollectionRepository;
11
use Symfony\Component\Messenger\MessageBusInterface;
12
use App\CommandHandler\Exception\ItemNotCreatedException;
13
use Psr\Log\LoggerInterface;
14
use App\Repository\CategoryRepository;
15
use App\Repository\CollectionRepository;
16
use Symfony\Component\Security\Core\Security;
17
18
class CreateItemCommandHandler implements CommandHandlerInterface
19
{
20
    /**
21
     * @var ItemRepository
22
     */
23
    private $repository;    
24
    /**
25
     * @var MessageBusInterface
26
     */
27
    private $eventBus;
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
    /**
33
     * @var CategoryRepository
34
     */
35
    private $categoryRepository;
36
    /**
37
     * @var CollectionRepository
38
     */
39
    private $collectionRepository;
40
    /**
41
     * @var ItemCollectionRepository
42
     */
43
    private $itemCollectionRepository; 
44
    /**
45
     * @var Security
46
     */
47
    private $security;
48
49
    /**
50
     * @param MessageBusInterface $eventBus
51
     * @param ItemRepository $repository
52
     * @param LoggerInterface $logger
53
     * @param CategoryRepository $categoryRepository
54
     * @param CollectionRepository $collectionRepository
55
     * @param ItemCollectionRepository $itemCollectionRepository
56
     */
57 1
    public function __construct(
58
        MessageBusInterface $eventBus, 
59
        ItemRepository $repository, 
60
        LoggerInterface $logger,
61
        CategoryRepository $categoryRepository,
62
        CollectionRepository $collectionRepository,
63
        ItemCollectionRepository $itemCollectionRepository,
64
        Security $security
65
    ) {
66 1
        $this->eventBus = $eventBus;
67 1
        $this->repository = $repository;
68 1
        $this->logger = $logger;
69 1
        $this->categoryRepository = $categoryRepository;
70 1
        $this->collectionRepository = $collectionRepository;
71 1
        $this->itemCollectionRepository = $itemCollectionRepository;
72 1
        $this->security = $security;
73 1
    }
74
75
    /**
76
     * @param CreateItemCommand $command
77
     */
78 1
    public function __invoke(CreateItemCommand $command)
79
    {
80
        try {
81 1
            $category = $this->categoryRepository->getCategory($command->getCategoryId());
82 1
            $user = $this->security->getUser();
83
            
84 1
            $item = new Item(
85 1
                $command->getId(),
86 1
                $command->getName(),
87
                $category,
88 1
                $command->getYear(),
89 1
                $command->getFormat(),
90 1
                $command->getAuthor(),
91 1
                $command->getPublisher(),
92 1
                $command->getDescription(),
93 1
                $command->getStore(),
94 1
                $command->getUrl(),
95
                $user
96
                );
97
   
98 1
            if (null !== $command->getCollections()) {
99
                $this->handleItemCollections($item, $command->getCollections());
100
            }      
101 1
            $this->repository->save($item);
102 1
            $this->eventBus->dispatch(new ItemCreatedEvent($command->getId()->toString()));
103
        } catch (\Exception $e) {
104
            $this->logger->error($e->getMessage());
105
            throw new ItemNotCreatedException('Item was not created: '.$e->getMessage());
106
        }
107 1
    }
108
109
    /**
110
     * @param Item $item
111
     * @param array $collections
112
     */
113
    public function handleItemCollections(Item $item, array $collections): void
114
    {
115
        foreach ($collections as $collectionId) {
116
            $collection = $this->collectionRepository->getCollection($collectionId);
117
            $itemCollectionEntity = new ItemCollection($item, $collection);
118
            $item->getCollections()->add($itemCollectionEntity);
119
        }
120
    }
121
}
122