Completed
Pull Request — master (#239)
by Luc
04:56
created

Projector::applyPlaceUpdatedFromUDB2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Label\ReadModels\Relations;
4
5
use Broadway\Domain\Metadata;
6
use CultuurNet\UDB3\Cdb\ActorItemFactory;
7
use CultuurNet\UDB3\Cdb\EventItemFactory;
8
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
9
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
10
use CultuurNet\UDB3\Label\LabelEventRelationTypeResolverInterface;
11
use CultuurNet\UDB3\Label\ReadModels\AbstractProjector;
12
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\LabelRelation;
13
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\WriteRepositoryInterface;
14
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
15
use CultuurNet\UDB3\Label\ValueObjects\RelationType;
16
use CultuurNet\UDB3\LabelEventInterface;
17
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2;
18
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2;
19
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
20
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2;
21
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
22
use ValueObjects\String\String as StringLiteral;
23
24
class Projector extends AbstractProjector
25
{
26
    /**
27
     * @var WriteRepositoryInterface
28
     */
29
    private $writeRepository;
30
31
    /**
32
     * @var LabelEventRelationTypeResolverInterface
33
     */
34
    private $offerTypeResolver;
35
36
    /**
37
     * Projector constructor.
38
     * @param WriteRepositoryInterface $writeRepository
39
     * @param LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver
40
     */
41
    public function __construct(
42
        WriteRepositoryInterface $writeRepository,
43
        LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver
44
    ) {
45
        $this->writeRepository = $writeRepository;
46
        $this->offerTypeResolver = $labelEventOfferTypeResolver;
47
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 View Code Duplication
    public function applyLabelAdded(LabelEventInterface $labelAdded, Metadata $metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $LabelRelation = $this->createLabelRelation($labelAdded);
56
57
        try {
58
            if (!is_null($LabelRelation)) {
59
                $this->writeRepository->save(
60
                    $LabelRelation->getLabelName(),
61
                    $LabelRelation->getRelationType(),
62
                    $LabelRelation->getRelationId()
63
                );
64
            }
65
        } catch (UniqueConstraintViolationException $exception) {
66
            // By design to catch unique exception.
67
        }
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 View Code Duplication
    public function applyLabelDeleted(LabelEventInterface $labelDeleted, Metadata $metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $labelRelation = $this->createLabelRelation($labelDeleted);
76
77
        if (!is_null($labelRelation)) {
78
            $this->writeRepository->deleteByLabelNameAndRelationId(
79
                $labelRelation->getLabelName(),
80
                $labelRelation->getRelationId()
81
            );
82
        }
83
    }
84
85
    /**
86
     * @param EventImportedFromUDB2 $eventImportedFromUDB2
87
     */
88
    public function applyEventImportedFromUDB2(
89
        EventImportedFromUDB2 $eventImportedFromUDB2
90
    ) {
91
        $event = EventItemFactory::createEventFromCdbXml(
92
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
93
            $eventImportedFromUDB2->getCdbXml()
94
        );
95
96
        $this->updateLabelRelationFromCdbItem($event, RelationType::EVENT());
97
    }
98
99
    /**
100
     * @param PlaceImportedFromUDB2 $placeImportedFromUDB2
101
     */
102
    public function applyPlaceImportedFromUDB2(
103
        PlaceImportedFromUDB2 $placeImportedFromUDB2
104
    ) {
105
        $place = ActorItemFactory::createActorFromCdbXml(
106
            $placeImportedFromUDB2->getCdbXmlNamespaceUri(),
107
            $placeImportedFromUDB2->getCdbXml()
108
        );
109
110
        $this->updateLabelRelationFromCdbItem($place, RelationType::PLACE());
111
    }
112
113
    /**
114
     * @param OrganizerImportedFromUDB2 $organizerImportedFromUDB2
115
     */
116
    public function applyOrganizerImportedFromUDB2(
117
        OrganizerImportedFromUDB2 $organizerImportedFromUDB2
118
    ) {
119
        $organizer = ActorItemFactory::createActorFromCdbXml(
120
            $organizerImportedFromUDB2->getCdbXmlNamespaceUri(),
121
            $organizerImportedFromUDB2->getCdbXml()
122
        );
123
124
        $this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER());
125
    }
126
127
    /**
128
     * @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2
129
     */
130
    public function applyEventUpdatedFromUDB2(
131
        EventUpdatedFromUDB2 $eventUpdatedFromUDB2
132
    ) {
133
        $event = EventItemFactory::createEventFromCdbXml(
134
            $eventUpdatedFromUDB2->getCdbXmlNamespaceUri(),
135
            $eventUpdatedFromUDB2->getCdbXml()
136
        );
137
138
        $this->updateLabelRelationFromCdbItem($event, RelationType::EVENT());
139
    }
140
141
    /**
142
     * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
143
     */
144
    public function applyPlaceUpdatedFromUDB2(
145
        PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
146
    ) {
147
        $place = ActorItemFactory::createActorFromCdbXml(
148
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
149
            $placeUpdatedFromUDB2->getCdbXml()
150
        );
151
152
        $this->updateLabelRelationFromCdbItem($place, RelationType::PLACE());
153
    }
154
155
    /**
156
     * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
157
     */
158
    public function applyOrganizerUpdatedFromUDB2(
159
        OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
160
    ) {
161
        $organizer = ActorItemFactory::createActorFromCdbXml(
162
            $organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(),
163
            $organizerUpdatedFromUDB2->getCdbXml()
164
        );
165
166
        $this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER());
167
    }
168
169
    /**
170
     * @param \CultureFeed_Cdb_Item_Base $cdbItem
171
     * @param RelationType $relationType
172
     */
173
    private function updateLabelRelationFromCdbItem(
174
        \CultureFeed_Cdb_Item_Base $cdbItem,
175
        RelationType $relationType
176
    ) {
177
        $relationId = new StringLiteral($cdbItem->getCdbId());
178
179
        $this->writeRepository->deleteByRelationId($relationId);
180
181
        $keywords = $cdbItem->getKeywords();
182
        foreach ($keywords as $keyword) {
183
            $this->writeRepository->save(
184
                new LabelName($keyword),
185
                $relationType,
186
                $relationId
187
            );
188
        }
189
    }
190
191
    /**
192
     * @param LabelEventInterface $labelEvent
193
     * @return LabelRelation
194
     */
195
    private function createLabelRelation(LabelEventInterface $labelEvent)
196
    {
197
        $labelRelation = null;
0 ignored issues
show
Unused Code introduced by
$labelRelation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
199
        $labelName = new LabelName((string) $labelEvent->getLabel());
200
        $relationType = $this->offerTypeResolver->getRelationType($labelEvent);
201
        $relationId = new StringLiteral($labelEvent->getItemId());
202
203
        $labelRelation = new LabelRelation(
204
            $labelName,
205
            $relationType,
206
            $relationId
207
        );
208
209
        return $labelRelation;
210
    }
211
}
212