Completed
Pull Request — master (#290)
by Luc
05:33
created

Organizer   C

Complexity

Total Complexity 33

Size/Duplication

Total Lines 321
Duplicated Lines 8.1 %

Coupling/Cohesion

Components 1
Dependencies 20

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 20
dl 26
loc 321
rs 6.0823
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getAggregateRootId() 0 4 1
A __construct() 0 9 1
A importFromUDB2() 0 16 1
A create() 0 13 1
A updateWithCdbXml() 0 10 1
A updateWebsite() 0 11 3
A updateTitle() 0 8 3
A updateAddress() 0 8 3
A updateContactPoint() 0 8 2
A addLabel() 0 6 2
A removeLabel() 0 6 2
A delete() 0 6 1
A applyOrganizerCreated() 0 6 1
A applyOrganizerCreatedWithUniqueWebsite() 0 8 1
A applyOrganizerImportedFromUDB2() 14 14 1
A applyOrganizerUpdatedFromUDB2() 12 12 1
A applyWebsiteUpdated() 0 4 1
A applyTitleUpdated() 0 4 1
A applyAddressUpdated() 0 4 1
A applyContactPointUpdated() 0 4 1
A applyLabelAdded() 0 4 1
A applyLabelRemoved() 0 4 1
A getTitle() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Organizer\Events\TitleUpdated;
22
use CultuurNet\UDB3\Organizer\Events\WebsiteUpdated;
23
use CultuurNet\UDB3\Title;
24
use ValueObjects\Web\Url;
25
26
class Organizer extends EventSourcedAggregateRoot implements UpdateableWithCdbXmlInterface
27
{
28
    /**
29
     * The actor id.
30
     *
31
     * @var string
32
     */
33
    protected $actorId;
34
35
    /**
36
     * @var Url
37
     */
38
    private $website;
39
40
    /**
41
     * @var Title
42
     */
43
    private $title;
44
45
    /**
46
     * @var Address|null
47
     */
48
    private $address;
49
50
    /**
51
     * @var ContactPoint
52
     */
53
    private $contactPoint;
54
55
    /**
56
     * @var LabelCollection
57
     */
58
    private $labels;
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAggregateRootId()
64
    {
65
        return $this->actorId;
66
    }
67
68
    public function __construct()
69
    {
70
        // Contact points can be empty, but we only want to start recording
71
        // ContactPointUpdated events as soon as the organizer is updated
72
        // with a non-empty contact point. To enforce this we initialize the
73
        // aggregate state with an empty contact point.
74
        $this->contactPoint = new ContactPoint();
75
        $this->labels = new LabelCollection();
76
    }
77
78
    /**
79
     * Import from UDB2.
80
     *
81
     * @param string $actorId
82
     *   The actor id.
83
     * @param string $cdbXml
84
     *   The cdb xml.
85
     * @param string $cdbXmlNamespaceUri
86
     *   The cdb xml namespace uri.
87
     *
88
     * @return Organizer
89
     *   The actor.
90
     */
91
    public static function importFromUDB2(
92
        $actorId,
93
        $cdbXml,
94
        $cdbXmlNamespaceUri
95
    ) {
96
        $organizer = new static();
97
        $organizer->apply(
98
            new OrganizerImportedFromUDB2(
99
                $actorId,
100
                $cdbXml,
101
                $cdbXmlNamespaceUri
102
            )
103
        );
104
105
        return $organizer;
106
    }
107
108
    /**
109
     * Factory method to create a new Organizer.
110
     *
111
     * @param string $id
112
     * @param Url $website
113
     * @param Title $title
114
     * @return Organizer
115
     */
116
    public static function create(
117
        $id,
118
        Url $website,
119
        Title $title
120
    ) {
121
        $organizer = new self();
122
123
        $organizer->apply(
124
            new OrganizerCreatedWithUniqueWebsite($id, $website, $title)
125
        );
126
127
        return $organizer;
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
134
    {
135
        $this->apply(
136
            new OrganizerUpdatedFromUDB2(
137
                $this->actorId,
138
                $cdbXml,
139
                $cdbXmlNamespaceUri
140
            )
141
        );
142
    }
143
144
    /**
145
     * @param Url $website
146
     */
147
    public function updateWebsite(Url $website)
148
    {
149
        if (is_null($this->website) || !$this->website->sameValueAs($website)) {
150
            $this->apply(
151
                new WebsiteUpdated(
152
                    $this->actorId,
153
                    $website
154
                )
155
            );
156
        }
157
    }
158
159
    /**
160
     * @param Title $title
161
     */
162
    public function updateTitle(Title $title)
163
    {
164
        if ($this->title && !$this->title->sameValueAs($title)) {
165
            $this->apply(
166
                new TitleUpdated($this->actorId, $title)
167
            );
168
        }
169
    }
170
171
    /**
172
     * @param Address $address
173
     */
174
    public function updateAddress(Address $address)
175
    {
176
        if (is_null($this->address) || !$this->address->sameAs($address)) {
177
            $this->apply(
178
                new AddressUpdated($this->actorId, $address)
179
            );
180
        }
181
    }
182
183
    /**
184
     * @param ContactPoint $contactPoint
185
     */
186
    public function updateContactPoint(ContactPoint $contactPoint)
187
    {
188
        if (!$this->contactPoint->sameAs($contactPoint)) {
189
            $this->apply(
190
                new ContactPointUpdated($this->actorId, $contactPoint)
191
            );
192
        }
193
    }
194
195
    /**
196
     * @param Label $label
197
     */
198
    public function addLabel(Label $label)
199
    {
200
        if (!$this->labels->contains($label)) {
201
            $this->apply(new LabelAdded($this->actorId, $label));
202
        }
203
    }
204
205
    /**
206
     * @param Label $label
207
     */
208
    public function removeLabel(Label $label)
209
    {
210
        if ($this->labels->contains($label)) {
211
            $this->apply(new LabelRemoved($this->actorId, $label));
212
        }
213
    }
214
215
    public function delete()
216
    {
217
        $this->apply(
218
            new OrganizerDeleted($this->getAggregateRootId())
219
        );
220
    }
221
222
    /**
223
     * Apply the organizer created event.
224
     * @param OrganizerCreated $organizerCreated
225
     */
226
    protected function applyOrganizerCreated(OrganizerCreated $organizerCreated)
227
    {
228
        $this->actorId = $organizerCreated->getOrganizerId();
229
230
        $this->title = $organizerCreated->getTitle();
231
    }
232
233
    /**
234
     * Apply the organizer created event.
235
     * @param OrganizerCreatedWithUniqueWebsite $organizerCreated
236
     */
237
    protected function applyOrganizerCreatedWithUniqueWebsite(OrganizerCreatedWithUniqueWebsite $organizerCreated)
238
    {
239
        $this->actorId = $organizerCreated->getOrganizerId();
240
241
        $this->website = $organizerCreated->getWebsite();
242
243
        $this->title = $organizerCreated->getTitle();
244
    }
245
246
    /**
247
     * @param OrganizerImportedFromUDB2 $organizerImported
248
     */
249 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...
250
        OrganizerImportedFromUDB2 $organizerImported
251
    ) {
252
        $this->actorId = (string) $organizerImported->getActorId();
253
254
        $actor = ActorItemFactory::createActorFromCdbXml(
255
            $organizerImported->getCdbXmlNamespaceUri(),
256
            $organizerImported->getCdbXml()
257
        );
258
259
        $this->title = $this->getTitle($actor);
260
261
        $this->labels = LabelCollection::fromKeywords($actor->getKeywords(true));
262
    }
263
264
    /**
265
     * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
266
     */
267 View Code Duplication
    protected function applyOrganizerUpdatedFromUDB2(
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...
268
        OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
269
    ) {
270
        $actor = ActorItemFactory::createActorFromCdbXml(
271
            $organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(),
272
            $organizerUpdatedFromUDB2->getCdbXml()
273
        );
274
275
        $this->title = $this->getTitle($actor);
276
277
        $this->labels = LabelCollection::fromKeywords($actor->getKeywords(true));
278
    }
279
280
    /**
281
     * @param WebsiteUpdated $websiteUpdated
282
     */
283
    protected function applyWebsiteUpdated(WebsiteUpdated $websiteUpdated)
284
    {
285
        $this->website = $websiteUpdated->getWebsite();
286
    }
287
288
    /**
289
     * @param TitleUpdated $titleUpdated
290
     */
291
    protected function applyTitleUpdated(TitleUpdated $titleUpdated)
292
    {
293
        $this->title = $titleUpdated->getTitle();
294
    }
295
296
    /**
297
     * @param AddressUpdated $addressUpdated
298
     */
299
    protected function applyAddressUpdated(AddressUpdated $addressUpdated)
300
    {
301
        $this->address = $addressUpdated->getAddress();
302
    }
303
304
    /**
305
     * @param ContactPointUpdated $contactPointUpdated
306
     */
307
    protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated)
308
    {
309
        $this->contactPoint = $contactPointUpdated->getContactPoint();
310
    }
311
312
    /**
313
     * @param LabelAdded $labelAdded
314
     */
315
    protected function applyLabelAdded(LabelAdded $labelAdded)
316
    {
317
        $this->labels = $this->labels->with($labelAdded->getLabel());
318
    }
319
320
    /**
321
     * @param LabelRemoved $labelRemoved
322
     */
323
    protected function applyLabelRemoved(LabelRemoved $labelRemoved)
324
    {
325
        $this->labels = $this->labels->without($labelRemoved->getLabel());
326
    }
327
328
    /**
329
     * @param \CultureFeed_Cdb_Item_Actor $actor
330
     * @return null|Title
331
     */
332
    private function getTitle(\CultureFeed_Cdb_Item_Actor $actor)
333
    {
334
        $details = $actor->getDetails();
335
        $details->rewind();
336
337
        // The first language detail found will be used to retrieve
338
        // properties from which in UDB3 are not any longer considered
339
        // to be language specific.
340
        if ($details->valid()) {
341
            return new Title($details->current()->getTitle());
342
        } else {
343
            return null;
344
        }
345
    }
346
}
347