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\Value\ContactInformation; |
31
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
32
|
|
|
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId; |
33
|
|
|
use Surfnet\Stepup\Configuration\Value\Location; |
34
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationId; |
35
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationList; |
36
|
|
|
use Surfnet\Stepup\Configuration\Value\RaLocationName; |
37
|
|
|
use Surfnet\Stepup\Exception\DomainException; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) Events and value objects |
41
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) AggregateRoot |
42
|
|
|
*/ |
43
|
|
|
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var InstitutionConfigurationId |
47
|
|
|
*/ |
48
|
|
|
private $institutionConfigurationId; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Institution |
52
|
|
|
*/ |
53
|
|
|
private $institution; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var RaLocationList |
57
|
|
|
*/ |
58
|
|
|
private $raLocations; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param InstitutionConfigurationId $institutionConfigurationId |
62
|
|
|
* @param Institution $institution |
63
|
|
|
* @return InstitutionConfiguration |
64
|
|
|
*/ |
65
|
|
|
public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution) |
66
|
|
|
{ |
67
|
|
|
$institutionConfiguration = new self; |
68
|
|
|
$institutionConfiguration->apply( |
69
|
|
|
new NewInstitutionConfigurationCreatedEvent($institutionConfigurationId, $institution) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $institutionConfiguration; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
final public function __construct() |
76
|
|
|
{ |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param RaLocationId $raLocationId |
81
|
|
|
* @param RaLocationName $raLocationName |
82
|
|
|
* @param Location $location |
83
|
|
|
* @param ContactInformation $contactInformation |
84
|
|
|
*/ |
85
|
|
|
public function addRaLocation( |
86
|
|
|
RaLocationId $raLocationId, |
87
|
|
|
RaLocationName $raLocationName, |
88
|
|
|
Location $location, |
89
|
|
|
ContactInformation $contactInformation |
90
|
|
|
) { |
91
|
|
View Code Duplication |
if ($this->raLocations->containsWithId($raLocationId)) { |
|
|
|
|
92
|
|
|
throw new DomainException(sprintf( |
93
|
|
|
'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":' |
94
|
|
|
. ' it is already present', |
95
|
|
|
$raLocationId, |
96
|
|
|
$this->getAggregateRootId() |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->apply(new RaLocationAddedEvent( |
101
|
|
|
$this->institutionConfigurationId, |
102
|
|
|
$this->institution, |
103
|
|
|
$raLocationId, |
104
|
|
|
$raLocationName, |
105
|
|
|
$location, |
106
|
|
|
$contactInformation |
107
|
|
|
)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param RaLocationId $raLocationId |
112
|
|
|
* @param RaLocationName $raLocationName |
113
|
|
|
* @param Location $location |
114
|
|
|
* @param ContactInformation $contactInformation |
115
|
|
|
*/ |
116
|
|
|
public function changeRaLocation( |
117
|
|
|
RaLocationId $raLocationId, |
118
|
|
|
RaLocationName $raLocationName, |
119
|
|
|
Location $location, |
120
|
|
|
ContactInformation $contactInformation |
121
|
|
|
) { |
122
|
|
View Code Duplication |
if (!$this->raLocations->containsWithId($raLocationId)) { |
|
|
|
|
123
|
|
|
throw new DomainException(sprintf( |
124
|
|
|
'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":' |
125
|
|
|
. ' it is not present', |
126
|
|
|
$raLocationId, |
127
|
|
|
$this->getAggregateRootId() |
128
|
|
|
)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$raLocation = $this->raLocations->getById($raLocationId); |
132
|
|
|
|
133
|
|
|
if (!$raLocation->getName()->equals($raLocationName)) { |
134
|
|
|
$this->apply( |
135
|
|
|
new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
if (!$raLocation->getLocation()->equals($location)) { |
139
|
|
|
$this->apply( |
140
|
|
|
new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
if (!$raLocation->getContactInformation()->equals($contactInformation)) { |
144
|
|
|
$this->apply( |
145
|
|
|
new RaLocationContactInformationChangedEvent( |
146
|
|
|
$this->institutionConfigurationId, |
147
|
|
|
$raLocationId, |
148
|
|
|
$contactInformation |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param RaLocationId $raLocationId |
156
|
|
|
*/ |
157
|
|
|
public function removeRaLocation(RaLocationId $raLocationId) |
158
|
|
|
{ |
159
|
|
View Code Duplication |
if (!$this->raLocations->containsWithId($raLocationId)) { |
|
|
|
|
160
|
|
|
throw new DomainException(sprintf( |
161
|
|
|
'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":' |
162
|
|
|
. ' it is not present', |
163
|
|
|
$raLocationId, |
164
|
|
|
$this->getAggregateRootId() |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getAggregateRootId() |
172
|
|
|
{ |
173
|
|
|
return $this->institutionConfigurationId; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
177
|
|
|
{ |
178
|
|
|
$this->institutionConfigurationId = $event->institutionConfigurationId; |
179
|
|
|
$this->institution = $event->institution; |
180
|
|
|
$this->raLocations = new RaLocationList([]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event) |
184
|
|
|
{ |
185
|
|
|
$this->raLocations->add( |
186
|
|
|
RaLocation::create( |
187
|
|
|
$event->raLocationId, |
188
|
|
|
$event->raLocationName, |
189
|
|
|
$event->location, |
190
|
|
|
$event->contactInformation |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event) |
196
|
|
|
{ |
197
|
|
|
$raLocation = $this->raLocations->getById($event->raLocationId); |
198
|
|
|
$raLocation->rename($event->raLocationName); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event) |
202
|
|
|
{ |
203
|
|
|
$raLocation = $this->raLocations->getById($event->raLocationId); |
204
|
|
|
$raLocation->relocate($event->location); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event) |
208
|
|
|
{ |
209
|
|
|
$raLocation = $this->raLocations->getById($event->raLocationId); |
210
|
|
|
$raLocation->changeContactInformation($event->contactInformation); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event) |
214
|
|
|
{ |
215
|
|
|
$this->raLocations->removeWithId($event->raLocationId); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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.