Completed
Pull Request — develop (#209)
by
unknown
14:37 queued 07:30
created

configureVerifyEmailOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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