Completed
Pull Request — develop (#118)
by A.
11:33 queued 05:42
created

InstitutionConfiguration::changeRaLocation()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 24

Duplication

Lines 8
Ratio 21.62 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
dl 8
loc 37
rs 8.439
c 3
b 0
f 2
cc 5
eloc 24
nc 9
nop 4
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
 */
41
class InstitutionConfiguration extends EventSourcedAggregateRoot implements InstitutionConfigurationInterface
42
{
43
    /**
44
     * @var InstitutionConfigurationId
45
     */
46
    private $institutionConfigurationId;
47
48
    /**
49
     * @var RaLocationList
50
     */
51
    private $raLocations;
52
53
    /**
54
     * @param InstitutionConfigurationId $institutionConfigurationId
55
     * @param Institution $institution
56
     * @return InstitutionConfiguration
57
     */
58
    public static function create(InstitutionConfigurationId $institutionConfigurationId, Institution $institution)
59
    {
60
        $institutionConfiguration = new self;
61
        $institutionConfiguration->apply(
62
            new InstitutionConfigurationCreatedEvent($institutionConfigurationId, $institution)
63
        );
64
65
        return $institutionConfiguration;
66
    }
67
68
    final private function __construct()
0 ignored issues
show
introduced by
Instead of declaring the constructor as final, maybe you should declare the entire class as final.
Loading history...
69
    {
70
    }
71
72
    public function addRaLocation(
73
        RaLocationId $raLocationId,
74
        RaLocationName $raLocationName,
75
        Location $location,
76
        ContactInformation $contactInformation
77
    ) {
78 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...
79
            throw new DomainException(sprintf(
80
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
81
                . 'it is already present',
82
                $raLocationId,
83
                $this->getAggregateRootId()
84
            ));
85
        }
86
87
        $this->apply(new RaLocationAddedEvent(
88
            $this->institutionConfigurationId,
89
            $raLocationId,
90
            $raLocationName,
91
            $location,
92
            $contactInformation
93
        ));
94
    }
95
96
    public function changeRaLocation(
97
        RaLocationId $raLocationId,
98
        RaLocationName $raLocationName,
99
        Location $location,
100
        ContactInformation $contactInformation
101
    ) {
102 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...
103
            throw new DomainException(sprintf(
104
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
105
                . 'it is not present',
106
                $raLocationId,
107
                $this->getAggregateRootId()
108
            ));
109
        }
110
111
        $raLocation = $this->raLocations->getById($raLocationId);
112
113
        if (!$raLocation->getLocationName()->equals($raLocationName)) {
114
            $this->apply(
115
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
116
            );
117
        }
118
        if (!$raLocation->getLocation()->equals($location)) {
119
            $this->apply(
120
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
121
            );
122
        }
123
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
124
            $this->apply(
125
                new RaLocationContactInformationChangedEvent(
126
                    $this->institutionConfigurationId,
127
                    $raLocationId,
128
                    $contactInformation
129
                )
130
            );
131
        }
132
    }
133
134
    public function removeRaLocation(RaLocationId $raLocationId)
135
    {
136 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...
137
            throw new DomainException(sprintf(
138
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
139
                . 'it is not present',
140
                $raLocationId,
141
                $this->getAggregateRootId()
142
            ));
143
        }
144
145
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
146
    }
147
148
    public function getAggregateRootId()
149
    {
150
        return $this->institutionConfigurationId;
151
    }
152
153
    public function applyInstitutionConfigurationCreatedEvent()
154
    {
155
        // Implement
156
    }
157
158
    public function applyRaLocationAddedEvent()
159
    {
160
        // Implement
161
    }
162
163
    public function applyRaLocationRenamedEvent()
164
    {
165
        // Implement
166
    }
167
168
    public function applyRaLocationRelocatedEvent()
169
    {
170
        // Implement
171
    }
172
173
    public function applyRaLocationContactInformationChangedEvent()
174
    {
175
        // Implement
176
    }
177
178
    public function applyRaLocationRemovedEvent()
179
    {
180
        // Implement
181
    }
182
}
183