Completed
Push — feature/sf-subset/institution-... ( 7b1c92 )
by A.
04:11
created

InstitutionConfiguration::addRaLocation()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 8
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 4
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Configuration;
20
21
use Broadway\EventSourcing\EventSourcedAggregateRoot;
22
use Surfnet\Stepup\Configuration\Api\InstitutionConfiguration as InstitutionConfigurationInterface;
23
use Surfnet\Stepup\Configuration\Entity\RaLocation;
24
use Surfnet\Stepup\Configuration\Event\AllowedSecondFactorListUpdatedEvent;
25
use Surfnet\Stepup\Configuration\Event\InstitutionConfigurationRemovedEvent;
26
use Surfnet\Stepup\Configuration\Event\NewInstitutionConfigurationCreatedEvent;
27
use Surfnet\Stepup\Configuration\Event\RaLocationAddedEvent;
28
use Surfnet\Stepup\Configuration\Event\RaLocationContactInformationChangedEvent;
29
use Surfnet\Stepup\Configuration\Event\RaLocationRelocatedEvent;
30
use Surfnet\Stepup\Configuration\Event\RaLocationRemovedEvent;
31
use Surfnet\Stepup\Configuration\Event\RaLocationRenamedEvent;
32
use Surfnet\Stepup\Configuration\Event\ShowRaaContactInformationOptionChangedEvent;
33
use Surfnet\Stepup\Configuration\Event\UseRaLocationsOptionChangedEvent;
34
use Surfnet\Stepup\Configuration\Value\AllowedSecondFactorList;
35
use Surfnet\Stepup\Configuration\Value\ContactInformation;
36
use Surfnet\Stepup\Configuration\Value\Institution;
37
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
38
use Surfnet\Stepup\Configuration\Value\Location;
39
use Surfnet\Stepup\Configuration\Value\RaLocationId;
40
use Surfnet\Stepup\Configuration\Value\RaLocationList;
41
use Surfnet\Stepup\Configuration\Value\RaLocationName;
42
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
43
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
44
use Surfnet\Stepup\Exception\DomainException;
45
46
/**
47
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Events and value objects
48
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) AggregateRoot
49
 */
50
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface
51
{
52
    /**
53
     * @var InstitutionConfigurationId
54
     */
55
    private $institutionConfigurationId;
56
57
    /**
58
     * @var Institution
59
     */
60
    private $institution;
61
62
    /**
63
     * @var RaLocationList
64
     */
65
    private $raLocations;
66
67
    /**
68
     * @var UseRaLocationsOption
69
     */
70
    private $useRaLocationsOption;
71
72
    /**
73
     * @var ShowRaaContactInformationOption
74
     */
75
    private $showRaaContactInformationOption;
76
77
    /**
78
     * @var AllowedSecondFactorList
79
     */
80
    private $allowedSecondFactorList;
81
82
    /**
83
     * @var boolean
84
     */
85
    private $isMarkedAsDestroyed;
86
87
    /**
88
     * @param InstitutionConfigurationId $institutionConfigurationId
89
     * @param Institution $institution
90
     * @return InstitutionConfiguration
91
     */
92
    public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution)
93
    {
94
        $useRaLocationsOption            = UseRaLocationsOption::getDefault();
95
        $showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $showRaaContactInformationOption exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
96
        $allowedSecondFactorList         = AllowedSecondFactorList::blank();
97
98
        $institutionConfiguration = new self;
99
        $institutionConfiguration->apply(
100
            new NewInstitutionConfigurationCreatedEvent(
101
                $institutionConfigurationId,
102
                $institution,
103
                $useRaLocationsOption,
104
                $showRaaContactInformationOption
105
            )
106
        );
107
        $institutionConfiguration->apply(new AllowedSecondFactorListUpdatedEvent(
108
            $institutionConfigurationId,
109
            $institution,
110
            $allowedSecondFactorList
111
        ));
112
113
        return $institutionConfiguration;
114
    }
115
116
    /**
117
     * @return InstitutionConfiguration
118
     */
119
    public function rebuild()
120
    {
121
        // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid
122
        if ($this->isMarkedAsDestroyed !== true) {
123
            throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed');
124
        }
125
126
        $useRaLocationsOption            = UseRaLocationsOption::getDefault();
127
        $showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $showRaaContactInformationOption exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
128
        $allowedSecondFactorList         = AllowedSecondFactorList::blank();
129
130
        $this->apply(
131
            new NewInstitutionConfigurationCreatedEvent(
132
                $this->institutionConfigurationId,
133
                $this->institution,
134
                $useRaLocationsOption,
135
                $showRaaContactInformationOption
136
            )
137
        );
138
        $this->apply(new AllowedSecondFactorListUpdatedEvent(
139
            $this->institutionConfigurationId,
140
            $this->institution,
141
            $allowedSecondFactorList
142
        ));
143
144
        return $this;
145
    }
146
147
    final public function __construct()
148
    {
149
    }
150
151
    public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
152
    {
153
        if ($this->useRaLocationsOption->equals($useRaLocationsOption)) {
154
            return;
155
        }
156
157
        $this->apply(
158
            new UseRaLocationsOptionChangedEvent(
159
                $this->institutionConfigurationId,
160
                $this->institution,
161
                $useRaLocationsOption
162
            )
163
        );
164
    }
165
166
    public function configureShowRaaContactInformationOption(ShowRaaContactInformationOption $showRaaContactInformationOption)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $showRaaContactInformationOption exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
167
    {
168
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
169
            return;
170
        }
171
172
        $this->apply(
173
            new ShowRaaContactInformationOptionChangedEvent(
174
                $this->institutionConfigurationId,
175
                $this->institution,
176
                $showRaaContactInformationOption
177
            )
178
        );
179
    }
180
181
    public function updateAllowedSecondFactorList(AllowedSecondFactorList $allowedSecondFactorList)
182
    {
183
        if ($this->allowedSecondFactorList->equals($allowedSecondFactorList)) {
184
            return;
185
        }
186
187
        $this->apply(
188
            new AllowedSecondFactorListUpdatedEvent(
189
                $this->institutionConfigurationId,
190
                $this->institution,
191
                $allowedSecondFactorList
192
            )
193
        );
194
    }
195
196
    /**
197
     * @param RaLocationId $raLocationId
198
     * @param RaLocationName $raLocationName
199
     * @param Location $location
200
     * @param ContactInformation $contactInformation
201
     */
202
    public function addRaLocation(
203
        RaLocationId $raLocationId,
204
        RaLocationName $raLocationName,
205
        Location $location,
206
        ContactInformation $contactInformation
207
    ) {
208 View Code Duplication
        if ($this->raLocations->containsWithId($raLocationId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
209
            throw new DomainException(sprintf(
210
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
211
                . ' it is already present',
212
                $raLocationId,
213
                $this->getAggregateRootId()
214
            ));
215
        }
216
217
        $this->apply(new RaLocationAddedEvent(
218
            $this->institutionConfigurationId,
219
            $this->institution,
220
            $raLocationId,
221
            $raLocationName,
222
            $location,
223
            $contactInformation
224
        ));
225
    }
226
227
    /**
228
     * @param RaLocationId $raLocationId
229
     * @param RaLocationName $raLocationName
230
     * @param Location $location
231
     * @param ContactInformation $contactInformation
232
     */
233
    public function changeRaLocation(
234
        RaLocationId $raLocationId,
235
        RaLocationName $raLocationName,
236
        Location $location,
237
        ContactInformation $contactInformation
238
    ) {
239 View Code Duplication
        if (!$this->raLocations->containsWithId($raLocationId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
240
            throw new DomainException(sprintf(
241
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
242
                . ' it is not present',
243
                $raLocationId,
244
                $this->getAggregateRootId()
245
            ));
246
        }
247
248
        $raLocation = $this->raLocations->getById($raLocationId);
249
250
        if (!$raLocation->getName()->equals($raLocationName)) {
251
            $this->apply(
252
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
253
            );
254
        }
255
        if (!$raLocation->getLocation()->equals($location)) {
256
            $this->apply(
257
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
258
            );
259
        }
260
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
261
            $this->apply(
262
                new RaLocationContactInformationChangedEvent(
263
                    $this->institutionConfigurationId,
264
                    $raLocationId,
265
                    $contactInformation
266
                )
267
            );
268
        }
269
    }
270
271
    /**
272
     * @param RaLocationId $raLocationId
273
     */
274
    public function removeRaLocation(RaLocationId $raLocationId)
275
    {
276 View Code Duplication
        if (!$this->raLocations->containsWithId($raLocationId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
277
            throw new DomainException(sprintf(
278
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
279
                . ' it is not present',
280
                $raLocationId,
281
                $this->getAggregateRootId()
282
            ));
283
        }
284
285
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
286
    }
287
288
    /**
289
     * @return void
290
     */
291
    public function destroy()
292
    {
293
        $this->apply(new InstitutionConfigurationRemovedEvent($this->institutionConfigurationId, $this->institution));
294
    }
295
296
    public function getAggregateRootId()
297
    {
298
        return $this->institutionConfigurationId;
299
    }
300
301
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
302
    {
303
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
304
        $this->institution                     = $event->institution;
305
        $this->useRaLocationsOption            = $event->useRaLocationsOption;
306
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
307
        $this->raLocations                     = new RaLocationList([]);
308
        $this->isMarkedAsDestroyed             = false;
309
    }
310
311
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
312
    {
313
        $this->useRaLocationsOption = $event->useRaLocationsOption;
314
    }
315
316
    protected function applyShowRaaContactInformationOptionChangedEvent(
317
        ShowRaaContactInformationOptionChangedEvent $event
318
    ) {
319
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
320
    }
321
322
    protected function applyAllowedSecondFactorListUpdatedEvent(AllowedSecondFactorListUpdatedEvent $event)
323
    {
324
        $this->allowedSecondFactorList = $event->allowedSecondFactorList;
325
    }
326
327
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
328
    {
329
        $this->raLocations->add(
330
            RaLocation::create(
331
                $event->raLocationId,
332
                $event->raLocationName,
333
                $event->location,
334
                $event->contactInformation
335
            )
336
        );
337
    }
338
339
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
340
    {
341
        $raLocation = $this->raLocations->getById($event->raLocationId);
342
        $raLocation->rename($event->raLocationName);
343
    }
344
345
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
346
    {
347
        $raLocation = $this->raLocations->getById($event->raLocationId);
348
        $raLocation->relocate($event->location);
349
    }
350
351
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
352
    {
353
        $raLocation = $this->raLocations->getById($event->raLocationId);
354
        $raLocation->changeContactInformation($event->contactInformation);
355
    }
356
357
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
358
    {
359
        $this->raLocations->removeWithId($event->raLocationId);
360
    }
361
362
    /**
363
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
364
     * @param InstitutionConfigurationRemovedEvent $event
365
     */
366
    protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
367
    {
368
        // reset all configuration to defaults. This way, should it be rebuild, it seems like it is new again
369
        $this->raLocations                     = new RaLocationList([]);
370
        $this->useRaLocationsOption            = UseRaLocationsOption::getDefault();
371
        $this->showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
372
        $this->allowedSecondFactorList         = AllowedSecondFactorList::blank();
373
374
        $this->isMarkedAsDestroyed             = true;
375
    }
376
}
377