Completed
Pull Request — master (#384)
by Kristof
03:27
created

Projector::setItemUpdateDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\ReadModel\Index;
4
5
use Broadway\EventHandling\EventListenerInterface;
6
use CultuurNet\UDB3\DomainMessageAdapter as DomainMessage;
7
use CultuurNet\UDB3\Event\Events\EventCopied;
8
use CultuurNet\UDB3\Event\Events\EventCreated;
9
use CultuurNet\UDB3\Event\Events\EventDeleted;
10
use CultuurNet\UDB3\Event\Events\EventProjectedToJSONLD;
11
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTraitWithDomainMessageAdapter;
12
use CultuurNet\UDB3\Offer\Events\AbstractEventWithIri;
13
use CultuurNet\UDB3\Offer\IriOfferIdentifierFactoryInterface;
14
use CultuurNet\UDB3\Place\Events\PlaceCreated;
15
use CultuurNet\UDB3\Place\Events\PlaceDeleted;
16
use CultuurNet\UDB3\Place\Events\PlaceProjectedToJSONLD;
17
use ValueObjects\Web\Domain;
18
use ValueObjects\Web\Url;
19
20
/**
21
 * Logs new events / updates to an index for querying.
22
 */
23
class Projector implements EventListenerInterface
24
{
25
    use DelegateEventHandlingToSpecificMethodTraitWithDomainMessageAdapter;
26
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    protected $repository;
31
32
    /**
33
     * @var Domain
34
     */
35
    protected $localDomain;
36
37
    /**
38
     * @var IriOfferIdentifierFactoryInterface
39
     */
40
    protected $identifierFactory;
41
42
    /**
43
     * @param RepositoryInterface $repository
44
     * @param Domain $localDomain
45
     * @param IriOfferIdentifierFactoryInterface $identifierFactory
46
     */
47
    public function __construct(
48
        RepositoryInterface $repository,
49
        Domain $localDomain,
50
        IriOfferIdentifierFactoryInterface $identifierFactory
51
    ) {
52
        $this->repository = $repository;
53
        $this->localDomain = $localDomain;
54
        $this->identifierFactory = $identifierFactory;
55
    }
56
57
    public function applyEventProjectedToJSONLD(
58
        EventProjectedToJSONLD $eventProjectedToJSONLD,
59
        DomainMessage $domainMessage
60
    ) {
61
        $this->setItemUpdateDate(
62
            $eventProjectedToJSONLD,
63
            $domainMessage
64
        );
65
    }
66
67
    public function applyPlaceProjectedToJSONLD(
68
        PlaceProjectedToJSONLD $placeProjectedToJSONLD,
69
        DomainMessage $domainMessage
70
    ) {
71
        $this->setItemUpdateDate(
72
            $placeProjectedToJSONLD,
73
            $domainMessage
74
        );
75
    }
76
77
    /**
78
     * @param AbstractEventWithIri $eventWithIri
79
     * @param DomainMessage $domainMessage
80
     */
81
    protected function setItemUpdateDate(
82
        AbstractEventWithIri $eventWithIri,
83
        DomainMessage $domainMessage
84
    ) {
85
        $identifier = $this->identifierFactory->fromIri(
86
            Url::fromNative($eventWithIri->getIri())
87
        );
88
89
        $this->repository->setUpdateDate(
90
            $identifier->getId(),
91
            $domainMessage->getRecordedDateTime()
92
        );
93
    }
94
95
    /**
96
     * Listener for event created commands.
97
     * @param EventCreated $eventCreated
98
     * @param DomainMessage $domainMessage
99
     */
100 View Code Duplication
    protected function applyEventCreated(
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...
101
        EventCreated $eventCreated,
102
        DomainMessage $domainMessage
103
    ) {
104
        $eventId = $eventCreated->getEventId();
105
106
        $location = $eventCreated->getLocation();
107
108
        $this->addNewItemToIndex(
109
            $domainMessage,
110
            $eventId,
111
            EntityType::EVENT(),
112
            $eventCreated->getTitle(),
113
            $location->getAddress()->getPostalCode(),
114
            $location->getAddress()->getCountry()->getCode()
115
        );
116
    }
117
118
    /**
119
     * @param EventCopied $eventCopied
120
     * @param DomainMessage $domainMessage
121
     */
122
    public function applyEventCopied(
123
        EventCopied $eventCopied,
124
        DomainMessage $domainMessage
125
    ) {
126
        $eventId = $eventCopied->getItemId();
127
128
        $this->addNewItemToIndex(
129
            $domainMessage,
130
            $eventId,
131
            EntityType::EVENT()
132
        );
133
    }
134
135
    /**
136
     * Listener for place created commands.
137
     * @param PlaceCreated $placeCreated
138
     * @param DomainMessage $domainMessage
139
     */
140 View Code Duplication
    protected function applyPlaceCreated(
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...
141
        PlaceCreated $placeCreated,
142
        DomainMessage $domainMessage
143
    ) {
144
        $placeId = $placeCreated->getPlaceId();
145
146
        $address = $placeCreated->getAddress();
147
148
        $this->addNewItemToIndex(
149
            $domainMessage,
150
            $placeId,
151
            EntityType::PLACE(),
152
            $placeCreated->getTitle(),
153
            $address->getPostalCode(),
154
            $address->getCountry()->getCode()
155
        );
156
    }
157
158
    private function addNewItemToIndex(
159
        DomainMessage $domainMessage,
160
        $id,
161
        EntityType $entityType,
162
        $name = '',
163
        $postalCode = '',
164
        $country = ''
165
    ) {
166
        $this->repository->updateIndex(
167
            $id,
168
            $entityType,
169
            $domainMessage->getUserId(),
170
            $name,
171
            $postalCode,
172
            $country,
173
            $this->localDomain,
174
            $domainMessage->getRecordedDateTime()
175
        );
176
    }
177
178
    /**
179
     * Remove the index for events
180
     * @param EventDeleted $eventDeleted
181
     */
182
    public function applyEventDeleted(
183
        EventDeleted $eventDeleted
184
    ) {
185
        $this->repository->deleteIndex(
186
            $eventDeleted->getItemId(),
187
            EntityType::EVENT()
188
        );
189
    }
190
191
    /**
192
     * Remove the index for places
193
     * @param PlaceDeleted $placeDeleted
194
     */
195
    public function applyPlaceDeleted(
196
        PlaceDeleted $placeDeleted
197
    ) {
198
        $this->repository->deleteIndex(
199
            $placeDeleted->getItemId(),
200
            EntityType::PLACE()
201
        );
202
    }
203
}
204