Completed
Pull Request — develop (#118)
by A.
09:31 queued 04:33
created

InstitutionConfiguration::removeRaLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 8
Ratio 61.54 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 8
loc 13
rs 9.4285
c 2
b 0
f 2
cc 2
eloc 8
nc 2
nop 1
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
0 ignored issues
show
Documentation introduced by
Should the return type not be InstitutionConfiguration|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
66
    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...
67
    {
68
    }
69
70
    public function addRaLocation(
71
        RaLocationId $raLocationId,
72
        RaLocationName $raLocationName,
73
        Location $location,
74
        ContactInformation $contactInformation
75
    ) {
76 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...
77
            throw new DomainException(sprintf(
78
                'Cannot add RaLocation with RaLocationId "%s" to RaLocations of InstitutionConfiguration "%s":'
79
                . 'it is already present',
80
                $raLocationId,
81
                $this->getAggregateRootId()
82
            ));
83
        }
84
85
        $this->apply(new RaLocationAddedEvent(
86
            $this->institutionConfigurationId,
87
            $raLocationId,
88
            $raLocationName,
89
            $location,
90
            $contactInformation
91
        ));
92
    }
93
94
    public function changeRaLocation(
95
        RaLocationId $raLocationId,
96
        RaLocationName $raLocationName,
97
        Location $location,
98
        ContactInformation $contactInformation
99
    ) {
100 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...
101
            throw new DomainException(sprintf(
102
                'Cannot change RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
103
                . 'it is not present',
104
                $raLocationId,
105
                $this->getAggregateRootId()
106
            ));
107
        }
108
109
        $raLocation = $this->raLocations->getById($raLocationId);
110
111
        if (!$raLocation->getLocationName()->equals($raLocationName)) {
112
            $this->apply(
113
                new RaLocationRenamedEvent($this->institutionConfigurationId, $raLocationId, $raLocationName)
114
            );
115
        }
116
        if (!$raLocation->getLocation()->equals($location)) {
117
            $this->apply(
118
                new RaLocationRelocatedEvent($this->institutionConfigurationId, $raLocationId, $location)
119
            );
120
        }
121
        if (!$raLocation->getContactInformation()->equals($contactInformation)) {
122
            $this->apply(
123
                new RaLocationContactInformationChangedEvent(
124
                    $this->institutionConfigurationId,
125
                    $raLocationId,
126
                    $contactInformation
127
                )
128
            );
129
        }
130
    }
131
132
    public function removeRaLocation(RaLocationId $raLocationId)
133
    {
134 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...
135
            throw new DomainException(sprintf(
136
                'Cannot remove RaLocation with RaLocationId "%s" in RaLocations of InstitutionConfiguration "%s":'
137
                . 'it is not present',
138
                $raLocationId,
139
                $this->getAggregateRootId()
140
            ));
141
        }
142
143
        $this->apply(new RaLocationRemovedEvent($this->institutionConfigurationId, $raLocationId));
144
    }
145
146
    public function getAggregateRootId()
147
    {
148
        return $this->institutionConfigurationId;
149
    }
150
151
    public function applyInstitutionConfigurationCreatedEvent()
152
    {
153
        // Implement
154
    }
155
156
    public function applyRaLocationAddedEvent()
157
    {
158
        // Implement
159
    }
160
161
    public function applyRaLocationRenamedEvent()
162
    {
163
        // Implement
164
    }
165
166
    public function applyRaLocationRelocatedEvent()
167
    {
168
        // Implement
169
    }
170
171
    public function applyRaLocationContactInformationChangedEvent()
172
    {
173
        // Implement
174
    }
175
176
    public function applyRaLocationRemovedEvent()
177
    {
178
        // Implement
179
    }
180
}
181