Completed
Pull Request — develop (#166)
by Daan van
22:27 queued 19:06
created

applyInstitutionConfigurationRemovedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 4
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\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
     * @param InstitutionConfigurationId $institutionConfigurationId
77
     * @param Institution $institution
78
     * @return InstitutionConfiguration
79
     */
80
    public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution)
81
    {
82
        $useRaLocationsOption            = UseRaLocationsOption::getDefault();
83
        $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...
84
85
        $institutionConfiguration = new self;
86
        $institutionConfiguration->apply(
87
            new NewInstitutionConfigurationCreatedEvent(
88
                $institutionConfigurationId,
89
                $institution,
90
                $useRaLocationsOption,
91
                $showRaaContactInformationOption
92
            )
93
        );
94
95
        return $institutionConfiguration;
96
    }
97
98
    final public function __construct()
99
    {
100
    }
101
102
    public function configureUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
103
    {
104
        if ($this->useRaLocationsOption->equals($useRaLocationsOption)) {
105
            return;
106
        }
107
108
        $this->apply(
109
            new UseRaLocationsOptionChangedEvent(
110
                $this->institutionConfigurationId,
111
                $this->institution,
112
                $useRaLocationsOption
113
            )
114
        );
115
    }
116
117
    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...
118
    {
119
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
120
            return;
121
        }
122
123
        $this->apply(
124
            new ShowRaaContactInformationOptionChangedEvent(
125
                $this->institutionConfigurationId,
126
                $this->institution,
127
                $showRaaContactInformationOption
128
            )
129
        );
130
    }
131
132
    /**
133
     * @param RaLocationId $raLocationId
134
     * @param RaLocationName $raLocationName
135
     * @param Location $location
136
     * @param ContactInformation $contactInformation
137
     */
138
    public function addRaLocation(
139
        RaLocationId $raLocationId,
140
        RaLocationName $raLocationName,
141
        Location $location,
142
        ContactInformation $contactInformation
143
    ) {
144 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...
145
            throw new DomainException(sprintf(
146
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
147
                . ' it is already present',
148
                $raLocationId,
149
                $this->getAggregateRootId()
150
            ));
151
        }
152
153
        $this->apply(new RaLocationAddedEvent(
154
            $this->institutionConfigurationId,
155
            $this->institution,
156
            $raLocationId,
157
            $raLocationName,
158
            $location,
159
            $contactInformation
160
        ));
161
    }
162
163
    /**
164
     * @param RaLocationId $raLocationId
165
     * @param RaLocationName $raLocationName
166
     * @param Location $location
167
     * @param ContactInformation $contactInformation
168
     */
169
    public function changeRaLocation(
170
        RaLocationId $raLocationId,
171
        RaLocationName $raLocationName,
172
        Location $location,
173
        ContactInformation $contactInformation
174
    ) {
175 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...
176
            throw new DomainException(sprintf(
177
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
178
                . ' it is not present',
179
                $raLocationId,
180
                $this->getAggregateRootId()
181
            ));
182
        }
183
184
        $raLocation = $this->raLocations->getById($raLocationId);
185
186
        if (!$raLocation->getName()->equals($raLocationName)) {
187
            $this->apply(
188
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
189
            );
190
        }
191
        if (!$raLocation->getLocation()->equals($location)) {
192
            $this->apply(
193
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
194
            );
195
        }
196
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
197
            $this->apply(
198
                new RaLocationContactInformationChangedEvent(
199
                    $this->institutionConfigurationId,
200
                    $raLocationId,
201
                    $contactInformation
202
                )
203
            );
204
        }
205
    }
206
207
    /**
208
     * @param RaLocationId $raLocationId
209
     */
210
    public function removeRaLocation(RaLocationId $raLocationId)
211
    {
212 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...
213
            throw new DomainException(sprintf(
214
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
215
                . ' it is not present',
216
                $raLocationId,
217
                $this->getAggregateRootId()
218
            ));
219
        }
220
221
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
222
    }
223
224
    /**
225
     * @return void
226
     */
227
    public function destroy()
228
    {
229
        $this->apply(new InstitutionConfigurationRemovedEvent($this->institutionConfigurationId, $this->institution));
0 ignored issues
show
Unused Code introduced by
The call to InstitutionConfiguration...vedEvent::__construct() has too many arguments starting with $this->institutionConfigurationId.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
230
    }
231
232
    public function getAggregateRootId()
233
    {
234
        return $this->institutionConfigurationId;
235
    }
236
237
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
238
    {
239
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
240
        $this->institution                     = $event->institution;
241
        $this->useRaLocationsOption            = $event->useRaLocationsOption;
242
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
243
        $this->raLocations                     = new RaLocationList([]);
244
    }
245
246
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
247
    {
248
        $this->useRaLocationsOption = $event->useRaLocationsOption;
249
    }
250
251
    protected function applyShowRaaContactInformationOptionChangedEvent(
252
        ShowRaaContactInformationOptionChangedEvent $event
253
    ) {
254
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
255
    }
256
257
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
258
    {
259
        $this->raLocations->add(
260
            RaLocation::create(
261
                $event->raLocationId,
262
                $event->raLocationName,
263
                $event->location,
264
                $event->contactInformation
265
            )
266
        );
267
    }
268
269
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
270
    {
271
        $raLocation = $this->raLocations->getById($event->raLocationId);
272
        $raLocation->rename($event->raLocationName);
273
    }
274
275
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
276
    {
277
        $raLocation = $this->raLocations->getById($event->raLocationId);
278
        $raLocation->relocate($event->location);
279
    }
280
281
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
282
    {
283
        $raLocation = $this->raLocations->getById($event->raLocationId);
284
        $raLocation->changeContactInformation($event->contactInformation);
285
    }
286
287
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
288
    {
289
        $this->raLocations->removeWithId($event->raLocationId);
290
    }
291
292
    /**
293
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
294
     * @param InstitutionConfigurationRemovedEvent $event
295
     */
296
    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...
297
    {
298
        // reset all configuration to defaults. This way, should it be rebuild, it seems it is new again
299
        $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...
300
        $this->useRaLocationsOption            = UseRaLocationsOption::getDefault();
301
        $this->showRaaContactInformationOption = ShowRaaContactInformationOption::getDefault();
302
    }
303
}
304