Completed
Push — master ( 80e539...e8c98b )
by Kristof
11s
created

Projector::applyOrganizerDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\Relations;
4
5
use Broadway\EventHandling\EventListenerInterface;
6
use CultuurNet\UDB3\Cdb\CdbId\EventCdbIdExtractorInterface;
7
use CultuurNet\UDB3\Cdb\EventItemFactory;
8
use CultuurNet\UDB3\Event\Events\EventCopied;
9
use CultuurNet\UDB3\Event\Events\EventCreated;
10
use CultuurNet\UDB3\Event\Events\EventDeleted;
11
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
12
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
13
use CultuurNet\UDB3\Event\Events\LocationUpdated;
14
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
15
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
16
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
17
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
18
19
class Projector implements EventListenerInterface
20
{
21
    use DelegateEventHandlingToSpecificMethodTrait;
22
23
    /**
24
     * @var RepositoryInterface
25
     */
26
    protected $repository;
27
28
    /**
29
     * @var EventCdbIdExtractorInterface
30
     */
31
    protected $cdbIdExtractor;
32
33
    /**
34
     * @param RepositoryInterface $repository
35
     * @param EventCdbIdExtractorInterface $cdbIdExtractor
36
     */
37
    public function __construct(
38
        RepositoryInterface $repository,
39
        EventCdbIdExtractorInterface $cdbIdExtractor
40
    ) {
41
        $this->repository = $repository;
42
        $this->cdbIdExtractor = $cdbIdExtractor;
43
    }
44
45
    /**
46
     * @param EventImportedFromUDB2 $event
47
     */
48
    protected function applyEventImportedFromUDB2(EventImportedFromUDB2 $event)
49
    {
50
        $this->applyEventDataFromUDB2($event);
51
    }
52
53
    /**
54
     * @param EventUpdatedFromUDB2 $event
55
     */
56
    protected function applyEventUpdatedFromUDB2(EventUpdatedFromUDB2 $event)
57
    {
58
        $this->applyEventDataFromUDB2($event);
59
    }
60
61
    /**
62
     * @param EventImportedFromUDB2|EventUpdatedFromUDB2 $event
63
     */
64
    protected function applyEventDataFromUDB2($event)
65
    {
66
        $eventId = $event->getEventId();
67
68
        $udb2Event = EventItemFactory::createEventFromCdbXml(
69
            $event->getCdbXmlNamespaceUri(),
70
            $event->getCdbXml()
71
        );
72
73
        $placeId = $this->cdbIdExtractor->getRelatedPlaceCdbId($udb2Event);
74
        $organizerId = $this->cdbIdExtractor->getRelatedOrganizerCdbId($udb2Event);
75
76
        $this->storeRelations($eventId, $placeId, $organizerId);
77
    }
78
79
    /**
80
     * @param EventCreated $event
81
     */
82
    protected function applyEventCreated(EventCreated $event)
83
    {
84
        $eventId = $event->getEventId();
85
86
        // Store relation if the event is connected with a place.
87
        $cdbid = $event->getLocation()->getCdbid();
88
        if (!empty($cdbid)) {
89
            $organizer = null;
90
            $this->storeRelations($eventId, $cdbid, $organizer);
91
        }
92
93
    }
94
95
    /**
96
     * @param EventCopied $eventCopied
97
     */
98
    protected function applyEventCopied(EventCopied $eventCopied)
99
    {
100
        $originalEventId = $eventCopied->getOriginalEventId();
101
        $placeId = $this->repository->getPlaceOfEvent($originalEventId);
102
        $organizerId = $this->repository->getOrganizerOfEvent($originalEventId);
103
104
        $this->repository->storeRelations(
105
            $eventCopied->getItemId(),
106
            $placeId,
107
            $organizerId
108
        );
109
    }
110
111
    /**
112
     * @param MajorInfoUpdated $majorInfoUpdated
113
     */
114
    protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated)
115
    {
116
        $eventId = $majorInfoUpdated->getItemId();
117
        $cdbId = $majorInfoUpdated->getLocation()->getCdbid();
118
        $this->repository->storePlace($eventId, $cdbId);
119
    }
120
121
    /**
122
     * @param LocationUpdated $locationUpdated
123
     */
124
    protected function applyLocationUpdated(LocationUpdated $locationUpdated)
125
    {
126
        $eventId = $locationUpdated->getItemId();
127
        $locationId = $locationUpdated->getLocationId()->toNative();
128
        $this->repository->storePlace($eventId, $locationId);
129
    }
130
131
    /**
132
     * Delete the relations.
133
     * @param EventDeleted $event
134
     */
135
    protected function applyEventDeleted(EventDeleted $event)
136
    {
137
        $eventId = $event->getItemId();
138
        $this->repository->removeRelations($eventId);
139
140
    }
141
142
    /**
143
     * Store the relation when the organizer was changed
144
     * @param OrganizerUpdated $organizerUpdated
145
     */
146
    protected function applyOrganizerUpdated(OrganizerUpdated $organizerUpdated)
147
    {
148
        $this->repository->storeOrganizer($organizerUpdated->getItemId(), $organizerUpdated->getOrganizerId());
149
    }
150
151
    /**
152
     * Remove the relation.
153
     * @param OrganizerDeleted $organizerDeleted
154
     */
155
    protected function applyOrganizerDeleted(OrganizerDeleted $organizerDeleted)
156
    {
157
        $this->repository->storeOrganizer($organizerDeleted->getItemId(), null);
158
    }
159
160
    /**
161
     * @param string $eventId
162
     * @param string $placeId
163
     * @param string $organizerId
164
     */
165
    protected function storeRelations($eventId, $placeId, $organizerId)
166
    {
167
        $this->repository->storeRelations($eventId, $placeId, $organizerId);
168
    }
169
}
170