|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\CommandHandler; |
|
4
|
|
|
|
|
5
|
|
|
use App\Command\RemoveItemFromCollectionCommand; |
|
6
|
|
|
use App\Repository\ItemCollectionRepository; |
|
7
|
|
|
use App\Repository\ItemRepository; |
|
8
|
|
|
use App\Repository\CollectionRepository; |
|
9
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
10
|
|
|
use App\CommandHandler\Exception\ItemNotUpdatedException; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class RemoveItemFromCollectionCommandHandler implements CommandHandlerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ItemCollectionRepository |
|
17
|
|
|
*/ |
|
18
|
|
|
private $repository; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageBusInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $eventBus; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ItemRepository |
|
29
|
|
|
*/ |
|
30
|
|
|
private $itemRepository; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var CollectionRepository |
|
33
|
|
|
*/ |
|
34
|
|
|
private $collectionRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param MessageBusInterface $eventBus |
|
38
|
|
|
* @param ItemCollectionRepository $repository |
|
39
|
|
|
* @param LoggerInterface $logger |
|
40
|
|
|
* @param ItemRepository $itemRepository |
|
41
|
|
|
* @param CollectionRepository $collectionRepository |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __construct( |
|
44
|
|
|
MessageBusInterface $eventBus, |
|
45
|
|
|
ItemCollectionRepository $repository, |
|
46
|
|
|
LoggerInterface $logger, |
|
47
|
|
|
ItemRepository $itemRepository, |
|
48
|
|
|
CollectionRepository $collectionRepository |
|
49
|
|
|
) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->eventBus = $eventBus; |
|
52
|
1 |
|
$this->repository = $repository; |
|
53
|
1 |
|
$this->logger = $logger; |
|
54
|
1 |
|
$this->itemRepository = $itemRepository; |
|
55
|
1 |
|
$this->collectionRepository = $collectionRepository; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param RemoveItemFromCollectionCommand $command |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function __invoke(RemoveItemFromCollectionCommand $command) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
1 |
|
$item = $this->itemRepository->getItem($command->getItemId()); |
|
65
|
1 |
|
$collection = $this->collectionRepository->getCollection($command->getCollectionId()); |
|
66
|
|
|
|
|
67
|
1 |
|
if (!$item->isInCollection($collection)) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$itemCollection = $this->repository->findItemCollection($item, $collection); |
|
72
|
|
|
|
|
73
|
1 |
|
if (!$itemCollection) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$this->repository->delete($itemCollection); |
|
78
|
|
|
} catch (\Exception $e) { |
|
79
|
|
|
$this->logger->error($e->getMessage()); |
|
80
|
|
|
throw new ItemNotUpdatedException('Item was not removed from collection: '.$e->getMessage()); |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|