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