|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Label\ReadModels\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Broadway\Domain\Metadata; |
|
6
|
|
|
use CultuurNet\UDB3\Label\LabelEventRelationTypeResolverInterface; |
|
7
|
|
|
use CultuurNet\UDB3\Label\ReadModels\AbstractProjector; |
|
8
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\LabelRelation; |
|
9
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\WriteRepositoryInterface; |
|
10
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\LabelName; |
|
11
|
|
|
use CultuurNet\UDB3\Offer\Events\AbstractLabelEvent as OfferAbstractLabelEvent; |
|
12
|
|
|
use CultuurNet\UDB3\Organizer\Events\AbstractLabelEvent as OrganizerAbstractLabelEvent; |
|
13
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
14
|
|
|
use ValueObjects\String\String as StringLiteral; |
|
15
|
|
|
|
|
16
|
|
|
class Projector extends AbstractProjector |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var WriteRepositoryInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $writeRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var LabelEventRelationTypeResolverInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $offerTypeResolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Projector constructor. |
|
30
|
|
|
* @param WriteRepositoryInterface $writeRepository |
|
31
|
|
|
* @param LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
WriteRepositoryInterface $writeRepository, |
|
35
|
|
|
LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
|
36
|
|
|
) { |
|
37
|
|
|
$this->writeRepository = $writeRepository; |
|
38
|
|
|
$this->offerTypeResolver = $labelEventOfferTypeResolver; |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function applyLabelAdded($labelAdded, Metadata $metadata) |
|
46
|
|
|
{ |
|
47
|
|
|
$LabelRelation = $this->createLabelRelation($labelAdded); |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
if (!is_null($LabelRelation)) { |
|
51
|
|
|
$this->writeRepository->save( |
|
52
|
|
|
$LabelRelation->getLabelName(), |
|
53
|
|
|
$LabelRelation->getRelationType(), |
|
54
|
|
|
$LabelRelation->getRelationId() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
|
58
|
|
|
// By design to catch unique exception. |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function applyLabelDeleted($labelDeleted, Metadata $metadata) |
|
66
|
|
|
{ |
|
67
|
|
|
$labelRelation = $this->createLabelRelation($labelDeleted); |
|
68
|
|
|
|
|
69
|
|
|
if (!is_null($labelRelation)) { |
|
70
|
|
|
$this->writeRepository->deleteByLabelNameAndRelationId( |
|
71
|
|
|
$labelRelation->getLabelName(), |
|
72
|
|
|
new StringLiteral($labelDeleted->getItemId()) |
|
|
|
|
|
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param OfferAbstractLabelEvent|OrganizerAbstractLabelEvent $labelEvent |
|
79
|
|
|
* @return LabelRelation |
|
80
|
|
|
*/ |
|
81
|
|
|
private function createLabelRelation($labelEvent) |
|
82
|
|
|
{ |
|
83
|
|
|
$labelRelation = null; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$labelName = new LabelName((string) $labelEvent->getLabel()); |
|
86
|
|
|
$relationType = $this->offerTypeResolver->getRelationType($labelEvent); |
|
87
|
|
|
$relationId = new StringLiteral($labelEvent->getItemId()); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$labelRelation = new LabelRelation( |
|
90
|
|
|
$labelName, |
|
91
|
|
|
$relationType, |
|
92
|
|
|
$relationId |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
return $labelRelation; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: