Completed
Push — feature/sf-subset/middleware-a... ( c90b65...4cd7d0 )
by A.
04:33
created

InstitutionConfiguration::changeRaLocation()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 24

Duplication

Lines 8
Ratio 21.62 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 37
rs 8.439
cc 5
eloc 24
nc 9
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
        $institutionConfiguration = new self;
95
        $institutionConfiguration->apply(
96
            new NewInstitutionConfigurationCreatedEvent(
97
                $institutionConfigurationId,
98
                $institution,
99
                UseRaLocationsOption::getDefault(),
100
                ShowRaaContactInformationOption::getDefault()
101
            )
102
        );
103
        $institutionConfiguration->apply(new AllowedSecondFactorListUpdatedEvent(
104
            $institutionConfigurationId,
105
            $institution,
106
            AllowedSecondFactorList::blank()
107
        ));
108
109
        return $institutionConfiguration;
110
    }
111
112
    /**
113
     * @return InstitutionConfiguration
114
     */
115
    public function rebuild()
116
    {
117
        // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid
118
        if ($this->isMarkedAsDestroyed !== true) {
119
            throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed');
120
        }
121
122
        $this->apply(
123
            new NewInstitutionConfigurationCreatedEvent(
124
                $this->institutionConfigurationId,
125
                $this->institution,
126
                UseRaLocationsOption::getDefault(),
127
                ShowRaaContactInformationOption::getDefault()
128
            )
129
        );
130
        $this->apply(new AllowedSecondFactorListUpdatedEvent(
131
            $this->institutionConfigurationId,
132
            $this->institution,
133
            AllowedSecondFactorList::blank()
134
        ));
135
136
        return $this;
137
    }
138
139
    final public function __construct()
140
    {
141
    }
142
143
    public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
144
    {
145
        if ($this->useRaLocationsOption->equals($useRaLocationsOption)) {
146
            return;
147
        }
148
149
        $this->apply(
150
            new UseRaLocationsOptionChangedEvent(
151
                $this->institutionConfigurationId,
152
                $this->institution,
153
                $useRaLocationsOption
154
            )
155
        );
156
    }
157
158
    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...
159
    {
160
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
161
            return;
162
        }
163
164
        $this->apply(
165
            new ShowRaaContactInformationOptionChangedEvent(
166
                $this->institutionConfigurationId,
167
                $this->institution,
168
                $showRaaContactInformationOption
169
            )
170
        );
171
    }
172
173
    public function updateAllowedSecondFactorList(AllowedSecondFactorList $allowedSecondFactorList)
174
    {
175
        if ($this->allowedSecondFactorList->equals($allowedSecondFactorList)) {
176
            return;
177
        }
178
179
        $this->apply(
180
            new AllowedSecondFactorListUpdatedEvent(
181
                $this->institutionConfigurationId,
182
                $this->institution,
183
                $allowedSecondFactorList
184
            )
185
        );
186
    }
187
188
    /**
189
     * @param RaLocationId $raLocationId
190
     * @param RaLocationName $raLocationName
191
     * @param Location $location
192
     * @param ContactInformation $contactInformation
193
     */
194
    public function addRaLocation(
195
        RaLocationId $raLocationId,
196
        RaLocationName $raLocationName,
197
        Location $location,
198
        ContactInformation $contactInformation
199
    ) {
200 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...
201
            throw new DomainException(sprintf(
202
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
203
                . ' it is already present',
204
                $raLocationId,
205
                $this->getAggregateRootId()
206
            ));
207
        }
208
209
        $this->apply(new RaLocationAddedEvent(
210
            $this->institutionConfigurationId,
211
            $this->institution,
212
            $raLocationId,
213
            $raLocationName,
214
            $location,
215
            $contactInformation
216
        ));
217
    }
218
219
    /**
220
     * @param RaLocationId $raLocationId
221
     * @param RaLocationName $raLocationName
222
     * @param Location $location
223
     * @param ContactInformation $contactInformation
224
     */
225
    public function changeRaLocation(
226
        RaLocationId $raLocationId,
227
        RaLocationName $raLocationName,
228
        Location $location,
229
        ContactInformation $contactInformation
230
    ) {
231 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...
232
            throw new DomainException(sprintf(
233
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
234
                . ' it is not present',
235
                $raLocationId,
236
                $this->getAggregateRootId()
237
            ));
238
        }
239
240
        $raLocation = $this->raLocations->getById($raLocationId);
241
242
        if (!$raLocation->getName()->equals($raLocationName)) {
243
            $this->apply(
244
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
245
            );
246
        }
247
        if (!$raLocation->getLocation()->equals($location)) {
248
            $this->apply(
249
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
250
            );
251
        }
252
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
253
            $this->apply(
254
                new RaLocationContactInformationChangedEvent(
255
                    $this->institutionConfigurationId,
256
                    $raLocationId,
257
                    $contactInformation
258
                )
259
            );
260
        }
261
    }
262
263
    /**
264
     * @param RaLocationId $raLocationId
265
     */
266
    public function removeRaLocation(RaLocationId $raLocationId)
267
    {
268 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...
269
            throw new DomainException(sprintf(
270
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
271
                . ' it is not present',
272
                $raLocationId,
273
                $this->getAggregateRootId()
274
            ));
275
        }
276
277
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
278
    }
279
280
    /**
281
     * @return void
282
     */
283
    public function destroy()
284
    {
285
        $this->apply(new InstitutionConfigurationRemovedEvent($this->institutionConfigurationId, $this->institution));
286
    }
287
288
    public function getAggregateRootId()
289
    {
290
        return $this->institutionConfigurationId;
291
    }
292
293
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
294
    {
295
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
296
        $this->institution                     = $event->institution;
297
        $this->useRaLocationsOption            = $event->useRaLocationsOption;
298
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
299
        $this->raLocations                     = new RaLocationList([]);
300
        $this->isMarkedAsDestroyed             = false;
301
    }
302
303
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
304
    {
305
        $this->useRaLocationsOption = $event->useRaLocationsOption;
306
    }
307
308
    protected function applyShowRaaContactInformationOptionChangedEvent(
309
        ShowRaaContactInformationOptionChangedEvent $event
310
    ) {
311
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
312
    }
313
314
    protected function applyAllowedSecondFactorListUpdatedEvent(AllowedSecondFactorListUpdatedEvent $event)
315
    {
316
        $this->allowedSecondFactorList = $event->allowedSecondFactorList;
317
    }
318
319
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
320
    {
321
        $this->raLocations->add(
322
            RaLocation::create(
323
                $event->raLocationId,
324
                $event->raLocationName,
325
                $event->location,
326
                $event->contactInformation
327
            )
328
        );
329
    }
330
331
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
332
    {
333
        $raLocation = $this->raLocations->getById($event->raLocationId);
334
        $raLocation->rename($event->raLocationName);
335
    }
336
337
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
338
    {
339
        $raLocation = $this->raLocations->getById($event->raLocationId);
340
        $raLocation->relocate($event->location);
341
    }
342
343
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
344
    {
345
        $raLocation = $this->raLocations->getById($event->raLocationId);
346
        $raLocation->changeContactInformation($event->contactInformation);
347
    }
348
349
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
350
    {
351
        $this->raLocations->removeWithId($event->raLocationId);
352
    }
353
354
    /**
355
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
356
     * @param InstitutionConfigurationRemovedEvent $event
357
     */
358
    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...
359
    {
360
        // reset all configuration to defaults. This way, should it be rebuild, it seems like it is new again
361
        $this->raLocations                     = new RaLocationList([]);
362
        $this->useRaLocationsOption            = UseRaLocationsOption::getDefault();
363
        $this->showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
364
        $this->allowedSecondFactorList         = AllowedSecondFactorList::blank();
365
366
        $this->isMarkedAsDestroyed             = true;
367
    }
368
}
369