Completed
Push — feature/institution-configurat... ( 1b22cb )
by A.
04:01
created

applyShowRaaContactInformationOptionChangedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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\NewInstitutionConfigurationCreatedEvent;
25
use Surfnet\Stepup\Configuration\Event\RaLocationAddedEvent;
26
use Surfnet\Stepup\Configuration\Event\RaLocationContactInformationChangedEvent;
27
use Surfnet\Stepup\Configuration\Event\RaLocationRelocatedEvent;
28
use Surfnet\Stepup\Configuration\Event\RaLocationRemovedEvent;
29
use Surfnet\Stepup\Configuration\Event\RaLocationRenamedEvent;
30
use Surfnet\Stepup\Configuration\Event\ShowRaaContactInformationOptionChangedEvent;
31
use Surfnet\Stepup\Configuration\Event\UseRaLocationsOptionChangedEvent;
32
use Surfnet\Stepup\Configuration\Value\ContactInformation;
33
use Surfnet\Stepup\Configuration\Value\Institution;
34
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
35
use Surfnet\Stepup\Configuration\Value\Location;
36
use Surfnet\Stepup\Configuration\Value\RaLocationId;
37
use Surfnet\Stepup\Configuration\Value\RaLocationList;
38
use Surfnet\Stepup\Configuration\Value\RaLocationName;
39
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
40
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
41
use Surfnet\Stepup\Exception\DomainException;
42
43
/**
44
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Events and value objects
45
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) AggregateRoot
46
 */
47
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface
48
{
49
    /**
50
     * @var InstitutionConfigurationId
51
     */
52
    private $institutionConfigurationId;
53
54
    /**
55
     * @var Institution
56
     */
57
    private $institution;
58
59
    /**
60
     * @var RaLocationList
61
     */
62
    private $raLocations;
63
64
    /**
65
     * @var UseRaLocationsOption
66
     */
67
    private $useRaLocationOption;
68
69
    /**
70
     * @var ShowRaaContactInformationOption
71
     */
72
    private $showRaaContactInformationOption;
73
74
    /**
75
     * @param InstitutionConfigurationId $institutionConfigurationId
76
     * @param Institution $institution
77
     * @param UseRaLocationsOption $useRaLocationsOption
0 ignored issues
show
Documentation introduced by
Should the type for parameter $useRaLocationsOption not be null|UseRaLocationsOption?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
     * @param ShowRaaContactInformationOption $showRaaContactInformationOption
0 ignored issues
show
Documentation introduced by
Should the type for parameter $showRaaContactInformationOption not be null|ShowRaaContactInformationOption?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
79
     * @return InstitutionConfiguration
80
     */
81
    public static function create(
82
        InstitutionConfigurationId $institutionConfigurationId,
83
        Institution $institution,
84
        UseRaLocationsOption $useRaLocationsOption = null,
85
        ShowRaaContactInformationOption $showRaaContactInformationOption = null
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...
86
    ) {
87
        if ($useRaLocationsOption === null) {
88
            $useRaLocationsOption = new UseRaLocationsOption(false);
89
        }
90
91
        if ($showRaaContactInformationOption === null) {
92
            $showRaaContactInformationOption = new ShowRaaContactInformationOption(true);
93
        }
94
95
        $institutionConfiguration = new self;
96
        $institutionConfiguration->apply(
97
            new NewInstitutionConfigurationCreatedEvent(
98
                $institutionConfigurationId,
99
                $institution,
100
                $useRaLocationsOption,
101
                $showRaaContactInformationOption
102
            )
103
        );
104
105
        return $institutionConfiguration;
106
    }
107
108
    final public function __construct()
109
    {
110
    }
111
112
    public function changeUseRaLocationsOption(UseRaLocationsOption $useRaLocationsOption)
113
    {
114
        if ($this->useRaLocationOption->equals($useRaLocationsOption)) {
115
            return;
116
        }
117
118
        $this->apply(
119
            new UseRaLocationsOptionChangedEvent(
120
                $this->institutionConfigurationId,
121
                $this->institution,
122
                $useRaLocationsOption
123
            )
124
        );
125
    }
126
127
    public function changeShowRaaContactInformationOption(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 123 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...
128
    {
129
        if ($this->showRaaContactInformationOption->equals($showRaaContactInformationOption)) {
130
            return;
131
        }
132
133
        $this->apply(
134
            new ShowRaaContactInformationOptionChangedEvent(
135
                $this->institutionConfigurationId,
136
                $this->institution,
137
                $showRaaContactInformationOption
138
            )
139
        );
140
    }
141
142
    /**
143
     * @param RaLocationId $raLocationId
144
     * @param RaLocationName $raLocationName
145
     * @param Location $location
146
     * @param ContactInformation $contactInformation
147
     */
148
    public function addRaLocation(
149
        RaLocationId $raLocationId,
150
        RaLocationName $raLocationName,
151
        Location $location,
152
        ContactInformation $contactInformation
153
    ) {
154 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...
155
            throw new DomainException(sprintf(
156
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
157
                . ' it is already present',
158
                $raLocationId,
159
                $this->getAggregateRootId()
160
            ));
161
        }
162
163
        $this->apply(new RaLocationAddedEvent(
164
            $this->institutionConfigurationId,
165
            $this->institution,
166
            $raLocationId,
167
            $raLocationName,
168
            $location,
169
            $contactInformation
170
        ));
171
    }
172
173
    /**
174
     * @param RaLocationId $raLocationId
175
     * @param RaLocationName $raLocationName
176
     * @param Location $location
177
     * @param ContactInformation $contactInformation
178
     */
179
    public function changeRaLocation(
180
        RaLocationId $raLocationId,
181
        RaLocationName $raLocationName,
182
        Location $location,
183
        ContactInformation $contactInformation
184
    ) {
185 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...
186
            throw new DomainException(sprintf(
187
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
188
                . ' it is not present',
189
                $raLocationId,
190
                $this->getAggregateRootId()
191
            ));
192
        }
193
194
        $raLocation = $this->raLocations->getById($raLocationId);
195
196
        if (!$raLocation->getName()->equals($raLocationName)) {
197
            $this->apply(
198
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
199
            );
200
        }
201
        if (!$raLocation->getLocation()->equals($location)) {
202
            $this->apply(
203
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
204
            );
205
        }
206
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
207
            $this->apply(
208
                new RaLocationContactInformationChangedEvent(
209
                    $this->institutionConfigurationId,
210
                    $raLocationId,
211
                    $contactInformation
212
                )
213
            );
214
        }
215
    }
216
217
    /**
218
     * @param RaLocationId $raLocationId
219
     */
220
    public function removeRaLocation(RaLocationId $raLocationId)
221
    {
222 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...
223
            throw new DomainException(sprintf(
224
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
225
                . ' it is not present',
226
                $raLocationId,
227
                $this->getAggregateRootId()
228
            ));
229
        }
230
231
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
232
    }
233
234
    public function getAggregateRootId()
235
    {
236
        return $this->institutionConfigurationId;
237
    }
238
239
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
240
    {
241
        $this->institutionConfigurationId      = $event->institutionConfigurationId;
242
        $this->institution                     = $event->institution;
243
        $this->useRaLocationOption             = $event->useRaLocationsOption;
244
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
245
        $this->raLocations                     = new RaLocationList([]);
246
    }
247
248
    protected function applyUseRaLocationsOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
249
    {
250
        $this->useRaLocationOption = $event->useRaLocationsOption;
251
    }
252
253
    protected function applyShowRaaContactInformationOptionChangedEvent(
254
        ShowRaaContactInformationOptionChangedEvent $event
255
    ) {
256
        $this->showRaaContactInformationOption = $event->showRaaContactInformationOption;
257
    }
258
259
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
260
    {
261
        $this->raLocations->add(
262
            RaLocation::create(
263
                $event->raLocationId,
264
                $event->raLocationName,
265
                $event->location,
266
                $event->contactInformation
267
            )
268
        );
269
    }
270
271
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
272
    {
273
        $raLocation = $this->raLocations->getById($event->raLocationId);
274
        $raLocation->rename($event->raLocationName);
275
    }
276
277
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
278
    {
279
        $raLocation = $this->raLocations->getById($event->raLocationId);
280
        $raLocation->relocate($event->location);
281
    }
282
283
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
284
    {
285
        $raLocation = $this->raLocations->getById($event->raLocationId);
286
        $raLocation->changeContactInformation($event->contactInformation);
287
    }
288
289
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
290
    {
291
        $this->raLocations->removeWithId($event->raLocationId);
292
    }
293
}
294