Completed
Push — develop ( ab63ec...7fd1ca )
by A.
10s
created

InstitutionConfigurationCommandHandler   B

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 50.4 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 17
dl 63
loc 125
rs 7.8571

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleReconfigureInstitutionConfigurationOptionsCommand() 0 15 1
A handleAddRaLocationCommand() 14 14 1
A handleChangeRaLocationCommand() 14 14 1
A handleRemoveRaLocationCommand() 0 9 1
A loadInstitutionConfigurationFor() 16 16 2
A handleCreateInstitutionConfigurationCommand() 19 19 2
A handleRemoveInstitutionConfigurationByUnnormalizedIdCommand() 0 13 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\StepupMiddleware\CommandHandlingBundle\Configuration\CommandHandler;
20
21
use Broadway\CommandHandling\CommandHandler;
22
use Broadway\Repository\AggregateNotFoundException;
23
use Broadway\Repository\RepositoryInterface;
24
use Surfnet\Stepup\Configuration\InstitutionConfiguration;
25
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
26
use Surfnet\Stepup\Configuration\Value\Institution;
27
use Surfnet\Stepup\Configuration\Value\Location;
28
use Surfnet\Stepup\Configuration\Value\RaLocationId;
29
use Surfnet\Stepup\Configuration\Value\RaLocationName;
30
use Surfnet\Stepup\Configuration\Value\ContactInformation;
31
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
32
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\AddRaLocationCommand;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ChangeRaLocationCommand;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveInstitutionConfigurationByUnnormalizedIdCommand;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
38
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveRaLocationCommand;
39
40
/**
41
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects
42
 */
43
class InstitutionConfigurationCommandHandler extends CommandHandler
44
{
45
    /**
46
     * @var RepositoryInterface
47
     */
48
    private $repository;
49
50
    public function __construct(RepositoryInterface $repository)
51
    {
52
        $this->repository = $repository;
53
    }
54
55 View Code Duplication
    public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
56
    {
57
        $institution = new Institution($command->institution);
58
        $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
59
60
        try {
61
            /** @var InstitutionConfiguration $institutionConfiguration */
62
            $institutionConfiguration = $this->repository->load(
63
                $institutionConfigurationId->getInstitutionConfigurationId()
64
            );
65
66
            $institutionConfiguration->rebuild();
67
68
        } catch (AggregateNotFoundException $exception) {
69
            $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution);
70
        }
71
72
        $this->repository->save($institutionConfiguration);
73
    }
74
75
    public function handleReconfigureInstitutionConfigurationOptionsCommand(
76
        ReconfigureInstitutionConfigurationOptionsCommand $command
77
    ) {
78
        $institution = new Institution($command->institution);
79
80
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
        $institutionConfiguration->configureUseRaLocationsOption(
82
            new UseRaLocationsOption($command->useRaLocationsOption)
83
        );
84
        $institutionConfiguration->configureShowRaaContactInformationOption(
85
            new ShowRaaContactInformationOption($command->showRaaContactInformationOption)
86
        );
87
88
        $this->repository->save($institutionConfiguration);
89
    }
90
91 View Code Duplication
    public function handleAddRaLocationCommand(AddRaLocationCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
93
        $institution = new Institution($command->institution);
94
95
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
        $institutionConfiguration->addRaLocation(
97
            new RaLocationId($command->raLocationId),
98
            new RaLocationName($command->raLocationName),
99
            new Location($command->location),
100
            new ContactInformation($command->contactInformation)
101
        );
102
103
        $this->repository->save($institutionConfiguration);
104
    }
105
106 View Code Duplication
    public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108
        $institution = new Institution($command->institution);
109
110
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
111
        $institutionConfiguration->changeRaLocation(
112
            new RaLocationId($command->raLocationId),
113
            new RaLocationName($command->raLocationName),
114
            new Location($command->location),
115
            new ContactInformation($command->contactInformation)
116
        );
117
118
        $this->repository->save($institutionConfiguration);
119
    }
120
121
    public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command)
122
    {
123
        $institution = new Institution($command->institution);
124
125
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
126
        $institutionConfiguration->removeRaLocation(new RaLocationId($command->raLocationId));
127
128
        $this->repository->save($institutionConfiguration);
129
    }
130
131
    public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand(
132
        RemoveInstitutionConfigurationByUnnormalizedIdCommand $command
133
    ) {
134
        $institution = new Institution($command->institution);
135
136
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\Stepup\Configura...ConfigurationId::from() has been deprecated with message: To be removed in next release; use normalizedFrom method to account for case-(in)sensitivity issues

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
        $institutionConfiguration = $this->repository->load(
138
            $institutionConfigurationId->getInstitutionConfigurationId()
139
        );
140
        $institutionConfiguration->destroy();
141
142
        $this->repository->save($institutionConfiguration);
143
    }
144
145
    /**
146
     * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids
147
     *
148
     * @param Institution $institution
149
     * @return InstitutionConfiguration
150
     */
151 View Code Duplication
    private function loadInstitutionConfigurationFor(Institution $institution)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
152
    {
153
        try {
154
            $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
155
            $institutionConfiguration = $this->repository->load(
156
                $institutionConfigurationId->getInstitutionConfigurationId()
157
            );
158
        } catch (AggregateNotFoundException $exception) {
159
            $institutionConfigurationId = InstitutionConfigurationId::from($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\Stepup\Configura...ConfigurationId::from() has been deprecated with message: To be removed in next release; use normalizedFrom method to account for case-(in)sensitivity issues

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
160
            $institutionConfiguration = $this->repository->load(
161
                $institutionConfigurationId->getInstitutionConfigurationId()
162
            );
163
        }
164
165
        return $institutionConfiguration;
166
    }
167
}
168