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

Organizer::importFromUDB2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace CultuurNet\UDB3\Organizer;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use CultuurNet\UDB3\Address\Address;
7
use CultuurNet\UDB3\Cdb\ActorItemFactory;
8
use CultuurNet\UDB3\Cdb\UpdateableWithCdbXmlInterface;
9
use CultuurNet\UDB3\ContactPoint;
10
use CultuurNet\UDB3\Label;
11
use CultuurNet\UDB3\LabelCollection;
12
use CultuurNet\UDB3\Organizer\Events\AddressUpdated;
13
use CultuurNet\UDB3\Organizer\Events\ContactPointUpdated;
14
use CultuurNet\UDB3\Organizer\Events\LabelAdded;
15
use CultuurNet\UDB3\Organizer\Events\LabelRemoved;
16
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
17
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
18
use CultuurNet\UDB3\Organizer\Events\OrganizerDeleted;
19
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2;
20
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2;
21
use CultuurNet\UDB3\Title;
22
use ValueObjects\Web\Url;
23
24
class Organizer extends EventSourcedAggregateRoot implements UpdateableWithCdbXmlInterface
25
{
26
    /**
27
     * The actor id.
28
     *
29
     * @var string
30
     */
31
    protected $actorId;
32
33
    /**
34
     * @var Address|null
35
     */
36
    private $address;
37
38
    /**
39
     * @var ContactPoint
40
     */
41
    private $contactPoint;
42
43
    /**
44
     * @var Label[]
45
     */
46
    private $labels = [];
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getAggregateRootId()
52
    {
53
        return $this->actorId;
54
    }
55
56
    public function __construct()
57
    {
58
        // Contact points can be empty, but we only want to start recording
59
        // ContactPointUpdated events as soon as the organizer is updated
60
        // with a non-empty contact point. To enforce this we initialize the
61
        // aggregate state with an empty contact point.
62
        $this->contactPoint = new ContactPoint();
63
    }
64
65
    /**
66
     * Import from UDB2.
67
     *
68
     * @param string $actorId
69
     *   The actor id.
70
     * @param string $cdbXml
71
     *   The cdb xml.
72
     * @param string $cdbXmlNamespaceUri
73
     *   The cdb xml namespace uri.
74
     *
75
     * @return Organizer
76
     *   The actor.
77
     */
78
    public static function importFromUDB2(
79
        $actorId,
80
        $cdbXml,
81
        $cdbXmlNamespaceUri
82
    ) {
83
        $organizer = new static();
84
        $organizer->apply(
85
            new OrganizerImportedFromUDB2(
86
                $actorId,
87
                $cdbXml,
88
                $cdbXmlNamespaceUri
89
            )
90
        );
91
92
        return $organizer;
93
    }
94
95
    /**
96
     * Factory method to create a new Organizer.
97
     *
98
     * @param string $id
99
     * @param Url $website
100
     * @param Title $title
101
     * @return Organizer
102
     */
103
    public static function create(
104
        $id,
105
        Url $website,
106
        Title $title
107
    ) {
108
        $organizer = new self();
109
110
        $organizer->apply(
111
            new OrganizerCreatedWithUniqueWebsite($id, $website, $title)
112
        );
113
114
        return $organizer;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
121
    {
122
        $this->apply(
123
            new OrganizerUpdatedFromUDB2(
124
                $this->actorId,
125
                $cdbXml,
126
                $cdbXmlNamespaceUri
127
            )
128
        );
129
    }
130
131
    /**
132
     * @param Address $address
133
     */
134
    public function updateAddress(Address $address)
135
    {
136
        if (is_null($this->address) || !$this->address->sameAs($address)) {
137
            $this->apply(
138
                new AddressUpdated($this->actorId, $address)
139
            );
140
        }
141
    }
142
143
    /**
144
     * @param ContactPoint $contactPoint
145
     */
146
    public function updateContactPoint(ContactPoint $contactPoint)
147
    {
148
        if (!$this->contactPoint->sameAs($contactPoint)) {
149
            $this->apply(
150
                new ContactPointUpdated($this->actorId, $contactPoint)
151
            );
152
        }
153
    }
154
155
    /**
156
     * @param Label $label
157
     */
158
    public function addLabel(Label $label)
159
    {
160
        if (!in_array($label, $this->labels)) {
161
            $this->apply(new LabelAdded($this->actorId, $label));
162
        }
163
    }
164
165
    /**
166
     * @param Label $label
167
     */
168
    public function removeLabel(Label $label)
169
    {
170
        if (in_array($label, $this->labels)) {
171
            $this->apply(new LabelRemoved($this->actorId, $label));
172
        }
173
    }
174
175
    public function delete()
176
    {
177
        $this->apply(
178
            new OrganizerDeleted($this->getAggregateRootId())
179
        );
180
    }
181
182
    /**
183
     * Apply the organizer created event.
184
     * @param OrganizerCreated $organizerCreated
185
     */
186
    protected function applyOrganizerCreated(OrganizerCreated $organizerCreated)
187
    {
188
        $this->actorId = $organizerCreated->getOrganizerId();
189
    }
190
191
    /**
192
     * Apply the organizer created event.
193
     * @param OrganizerCreatedWithUniqueWebsite $organizerCreated
194
     */
195
    protected function applyOrganizerCreatedWithUniqueWebsite(OrganizerCreatedWithUniqueWebsite $organizerCreated)
196
    {
197
        $this->actorId = $organizerCreated->getOrganizerId();
198
    }
199
200
    /**
201
     * @param OrganizerImportedFromUDB2 $organizerImported
202
     */
203 View Code Duplication
    protected function applyOrganizerImportedFromUDB2(
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...
204
        OrganizerImportedFromUDB2 $organizerImported
205
    ) {
206
        $this->actorId = (string) $organizerImported->getActorId();
207
208
        $actor = ActorItemFactory::createActorFromCdbXml(
209
            $organizerImported->getCdbXmlNamespaceUri(),
210
            $organizerImported->getCdbXml()
211
        );
212
213
        $this->setLabelsFromUDB2Item($actor);
214
    }
215
216
    /**
217
     * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
218
     */
219
    protected function applyOrganizerUpdatedFromUDB2(
220
        OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
221
    ) {
222
        $actor = ActorItemFactory::createActorFromCdbXml(
223
            $organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(),
224
            $organizerUpdatedFromUDB2->getCdbXml()
225
        );
226
227
        $this->setLabelsFromUDB2Item($actor);
228
    }
229
230
    /**
231
     * @param AddressUpdated $addressUpdated
232
     */
233
    protected function applyAddressUpdated(AddressUpdated $addressUpdated)
234
    {
235
        $this->address = $addressUpdated->getAddress();
236
    }
237
238
    /**
239
     * @param ContactPointUpdated $contactPointUpdated
240
     */
241
    protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated)
242
    {
243
        $this->contactPoint = $contactPointUpdated->getContactPoint();
244
    }
245
246
    /**
247
     * @param LabelAdded $labelAdded
248
     */
249
    protected function applyLabelAdded(LabelAdded $labelAdded)
250
    {
251
        $this->labels[] = $labelAdded->getLabel();
252
    }
253
254
    /**
255
     * @param LabelRemoved $labelRemoved
256
     */
257
    protected function applyLabelRemoved(LabelRemoved $labelRemoved)
258
    {
259
        $label = $labelRemoved->getLabel();
260
        $this->labels = array_diff($this->labels, [$label]);
261
    }
262
263
    /**
264
     * @param \CultureFeed_Cdb_Item_Base $udb2Item
265
     */
266 View Code Duplication
    protected function setLabelsFromUDB2Item(\CultureFeed_Cdb_Item_Base $udb2Item)
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...
267
    {
268
        $this->labels = [];
269
270
        /** @var \CultureFeed_Cdb_Data_Keyword $udb2Keyword */
271
        foreach (array_values($udb2Item->getKeywords(true)) as $udb2Keyword) {
272
            $keyword = trim($udb2Keyword->getValue());
273
            if ($keyword) {
274
                $this->labels[] = new Label($keyword, $udb2Keyword->isVisible());
275
            }
276
        }
277
    }
278
}
279