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