Completed
Push — master ( ace74a...38a91c )
by Daan van
04:54
created

InstitutionConfiguration::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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