Completed
Push — feature/update-inst-conf-aggre... ( d009e4 )
by Michiel
02:33
created

applyRaLocationRemovedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\NumberOfTokensPerIdentityOptionChangedEvent;
28
use Surfnet\Stepup\Configuration\Event\RaLocationAddedEvent;
29
use Surfnet\Stepup\Configuration\Event\RaLocationContactInformationChangedEvent;
30
use Surfnet\Stepup\Configuration\Event\RaLocationRelocatedEvent;
31
use Surfnet\Stepup\Configuration\Event\RaLocationRemovedEvent;
32
use Surfnet\Stepup\Configuration\Event\RaLocationRenamedEvent;
33
use Surfnet\Stepup\Configuration\Event\SelectRaaOptionChangedEvent;
34
use Surfnet\Stepup\Configuration\Event\ShowRaaContactInformationOptionChangedEvent;
35
use Surfnet\Stepup\Configuration\Event\UseRaaOptionChangedEvent;
36
use Surfnet\Stepup\Configuration\Event\UseRaLocationsOptionChangedEvent;
37
use Surfnet\Stepup\Configuration\Event\UseRaOptionChangedEvent;
38
use Surfnet\Stepup\Configuration\Event\VerifyEmailOptionChangedEvent;
39
use Surfnet\Stepup\Configuration\Value\AllowedSecondFactorList;
40
use Surfnet\Stepup\Configuration\Value\ContactInformation;
41
use Surfnet\Stepup\Configuration\Value\Institution;
42
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
43
use Surfnet\Stepup\Configuration\Value\Location;
44
use Surfnet\Stepup\Configuration\Value\NumberOfTokensPerIdentityOption;
45
use Surfnet\Stepup\Configuration\Value\RaLocationId;
46
use Surfnet\Stepup\Configuration\Value\RaLocationList;
47
use Surfnet\Stepup\Configuration\Value\RaLocationName;
48
use Surfnet\Stepup\Configuration\Value\SelectRaaOption;
49
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
50
use Surfnet\Stepup\Configuration\Value\UseRaaOption;
51
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
52
use Surfnet\Stepup\Configuration\Value\UseRaOption;
53
use Surfnet\Stepup\Configuration\Value\VerifyEmailOption;
54
use Surfnet\Stepup\Exception\DomainException;
55
56
/**
57
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Events and value objects
58
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) AggregateRoot
59
 */
60
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface
61
{
62
    /**
63
     * @var InstitutionConfigurationId
64
     */
65
    private $institutionConfigurationId;
66
67
    /**
68
     * @var Institution
69
     */
70
    private $institution;
71
72
    /**
73
     * @var RaLocationList
74
     */
75
    private $raLocations;
76
77
    /**
78
     * @var UseRaLocationsOption
79
     */
80
    private $useRaLocationsOption;
81
82
    /**
83
     * @var ShowRaaContactInformationOption
84
     */
85
    private $showRaaContactInformationOption;
86
87
    /**
88
     * @var VerifyEmailOption
89
     */
90
    private $verifyEmailOption;
91
92
    /**
93
     * @var NumberOfTokensPerIdentityOption
94
     */
95
    private $numberOfTokensPerIdentityOption;
96
97
    /**
98
     * @var UseRaOption
99
     */
100
    private $useRaOption;
101
102
    /**
103
     * @var UseRaaOption
104
     */
105
106
    private $useRaaOption;
107
108
    /**
109
     * @var SelectRaaOption
110
     */
111
    private $selectRaaOption;
112
113
    /**
114
     * @var AllowedSecondFactorList
115
     */
116
    private $allowedSecondFactorList;
117
118
    /**
119
     * @var boolean
120
     */
121
    private $isMarkedAsDestroyed;
122
123
    /**
124
     * @param InstitutionConfigurationId $institutionConfigurationId
125
     * @param Institution $institution
126
     * @return InstitutionConfiguration
127
     */
128
    public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution)
129
    {
130
        $institutionConfiguration = new self;
131
        $institutionConfiguration->apply(
132
            new NewInstitutionConfigurationCreatedEvent(
133
                $institutionConfigurationId,
134
                $institution,
135
                UseRaLocationsOption::getDefault(),
136
                ShowRaaContactInformationOption::getDefault(),
137
                VerifyEmailOption::getDefault(),
138
                NumberOfTokensPerIdentityOption::getDefault(),
139
                UseRaOption::getDefault(),
140
                UseRaaOption::getDefault(),
141
                SelectRaaOption::getDefault()
142
            )
143
        );
144
        $institutionConfiguration->apply(new AllowedSecondFactorListUpdatedEvent(
145
            $institutionConfigurationId,
146
            $institution,
147
            AllowedSecondFactorList::blank()
148
        ));
149
150
        return $institutionConfiguration;
151
    }
152
153
    /**
154
     * @return InstitutionConfiguration
155
     */
156
    public function rebuild()
157
    {
158
        // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid
159
        if ($this->isMarkedAsDestroyed !== true) {
160
            throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed');
161
        }
162
163
        $this->apply(
164
            new NewInstitutionConfigurationCreatedEvent(
165
                $this->institutionConfigurationId,
166
                $this->institution,
167
                UseRaLocationsOption::getDefault(),
168
                ShowRaaContactInformationOption::getDefault(),
169
                VerifyEmailOption::getDefault(),
170
                NumberOfTokensPerIdentityOption::getDefault(),
171
                UseRaOption::getDefault(),
172
                UseRaaOption::getDefault(),
173
                SelectRaaOption::getDefault()
174
            )
175
        );
176
        $this->apply(new AllowedSecondFactorListUpdatedEvent(
177
            $this->institutionConfigurationId,
178
            $this->institution,
179
            AllowedSecondFactorList::blank()
180
        ));
181
182
        return $this;
183
    }
184
185
    final public function __construct()
186
    {
187
    }
188
189
    public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
190
    {
191
        if ($this->useRaLocationsOption->equals($useRaLocationsOption)) {
192
            return;
193
        }
194
195
        $this->apply(
196
            new UseRaLocationsOptionChangedEvent(
197
                $this->institutionConfigurationId,
198
                $this->institution,
199
                $useRaLocationsOption
200
            )
201
        );
202
    }
203
204
    public function configureShowRaaContactInformationOption(ShowRaaContactInformationOption $showRaaContactInformationOption)
205
    {
206
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
207
            return;
208
        }
209
210
        $this->apply(
211
            new ShowRaaContactInformationOptionChangedEvent(
212
                $this->institutionConfigurationId,
213
                $this->institution,
214
                $showRaaContactInformationOption
215
            )
216
        );
217
    }
218
219
    public function configureVerifyEmailOption(VerifyEmailOption $verifyEmailOption)
220
    {
221
        if ($this->verifyEmailOption->equals($verifyEmailOption)) {
222
            return;
223
        }
224
225
        $this->apply(
226
            new VerifyEmailOptionChangedEvent(
227
                $this->institutionConfigurationId,
228
                $this->institution,
229
                $verifyEmailOption
230
            )
231
        );
232
    }
233
234
    public function configureNumberOfTokensPerIdentityOption(
235
        NumberOfTokensPerIdentityOption $numberOfTokensPerIdentityOption
236
    ) {
237
        if ($this->numberOfTokensPerIdentityOption->equals($numberOfTokensPerIdentityOption)) {
238
            return;
239
        }
240
241
        $this->apply(
242
            new NumberOfTokensPerIdentityOptionChangedEvent(
243
                $this->institutionConfigurationId,
244
                $this->institution,
245
                $numberOfTokensPerIdentityOption
246
            )
247
        );
248
    }
249
250
    public function configureUseRaOption(UseRaOption $useRaOption)
251
    {
252
        if ($this->useRaOption->equals($useRaOption)) {
253
            return;
254
        }
255
256
        $this->apply(
257
            new UseRaOptionChangedEvent(
258
                $this->institutionConfigurationId,
259
                $this->institution,
260
                $useRaOption
261
            )
262
        );
263
    }
264
265
    public function configureUseRaaOption(UseRaaOption $useRaaOption)
266
    {
267
        if ($this->useRaaOption->equals($useRaaOption)) {
268
            return;
269
        }
270
271
        $this->apply(
272
            new UseRaaOptionChangedEvent(
273
                $this->institutionConfigurationId,
274
                $this->institution,
275
                $useRaaOption
276
            )
277
        );
278
    }
279
280
    public function configureSelectRaaOption(SelectRaaOption $selectRaaOption)
281
    {
282
        if ($this->selectRaaOption->equals($selectRaaOption)) {
283
            return;
284
        }
285
286
        $this->apply(
287
            new SelectRaaOptionChangedEvent(
288
                $this->institutionConfigurationId,
289
                $this->institution,
290
                $selectRaaOption
291
            )
292
        );
293
    }
294
295
    public function updateAllowedSecondFactorList(AllowedSecondFactorList $allowedSecondFactorList)
296
    {
297
        // AllowedSecondFactorList can be null for InstitutionConfigurations for which this functionality did not exist
298
        if ($this->allowedSecondFactorList !== null
299
            && $this->allowedSecondFactorList->equals($allowedSecondFactorList)
300
        ) {
301
            return;
302
        }
303
304
        $this->apply(
305
            new AllowedSecondFactorListUpdatedEvent(
306
                $this->institutionConfigurationId,
307
                $this->institution,
308
                $allowedSecondFactorList
309
            )
310
        );
311
    }
312
313
    /**
314
     * @param RaLocationId $raLocationId
315
     * @param RaLocationName $raLocationName
316
     * @param Location $location
317
     * @param ContactInformation $contactInformation
318
     */
319
    public function addRaLocation(
320
        RaLocationId $raLocationId,
321
        RaLocationName $raLocationName,
322
        Location $location,
323
        ContactInformation $contactInformation
324
    ) {
325 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...
326
            throw new DomainException(sprintf(
327
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
328
                . ' it is already present',
329
                $raLocationId,
330
                $this->getAggregateRootId()
331
            ));
332
        }
333
334
        $this->apply(new RaLocationAddedEvent(
335
            $this->institutionConfigurationId,
336
            $this->institution,
337
            $raLocationId,
338
            $raLocationName,
339
            $location,
340
            $contactInformation
341
        ));
342
    }
343
344
    /**
345
     * @param RaLocationId $raLocationId
346
     * @param RaLocationName $raLocationName
347
     * @param Location $location
348
     * @param ContactInformation $contactInformation
349
     */
350
    public function changeRaLocation(
351
        RaLocationId $raLocationId,
352
        RaLocationName $raLocationName,
353
        Location $location,
354
        ContactInformation $contactInformation
355
    ) {
356 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...
357
            throw new DomainException(sprintf(
358
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
359
                . ' it is not present',
360
                $raLocationId,
361
                $this->getAggregateRootId()
362
            ));
363
        }
364
365
        $raLocation = $this->raLocations->getById($raLocationId);
366
367
        if (!$raLocation->getName()->equals($raLocationName)) {
368
            $this->apply(
369
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
370
            );
371
        }
372
        if (!$raLocation->getLocation()->equals($location)) {
373
            $this->apply(
374
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
375
            );
376
        }
377
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
378
            $this->apply(
379
                new RaLocationContactInformationChangedEvent(
380
                    $this->institutionConfigurationId,
381
                    $raLocationId,
382
                    $contactInformation
383
                )
384
            );
385
        }
386
    }
387
388
    /**
389
     * @param RaLocationId $raLocationId
390
     */
391
    public function removeRaLocation(RaLocationId $raLocationId)
392
    {
393 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...
394
            throw new DomainException(sprintf(
395
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
396
                . ' it is not present',
397
                $raLocationId,
398
                $this->getAggregateRootId()
399
            ));
400
        }
401
402
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
403
    }
404
405
    /**
406
     * @return void
407
     */
408
    public function destroy()
409
    {
410
        $this->apply(new InstitutionConfigurationRemovedEvent($this->institutionConfigurationId, $this->institution));
411
    }
412
413
    public function getAggregateRootId()
414
    {
415
        return $this->institutionConfigurationId;
416
    }
417
418
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
419
    {
420
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
421
        $this->institution                     = $event->institution;
422
        $this->useRaLocationsOption            = $event->useRaLocationsOption;
423
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
424
        $this->verifyEmailOption               = $event->verifyEmailOption;
425
        $this->numberOfTokensPerIdentityOption = $event->numberOfTokensPerIdentityOption;
426
        $this->raLocations                     = new RaLocationList([]);
427
        $this->isMarkedAsDestroyed             = false;
428
    }
429
430
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
431
    {
432
        $this->useRaLocationsOption = $event->useRaLocationsOption;
433
    }
434
435
    protected function applyShowRaaContactInformationOptionChangedEvent(
436
        ShowRaaContactInformationOptionChangedEvent $event
437
    ) {
438
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
439
    }
440
441
    protected function applyVerifyEmailOptionChangedEvent(
442
        VerifyEmailOptionChangedEvent $event
443
    ) {
444
        $this->verifyEmailOption = $event->verifyEmailOption;
445
    }
446
447
    protected function applyNumberOfTokensPerIdentityOptionChangedEvent(
448
        NumberOfTokensPerIdentityOptionChangedEvent $event
449
    ) {
450
        $this->numberOfTokensPerIdentityOption = $event->numberOfTokensPerIdentityOption;
451
    }
452
453
    protected function applyAllowedSecondFactorListUpdatedEvent(AllowedSecondFactorListUpdatedEvent $event)
454
    {
455
        $this->allowedSecondFactorList = $event->allowedSecondFactorList;
456
    }
457
458
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
459
    {
460
        $this->raLocations->add(
461
            RaLocation::create(
462
                $event->raLocationId,
463
                $event->raLocationName,
464
                $event->location,
465
                $event->contactInformation
466
            )
467
        );
468
    }
469
470
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
471
    {
472
        $raLocation = $this->raLocations->getById($event->raLocationId);
473
        $raLocation->rename($event->raLocationName);
474
    }
475
476
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
477
    {
478
        $raLocation = $this->raLocations->getById($event->raLocationId);
479
        $raLocation->relocate($event->location);
480
    }
481
482
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
483
    {
484
        $raLocation = $this->raLocations->getById($event->raLocationId);
485
        $raLocation->changeContactInformation($event->contactInformation);
486
    }
487
488
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
489
    {
490
        $this->raLocations->removeWithId($event->raLocationId);
491
    }
492
493
    /**
494
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
495
     * @param InstitutionConfigurationRemovedEvent $event
496
     */
497
    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...
498
    {
499
        // reset all configuration to defaults. This way, should it be rebuild, it seems like it is new again
500
        $this->raLocations                     = new RaLocationList([]);
501
        $this->useRaLocationsOption            = UseRaLocationsOption::getDefault();
502
        $this->showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
503
        $this->verifyEmailOption               = VerifyEmailOption::getDefault();
504
        $this->numberOfTokensPerIdentityOption = NumberOfTokensPerIdentityOption::getDefault();
505
        $this->allowedSecondFactorList         = AllowedSecondFactorList::blank();
506
507
        $this->isMarkedAsDestroyed             = true;
508
    }
509
}
510