Completed
Push — feature/warn-on-non-utc-usage ( f71ca6...75a7ba )
by Boy
12:02 queued 07:38
created

InstitutionConfiguration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 8
Bugs 0 Features 3
Metric Value
wmc 18
c 8
b 0
f 3
lcom 1
cbo 13
dl 24
loc 174
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 1
A __construct() 0 3 1
B changeRaLocation() 8 37 5
A removeRaLocation() 8 13 2
A getAggregateRootId() 0 4 1
A addRaLocation() 8 23 2
A applyNewInstitutionConfigurationCreatedEvent() 0 6 1
A applyRaLocationAddedEvent() 0 11 1
A applyRaLocationRenamedEvent() 0 5 1
A applyRaLocationRelocatedEvent() 0 5 1
A applyRaLocationContactInformationChangedEvent() 0 5 1
A applyRaLocationRemovedEvent() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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...
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
    protected function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
176
    {
177
        $this->institutionConfigurationId = $event->institutionConfigurationId;
178
        $this->institution = $event->institution;
179
        $this->raLocations = new RaLocationList([]);
180
    }
181
182
    protected function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
183
    {
184
        $this->raLocations->add(
185
            RaLocation::create(
186
                $event->raLocationId,
187
                $event->raLocationName,
188
                $event->location,
189
                $event->contactInformation
190
            )
191
        );
192
    }
193
194
    protected function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
195
    {
196
        $raLocation = $this->raLocations->getById($event->raLocationId);
197
        $raLocation->rename($event->raLocationName);
198
    }
199
200
    protected function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
201
    {
202
        $raLocation = $this->raLocations->getById($event->raLocationId);
203
        $raLocation->relocate($event->location);
204
    }
205
206
    protected function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
207
    {
208
        $raLocation = $this->raLocations->getById($event->raLocationId);
209
        $raLocation->changeContactInformation($event->contactInformation);
210
    }
211
212
    protected function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
213
    {
214
        $this->raLocations->removeWithId($event->raLocationId);
215
    }
216
}
217