Completed
Pull Request — master (#270)
by Luc
06:03
created

Projector::applyEventCopied()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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