Completed
Push — master ( a3c457...aab356 )
by
unknown
03:38
created

applyRaLocationRemovedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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