Completed
Push — feature/sf-subset/institution-... ( 7b1c92 )
by A.
04:11
created

loadInstitutionConfigurationFor()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 2
eloc 10
nc 3
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\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\AllowedSecondFactorList;
26
use Surfnet\Stepup\Configuration\Value\ContactInformation;
27
use Surfnet\Stepup\Configuration\Value\Institution;
28
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
29
use Surfnet\Stepup\Configuration\Value\Location;
30
use Surfnet\Stepup\Configuration\Value\RaLocationId;
31
use Surfnet\Stepup\Configuration\Value\RaLocationName;
32
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
33
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
34
use Surfnet\StepupBundle\Value\SecondFactorType;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\AddRaLocationCommand;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ChangeRaLocationCommand;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
38
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...
39
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...
40
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveRaLocationCommand;
41
42
/**
43
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects
44
 */
45
class InstitutionConfigurationCommandHandler extends CommandHandler
46
{
47
    /**
48
     * @var RepositoryInterface
49
     */
50
    private $repository;
51
52
    public function __construct(RepositoryInterface $repository)
53
    {
54
        $this->repository = $repository;
55
    }
56
57
    public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command)
58
    {
59
        $institution = new Institution($command->institution);
60
        $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
61
62
        try {
63
            /** @var InstitutionConfiguration $institutionConfiguration */
64
            $institutionConfiguration = $this->repository->load(
65
                $institutionConfigurationId->getInstitutionConfigurationId()
66
            );
67
68
            $institutionConfiguration->rebuild();
69
70
        } catch (AggregateNotFoundException $exception) {
71
            $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution);
72
        }
73
74
        $this->repository->save($institutionConfiguration);
75
    }
76
77
    public function handleReconfigureInstitutionConfigurationOptionsCommand(
78
        ReconfigureInstitutionConfigurationOptionsCommand $command
79
    ) {
80
        $institution = new Institution($command->institution);
81
82
        $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...
83
        $institutionConfiguration->configureUseRaLocationsOption(
84
            new UseRaLocationsOption($command->useRaLocationsOption)
85
        );
86
        $institutionConfiguration->configureShowRaaContactInformationOption(
87
            new ShowRaaContactInformationOption($command->showRaaContactInformationOption)
88
        );
89
90
        $allowedSecondFactors = array_map(function ($allowedSecondFactor) {
91
            return new SecondFactorType($allowedSecondFactor);
92
        }, $command->allowedSecondFactors);
93
94
        $institutionConfiguration->updateAllowedSecondFactorList(
95
            AllowedSecondFactorList::ofTypes($allowedSecondFactors)
96
        );
97
98
        $this->repository->save($institutionConfiguration);
99
    }
100
101 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...
102
    {
103
        $institution = new Institution($command->institution);
104
105
        $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...
106
        $institutionConfiguration->addRaLocation(
107
            new RaLocationId($command->raLocationId),
108
            new RaLocationName($command->raLocationName),
109
            new Location($command->location),
110
            new ContactInformation($command->contactInformation)
111
        );
112
113
        $this->repository->save($institutionConfiguration);
114
    }
115
116 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...
117
    {
118
        $institution = new Institution($command->institution);
119
120
        $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...
121
        $institutionConfiguration->changeRaLocation(
122
            new RaLocationId($command->raLocationId),
123
            new RaLocationName($command->raLocationName),
124
            new Location($command->location),
125
            new ContactInformation($command->contactInformation)
126
        );
127
128
        $this->repository->save($institutionConfiguration);
129
    }
130
131
    public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command)
132
    {
133
        $institution = new Institution($command->institution);
134
135
        $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...
136
        $institutionConfiguration->removeRaLocation(new RaLocationId($command->raLocationId));
137
138
        $this->repository->save($institutionConfiguration);
139
    }
140
141 View Code Duplication
    public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand(
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...
142
        RemoveInstitutionConfigurationByUnnormalizedIdCommand $command
143
    ) {
144
        $institution = new Institution($command->institution);
145
146
        $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...
147
        $institutionConfiguration = $this->repository->load(
148
            $institutionConfigurationId->getInstitutionConfigurationId()
149
        );
150
        $institutionConfiguration->destroy();
151
152
        $this->repository->save($institutionConfiguration);
153
    }
154
155
    /**
156
     * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids
157
     *
158
     * @param Institution $institution
159
     * @return InstitutionConfiguration
160
     */
161 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...
162
    {
163
        try {
164
            $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
165
            $institutionConfiguration = $this->repository->load(
166
                $institutionConfigurationId->getInstitutionConfigurationId()
167
            );
168
        } catch (AggregateNotFoundException $exception) {
169
            $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...
170
            $institutionConfiguration = $this->repository->load(
171
                $institutionConfigurationId->getInstitutionConfigurationId()
172
            );
173
        }
174
175
        return $institutionConfiguration;
176
    }
177
}
178