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

Organizer   B

Complexity

Total Complexity 29

Size/Duplication

Total Lines 291
Duplicated Lines 8.93 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 18
dl 26
loc 291
rs 7.3333
c 0
b 0
f 0

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