Completed
Push — develop ( ab63ec...7fd1ca )
by A.
10s
created

InstitutionConfiguration::rebuild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
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\InstitutionConfigurationRemovedEvent;
25
use Surfnet\Stepup\Configuration\Event\NewInstitutionConfigurationCreatedEvent;
26
use Surfnet\Stepup\Configuration\Event\RaLocationAddedEvent;
27
use Surfnet\Stepup\Configuration\Event\RaLocationContactInformationChangedEvent;
28
use Surfnet\Stepup\Configuration\Event\RaLocationRelocatedEvent;
29
use Surfnet\Stepup\Configuration\Event\RaLocationRemovedEvent;
30
use Surfnet\Stepup\Configuration\Event\RaLocationRenamedEvent;
31
use Surfnet\Stepup\Configuration\Event\ShowRaaContactInformationOptionChangedEvent;
32
use Surfnet\Stepup\Configuration\Event\UseRaLocationsOptionChangedEvent;
33
use Surfnet\Stepup\Configuration\Value\ContactInformation;
34
use Surfnet\Stepup\Configuration\Value\Institution;
35
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
36
use Surfnet\Stepup\Configuration\Value\Location;
37
use Surfnet\Stepup\Configuration\Value\RaLocationId;
38
use Surfnet\Stepup\Configuration\Value\RaLocationList;
39
use Surfnet\Stepup\Configuration\Value\RaLocationName;
40
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
41
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
42
use Surfnet\Stepup\Exception\DomainException;
43
44
/**
45
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Events and value objects
46
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) AggregateRoot
47
 */
48
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface
49
{
50
    /**
51
     * @var InstitutionConfigurationId
52
     */
53
    private $institutionConfigurationId;
54
55
    /**
56
     * @var Institution
57
     */
58
    private $institution;
59
60
    /**
61
     * @var RaLocationList
62
     */
63
    private $raLocations;
64
65
    /**
66
     * @var UseRaLocationsOption
67
     */
68
    private $useRaLocationsOption;
69
70
    /**
71
     * @var ShowRaaContactInformationOption
72
     */
73
    private $showRaaContactInformationOption;
74
75
    /**
76
     * @var boolean
77
     */
78
    private $isMarkedAsDestroyed;
79
80
    /**
81
     * @param InstitutionConfigurationId $institutionConfigurationId
82
     * @param Institution $institution
83
     * @return InstitutionConfiguration
84
     */
85
    public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution)
86
    {
87
        $useRaLocationsOption            = UseRaLocationsOption::getDefault();
88
        $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...
89
90
        $institutionConfiguration = new self;
91
        $institutionConfiguration->apply(
92
            new NewInstitutionConfigurationCreatedEvent(
93
                $institutionConfigurationId,
94
                $institution,
95
                $useRaLocationsOption,
96
                $showRaaContactInformationOption
97
            )
98
        );
99
100
        return $institutionConfiguration;
101
    }
102
103
    /**
104
     * @return InstitutionConfiguration
105
     */
106
    public function rebuild()
107
    {
108
        // We can only rebuild a destroyed InstitutionConfiguration, all other cases are not valid
109
        if ($this->isMarkedAsDestroyed !== true) {
110
            throw new DomainException('Cannot rebuild InstitutionConfiguration as it has not been destroyed');
111
        }
112
113
        $this->apply(
114
            new NewInstitutionConfigurationCreatedEvent(
115
                $this->institutionConfigurationId,
116
                $this->institution,
117
                UseRaLocationsOption::getDefault(),
118
                ShowRaaContactInformationOption::getDefault()
119
            )
120
        );
121
122
        return $this;
123
    }
124
125
    final public function __construct()
126
    {
127
    }
128
129
    public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
130
    {
131
        if ($this->useRaLocationsOption->equals($useRaLocationsOption)) {
132
            return;
133
        }
134
135
        $this->apply(
136
            new UseRaLocationsOptionChangedEvent(
137
                $this->institutionConfigurationId,
138
                $this->institution,
139
                $useRaLocationsOption
140
            )
141
        );
142
    }
143
144
    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...
145
    {
146
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
147
            return;
148
        }
149
150
        $this->apply(
151
            new ShowRaaContactInformationOptionChangedEvent(
152
                $this->institutionConfigurationId,
153
                $this->institution,
154
                $showRaaContactInformationOption
155
            )
156
        );
157
    }
158
159
    /**
160
     * @param RaLocationId $raLocationId
161
     * @param RaLocationName $raLocationName
162
     * @param Location $location
163
     * @param ContactInformation $contactInformation
164
     */
165
    public function addRaLocation(
166
        RaLocationId $raLocationId,
167
        RaLocationName $raLocationName,
168
        Location $location,
169
        ContactInformation $contactInformation
170
    ) {
171 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...
172
            throw new DomainException(sprintf(
173
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
174
                . ' it is already present',
175
                $raLocationId,
176
                $this->getAggregateRootId()
177
            ));
178
        }
179
180
        $this->apply(new RaLocationAddedEvent(
181
            $this->institutionConfigurationId,
182
            $this->institution,
183
            $raLocationId,
184
            $raLocationName,
185
            $location,
186
            $contactInformation
187
        ));
188
    }
189
190
    /**
191
     * @param RaLocationId $raLocationId
192
     * @param RaLocationName $raLocationName
193
     * @param Location $location
194
     * @param ContactInformation $contactInformation
195
     */
196
    public function changeRaLocation(
197
        RaLocationId $raLocationId,
198
        RaLocationName $raLocationName,
199
        Location $location,
200
        ContactInformation $contactInformation
201
    ) {
202 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...
203
            throw new DomainException(sprintf(
204
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
205
                . ' it is not present',
206
                $raLocationId,
207
                $this->getAggregateRootId()
208
            ));
209
        }
210
211
        $raLocation = $this->raLocations->getById($raLocationId);
212
213
        if (!$raLocation->getName()->equals($raLocationName)) {
214
            $this->apply(
215
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
216
            );
217
        }
218
        if (!$raLocation->getLocation()->equals($location)) {
219
            $this->apply(
220
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
221
            );
222
        }
223
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
224
            $this->apply(
225
                new RaLocationContactInformationChangedEvent(
226
                    $this->institutionConfigurationId,
227
                    $raLocationId,
228
                    $contactInformation
229
                )
230
            );
231
        }
232
    }
233
234
    /**
235
     * @param RaLocationId $raLocationId
236
     */
237
    public function removeRaLocation(RaLocationId $raLocationId)
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 remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
242
                . ' it is not present',
243
                $raLocationId,
244
                $this->getAggregateRootId()
245
            ));
246
        }
247
248
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
249
    }
250
251
    /**
252
     * @return void
253
     */
254
    public function destroy()
255
    {
256
        $this->apply(new InstitutionConfigurationRemovedEvent($this->institutionConfigurationId, $this->institution));
257
    }
258
259
    public function getAggregateRootId()
260
    {
261
        return $this->institutionConfigurationId;
262
    }
263
264
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
265
    {
266
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
267
        $this->institution                     = $event->institution;
268
        $this->useRaLocationsOption            = $event->useRaLocationsOption;
269
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
270
        $this->raLocations                     = new RaLocationList([]);
271
        $this->isMarkedAsDestroyed             = false;
272
    }
273
274
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
275
    {
276
        $this->useRaLocationsOption = $event->useRaLocationsOption;
277
    }
278
279
    protected function applyShowRaaContactInformationOptionChangedEvent(
280
        ShowRaaContactInformationOptionChangedEvent $event
281
    ) {
282
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
283
    }
284
285
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
286
    {
287
        $this->raLocations->add(
288
            RaLocation::create(
289
                $event->raLocationId,
290
                $event->raLocationName,
291
                $event->location,
292
                $event->contactInformation
293
            )
294
        );
295
    }
296
297
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
298
    {
299
        $raLocation = $this->raLocations->getById($event->raLocationId);
300
        $raLocation->rename($event->raLocationName);
301
    }
302
303
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
304
    {
305
        $raLocation = $this->raLocations->getById($event->raLocationId);
306
        $raLocation->relocate($event->location);
307
    }
308
309
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
310
    {
311
        $raLocation = $this->raLocations->getById($event->raLocationId);
312
        $raLocation->changeContactInformation($event->contactInformation);
313
    }
314
315
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
316
    {
317
        $this->raLocations->removeWithId($event->raLocationId);
318
    }
319
320
    /**
321
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
322
     * @param InstitutionConfigurationRemovedEvent $event
323
     */
324
    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...
325
    {
326
        // reset all configuration to defaults. This way, should it be rebuild, it seems it is new again
327
        $this->raLocations                     = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<Surfnet\Stepup\Co...n\Value\RaLocationList> of property $raLocations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
328
        $this->useRaLocationsOption            = UseRaLocationsOption::getDefault();
329
        $this->showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
330
331
        $this->isMarkedAsDestroyed             = true;
332
    }
333
}
334