Completed
Push — feature/ra-locations-model ( e718cb...886306 )
by A.
09:31 queued 05:17
created

getInstitutionConfigurationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        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
            $raLocationId,
103
            $raLocationName,
104
            $location,
105
            $contactInformation
106
        ));
107
    }
108
109
    /**
110
     * @param RaLocationId $raLocationId
111
     * @param RaLocationName $raLocationName
112
     * @param Location $location
113
     * @param ContactInformation $contactInformation
114
     */
115
    public function changeRaLocation(
116
        RaLocationId $raLocationId,
117
        RaLocationName $raLocationName,
118
        Location $location,
119
        ContactInformation $contactInformation
120
    ) {
121 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...
122
            throw new DomainException(sprintf(
123
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
124
                . 'it is not present',
125
                $raLocationId,
126
                $this->getAggregateRootId()
127
            ));
128
        }
129
130
        $raLocation = $this->raLocations->getById($raLocationId);
131
132
        if (!$raLocation->getRaLocationName()->equals($raLocationName)) {
133
            $this->apply(
134
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
135
            );
136
        }
137
        if (!$raLocation->getLocation()->equals($location)) {
138
            $this->apply(
139
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
140
            );
141
        }
142
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
143
            $this->apply(
144
                new RaLocationContactInformationChangedEvent(
145
                    $this->institutionConfigurationId,
146
                    $raLocationId,
147
                    $contactInformation
148
                )
149
            );
150
        }
151
    }
152
153
    /**
154
     * @param RaLocationId $raLocationId
155
     */
156
    public function removeRaLocation(RaLocationId $raLocationId)
157
    {
158 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...
159
            throw new DomainException(sprintf(
160
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
161
                . 'it is not present',
162
                $raLocationId,
163
                $this->getAggregateRootId()
164
            ));
165
        }
166
167
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
168
    }
169
170
    public function getAggregateRootId()
171
    {
172
        return $this->institutionConfigurationId;
173
    }
174
175
    /**
176
     * @return InstitutionConfigurationId
177
     */
178
    public function getInstitutionConfigurationId()
179
    {
180
        return $this->institutionConfigurationId;
181
    }
182
183
    /**
184
     * @return Institution
185
     */
186
    public function getInstitution()
187
    {
188
        return $this->institution;
189
    }
190
191
    /**
192
     * @return RaLocationList
193
     */
194
    public function getRaLocations()
195
    {
196
        return $this->raLocations;
197
    }
198
199
    public function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
200
    {
201
        $this->institutionConfigurationId = $event->institutionConfigurationId;
202
        $this->institution = $event->institution;
203
        $this->raLocations = new RaLocationList([]);
204
    }
205
206
    public function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
207
    {
208
        $this->raLocations->add(
209
            RaLocation::create(
210
                $event->raLocationId,
211
                $event->raLocationName,
212
                $event->location,
213
                $event->contactInformation
214
            )
215
        );
216
    }
217
218
    public function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
219
    {
220
        $raLocation = $this->raLocations->getById($event->raLocationId);
221
        $raLocation->rename($event->raLocationName);
222
    }
223
224
    public function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
225
    {
226
        $raLocation = $this->raLocations->getById($event->raLocationId);
227
        $raLocation->relocate($event->location);
228
    }
229
230
    public function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
231
    {
232
        $raLocation = $this->raLocations->getById($event->raLocationId);
233
        $raLocation->changeContactInformation($event->contactInformation);
234
    }
235
236
    public function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
237
    {
238
        $this->raLocations->removeWithId($event->raLocationId);
239
    }
240
}
241