|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Label; |
|
4
|
|
|
|
|
5
|
|
|
use Broadway\Repository\RepositoryInterface; |
|
6
|
|
|
use Broadway\UuidGenerator\UuidGeneratorInterface; |
|
7
|
|
|
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler as AbstractCommandHandler; |
|
8
|
|
|
use CultuurNet\UDB3\EventSourcing\DBAL\UniqueConstraintException; |
|
9
|
|
|
use CultuurNet\UDB3\Label; |
|
|
|
|
|
|
10
|
|
|
use CultuurNet\UDB3\Label\Commands\Create; |
|
11
|
|
|
use CultuurNet\UDB3\Label\Commands\CreateCopy; |
|
12
|
|
|
use CultuurNet\UDB3\Label\Commands\MakeInvisible; |
|
13
|
|
|
use CultuurNet\UDB3\Label\Commands\MakePrivate; |
|
14
|
|
|
use CultuurNet\UDB3\Label\Commands\MakePublic; |
|
15
|
|
|
use CultuurNet\UDB3\Label\Commands\MakeVisible; |
|
16
|
|
|
use CultuurNet\UDB3\Label\Label as LabelAggregate; |
|
17
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\LabelName; |
|
18
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\Privacy; |
|
19
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\Visibility; |
|
20
|
|
|
use CultuurNet\UDB3\Offer\Commands\AbstractAddLabel; |
|
21
|
|
|
use CultuurNet\UDB3\Offer\Commands\AbstractSyncLabels; |
|
22
|
|
|
use ValueObjects\Identity\UUID; |
|
23
|
|
|
|
|
24
|
|
|
class CommandHandler extends AbstractCommandHandler |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var RepositoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $repository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var UuidGeneratorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $uuidGenerator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* CommandHandler constructor. |
|
38
|
|
|
* @param RepositoryInterface $repository |
|
39
|
|
|
* @param UuidGeneratorInterface $uuidGenerator |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
RepositoryInterface $repository, |
|
43
|
|
|
UuidGeneratorInterface $uuidGenerator |
|
44
|
|
|
) { |
|
45
|
|
|
$this->repository = $repository; |
|
46
|
|
|
$this->uuidGenerator = $uuidGenerator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function handle($command) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($command instanceof AbstractAddLabel) { |
|
55
|
|
|
$this->handleAddLabel($command); |
|
56
|
|
|
} else if ($command instanceof AbstractSyncLabels) { |
|
57
|
|
|
$this->handleSyncLabels($command); |
|
58
|
|
|
} else { |
|
59
|
|
|
parent::handle($command); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Create $create |
|
65
|
|
|
*/ |
|
66
|
|
|
public function handleCreate(Create $create) |
|
67
|
|
|
{ |
|
68
|
|
|
$label = LabelAggregate::create( |
|
69
|
|
|
$create->getUuid(), |
|
70
|
|
|
$create->getName(), |
|
71
|
|
|
$create->getVisibility(), |
|
72
|
|
|
$create->getPrivacy() |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->save($label); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param CreateCopy $createCopy |
|
80
|
|
|
*/ |
|
81
|
|
|
public function handleCreateCopy(CreateCopy $createCopy) |
|
82
|
|
|
{ |
|
83
|
|
|
$label = LabelAggregate::createCopy( |
|
84
|
|
|
$createCopy->getUuid(), |
|
85
|
|
|
$createCopy->getName(), |
|
86
|
|
|
$createCopy->getVisibility(), |
|
87
|
|
|
$createCopy->getPrivacy(), |
|
88
|
|
|
$createCopy->getParentUuid() |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$this->save($label); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param AbstractSyncLabels $mergeLabels |
|
96
|
|
|
*/ |
|
97
|
|
|
public function handleSyncLabels(AbstractSyncLabels $mergeLabels) |
|
98
|
|
|
{ |
|
99
|
|
|
$labelsArray = $mergeLabels->getLabelCollection()->asArray(); |
|
100
|
|
|
|
|
101
|
|
|
array_walk( |
|
102
|
|
|
$labelsArray, |
|
103
|
|
|
function (Label $label) { |
|
104
|
|
|
$this->createLabel($label); |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param AbstractAddLabel $addLabel |
|
111
|
|
|
*/ |
|
112
|
|
|
public function handleAddLabel(AbstractAddLabel $addLabel) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->createLabel($addLabel->getLabel()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param MakeVisible $makeVisible |
|
119
|
|
|
*/ |
|
120
|
|
|
public function handleMakeVisible(MakeVisible $makeVisible) |
|
121
|
|
|
{ |
|
122
|
|
|
$label = $this->load($makeVisible->getUuid()); |
|
123
|
|
|
|
|
124
|
|
|
$label->makeVisible(); |
|
125
|
|
|
|
|
126
|
|
|
$this->save($label); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param MakeInvisible $makeInvisible |
|
131
|
|
|
*/ |
|
132
|
|
|
public function handleMakeInvisible(MakeInvisible $makeInvisible) |
|
133
|
|
|
{ |
|
134
|
|
|
$label = $this->load($makeInvisible->getUuid()); |
|
135
|
|
|
|
|
136
|
|
|
$label->makeInvisible(); |
|
137
|
|
|
|
|
138
|
|
|
$this->save($label); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param MakePublic $makePublic |
|
143
|
|
|
*/ |
|
144
|
|
|
public function handleMakePublic(MakePublic $makePublic) |
|
145
|
|
|
{ |
|
146
|
|
|
$label = $this->load($makePublic->getUuid()); |
|
147
|
|
|
|
|
148
|
|
|
$label->makePublic(); |
|
149
|
|
|
|
|
150
|
|
|
$this->save($label); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param MakePrivate $makePrivate |
|
155
|
|
|
*/ |
|
156
|
|
|
public function handleMakePrivate(MakePrivate $makePrivate) |
|
157
|
|
|
{ |
|
158
|
|
|
$label = $this->load($makePrivate->getUuid()); |
|
159
|
|
|
|
|
160
|
|
|
$label->makePrivate(); |
|
161
|
|
|
|
|
162
|
|
|
$this->save($label); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param UUID $uuid |
|
167
|
|
|
* @return LabelAggregate |
|
168
|
|
|
*/ |
|
169
|
|
|
private function load(UUID $uuid) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->repository->load($uuid); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param LabelAggregate $label |
|
176
|
|
|
*/ |
|
177
|
|
|
private function save(LabelAggregate $label) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->repository->save($label); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param Label $label |
|
184
|
|
|
*/ |
|
185
|
|
|
private function createLabel(Label $label) |
|
186
|
|
|
{ |
|
187
|
|
|
$label = LabelAggregate::create( |
|
188
|
|
|
new UUID($this->uuidGenerator->generate()), |
|
189
|
|
|
new LabelName((string)$label), |
|
190
|
|
|
Visibility::VISIBLE(), |
|
191
|
|
|
Privacy::PRIVACY_PUBLIC() |
|
192
|
|
|
); |
|
193
|
|
|
try { |
|
194
|
|
|
$this->save($label); |
|
195
|
|
|
} catch (UniqueConstraintException $exception) { |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: