1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Offer; |
4
|
|
|
|
5
|
|
|
use Broadway\Repository\AggregateNotFoundException; |
6
|
|
|
use Broadway\Repository\RepositoryInterface; |
7
|
|
|
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler; |
8
|
|
|
use CultuurNet\UDB3\Label; |
9
|
|
|
use CultuurNet\UDB3\Offer\Commands\AddLabelToMultiple; |
10
|
|
|
use CultuurNet\UDB3\Offer\Commands\AddLabelToQuery; |
11
|
|
|
use CultuurNet\UDB3\Search\ResultsGeneratorInterface; |
12
|
|
|
use CultuurNet\UDB3\Variations\AggregateDeletedException; |
13
|
|
|
use Psr\Log\LoggerAwareInterface; |
14
|
|
|
use Psr\Log\LoggerAwareTrait; |
15
|
|
|
use Psr\Log\NullLogger; |
16
|
|
|
|
17
|
|
|
class BulkLabelCommandHandler extends Udb3CommandHandler implements LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ResultsGeneratorInterface |
23
|
|
|
*/ |
24
|
|
|
private $resultsGenerator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RepositoryInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $repositories; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
ResultsGeneratorInterface $resultsGenerator |
33
|
|
|
) { |
34
|
|
|
$this->resultsGenerator = $resultsGenerator; |
35
|
|
|
$this->repositories = []; |
36
|
|
|
|
37
|
|
|
$this->setLogger(new NullLogger()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OfferType $offerType |
42
|
|
|
* @param RepositoryInterface $repository |
43
|
|
|
* @return BulkLabelCommandHandler |
44
|
|
|
*/ |
45
|
|
|
public function withRepository(OfferType $offerType, RepositoryInterface $repository) |
46
|
|
|
{ |
47
|
|
|
$c = clone $this; |
48
|
|
|
$c->repositories[$offerType->toNative()] = $repository; |
49
|
|
|
return $c; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AddLabelToQuery $addLabelToQuery |
54
|
|
|
*/ |
55
|
|
|
public function handleAddLabelToQuery(AddLabelToQuery $addLabelToQuery) |
56
|
|
|
{ |
57
|
|
|
$label = $addLabelToQuery->getLabel(); |
58
|
|
|
$query = $addLabelToQuery->getQuery(); |
59
|
|
|
|
60
|
|
|
foreach ($this->resultsGenerator->search($query) as $result) { |
61
|
|
|
/* @var OfferIdentifierInterface $result */ |
62
|
|
|
$this->label( |
63
|
|
|
$result->getType(), |
64
|
|
|
$result->getId(), |
65
|
|
|
$label, |
66
|
|
|
AddLabelToQuery::class |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param AddLabelToMultiple $addLabelToMultiple |
73
|
|
|
*/ |
74
|
|
|
public function handleAddLabelToMultiple(AddLabelToMultiple $addLabelToMultiple) |
75
|
|
|
{ |
76
|
|
|
$label = $addLabelToMultiple->getLabel(); |
77
|
|
|
|
78
|
|
|
$offerIdentifiers = $addLabelToMultiple->getOfferIdentifiers() |
79
|
|
|
->toArray(); |
80
|
|
|
|
81
|
|
|
foreach ($offerIdentifiers as $offerIdentifier) { |
82
|
|
|
$this->label( |
83
|
|
|
$offerIdentifier->getType(), |
84
|
|
|
$offerIdentifier->getId(), |
85
|
|
|
$label, |
86
|
|
|
AddLabelToMultiple::class |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param OfferType $type |
93
|
|
|
* @param string $id |
94
|
|
|
* @param Label $label |
95
|
|
|
* @param string|null $originalCommandName |
96
|
|
|
* Original command name, for logging purposes if an entity is not found. |
97
|
|
|
*/ |
98
|
|
|
private function label(OfferType $type, $id, Label $label, $originalCommandName = null) |
99
|
|
|
{ |
100
|
|
|
$type = $type->toNative(); |
101
|
|
|
|
102
|
|
|
if (!isset($this->repositories[$type])) { |
103
|
|
|
throw new \LogicException("Found no repository for type {$type}."); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$repository = $this->repositories[$type]; |
107
|
|
|
|
108
|
|
|
$logContext = [ |
109
|
|
|
'id' => $id, |
110
|
|
|
'type' => $type, |
111
|
|
|
'command' => $originalCommandName, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
/* @var Offer $offer */ |
115
|
|
|
try { |
116
|
|
|
$offer = $repository->load($id); |
117
|
|
|
$offer->addLabel($label); |
118
|
|
|
$repository->save($offer); |
119
|
|
|
} catch (AggregateNotFoundException $e) { |
120
|
|
|
$this->logger->error('bulk_label_command_entity_not_found', $logContext); |
121
|
|
|
} catch (AggregateDeletedException $e) { |
122
|
|
|
$this->logger->error('bulk_label_command_entity_deleted', $logContext); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|