handleReconfigureInstitutionConfigurationOptionsCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 37
nc 1
nop 1
dl 0
loc 65
rs 9.328
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\CommandHandler;
20
21
use Broadway\CommandHandling\SimpleCommandHandler;
22
use Broadway\Repository\AggregateNotFoundException;
23
use Broadway\Repository\Repository as 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\InstitutionAuthorizationOption;
29
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
30
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
31
use Surfnet\Stepup\Configuration\Value\Location;
32
use Surfnet\Stepup\Configuration\Value\NumberOfTokensPerIdentityOption;
33
use Surfnet\Stepup\Configuration\Value\RaLocationId;
34
use Surfnet\Stepup\Configuration\Value\RaLocationName;
35
use Surfnet\Stepup\Configuration\Value\SelfAssertedTokensOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configura...elfAssertedTokensOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Surfnet\Stepup\Configuration\Value\SelfVetOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configuration\Value\SelfVetOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
38
use Surfnet\Stepup\Configuration\Value\SsoOn2faOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configuration\Value\SsoOn2faOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
use Surfnet\Stepup\Configuration\Value\SsoRegistrationBypassOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configura...egistrationBypassOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
41
use Surfnet\Stepup\Configuration\Value\VerifyEmailOption;
42
use Surfnet\StepupBundle\Value\SecondFactorType;
43
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\AddRaLocationCommand;
44
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ChangeRaLocationCommand;
45
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
46
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ReconfigureInstitutionConfigurationOptionsCommand;
47
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveInstitutionConfigurationByUnnormalizedIdCommand;
48
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveRaLocationCommand;
49
50
/**
51
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects
52
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
53
class InstitutionConfigurationCommandHandler extends SimpleCommandHandler
54
{
55
    public function __construct(private readonly RepositoryInterface $repository)
56
    {
57
    }
58
59
    public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command): void
60
    {
61
        $institution = new Institution($command->institution);
62
        $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
63
64
        try {
65
            /** @var InstitutionConfiguration $institutionConfiguration */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
66
            $institutionConfiguration = $this->repository->load(
67
                $institutionConfigurationId->getInstitutionConfigurationId(),
68
            );
69
70
            $institutionConfiguration->rebuild();
71
        } catch (AggregateNotFoundException) {
72
            $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution);
73
        }
74
75
        $this->repository->save($institutionConfiguration);
76
    }
77
78
    public function handleReconfigureInstitutionConfigurationOptionsCommand(
79
        ReconfigureInstitutionConfigurationOptionsCommand $command,
80
    ): void {
81
        $institution = new Institution($command->institution);
82
83
        $allowedSecondFactors = array_map(
84
            fn($allowedSecondFactor): SecondFactorType => new SecondFactorType($allowedSecondFactor),
85
            $command->allowedSecondFactors,
86
        );
87
88
89
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated: Should be used until existing institution configurations have been migrated to using normalized ids ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor($institution);

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

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

Loading history...
90
        $institutionConfiguration->configureUseRaLocationsOption(
91
            new UseRaLocationsOption($command->useRaLocationsOption),
92
        );
93
        $institutionConfiguration->configureVerifyEmailOption(
94
            new VerifyEmailOption($command->verifyEmailOption),
95
        );
96
        $institutionConfiguration->configureNumberOfTokensPerIdentityOption(
97
            new NumberOfTokensPerIdentityOption($command->numberOfTokensPerIdentityOption),
98
        );
99
        $institutionConfiguration->configureShowRaaContactInformationOption(
100
            new ShowRaaContactInformationOption($command->showRaaContactInformationOption),
101
        );
102
103
        // Configure the authorization options on the aggregate
104
        $institutionConfiguration->updateUseRaOption(
105
            InstitutionAuthorizationOption::fromInstitutionConfig(
106
                InstitutionRole::useRa(),
107
                $command->useRaOption,
108
            ),
109
        );
110
        $institutionConfiguration->updateUseRaaOption(
111
            InstitutionAuthorizationOption::fromInstitutionConfig(
112
                InstitutionRole::useRaa(),
113
                $command->useRaaOption,
114
            ),
115
        );
116
        $institutionConfiguration->updateSelectRaaOption(
117
            InstitutionAuthorizationOption::fromInstitutionConfig(
118
                InstitutionRole::selectRaa(),
119
                $command->selectRaaOption,
120
            ),
121
        );
122
123
        $institutionConfiguration->updateAllowedSecondFactorList(
124
            AllowedSecondFactorList::ofTypes($allowedSecondFactors),
125
        );
126
127
        // Handle optional options
128
        $selfVetOptionValue = $command->selfVetOption ?? SelfVetOption::getDefault()->isEnabled();
129
        $institutionConfiguration->configureSelfVetOption(new SelfVetOption($selfVetOptionValue));
130
131
        $ssoOn2faOptionValue = $command->ssoOn2faOption ?? SsoOn2faOption::getDefault()->isEnabled();
132
        $institutionConfiguration->configureSsoOn2faOption(new SsoOn2faOption($ssoOn2faOptionValue));
133
134
        $ssoRegistrationBypassOptionValue = $command->ssoRegistrationBypassOption ?? SsoRegistrationBypassOption::getDefault()->isEnabled();
135
        $institutionConfiguration->configureSsoRegistrationBypassOption(new SsoRegistrationBypassOption($ssoRegistrationBypassOptionValue));
136
137
        $satOption = $command->selfAssertedTokensOption ?? SelfAssertedTokensOption::getDefault()->isEnabled();
138
        $institutionConfiguration->configureSelfAssertedTokensOption(
139
            new SelfAssertedTokensOption($satOption),
140
        );
141
142
        $this->repository->save($institutionConfiguration);
143
    }
144
145
    public function handleAddRaLocationCommand(AddRaLocationCommand $command): void
146
    {
147
        $institution = new Institution($command->institution);
148
149
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated: Should be used until existing institution configurations have been migrated to using normalized ids ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

149
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor($institution);

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

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

Loading history...
150
        $institutionConfiguration->addRaLocation(
151
            new RaLocationId($command->raLocationId),
152
            new RaLocationName($command->raLocationName),
153
            new Location($command->location),
154
            new ContactInformation($command->contactInformation),
155
        );
156
157
        $this->repository->save($institutionConfiguration);
158
    }
159
160
    public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command): void
161
    {
162
        $institution = new Institution($command->institution);
163
164
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated: Should be used until existing institution configurations have been migrated to using normalized ids ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

164
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor($institution);

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

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

Loading history...
165
        $institutionConfiguration->changeRaLocation(
166
            new RaLocationId($command->raLocationId),
167
            new RaLocationName($command->raLocationName),
168
            new Location($command->location),
169
            new ContactInformation($command->contactInformation),
170
        );
171
172
        $this->repository->save($institutionConfiguration);
173
    }
174
175
    public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command): void
176
    {
177
        $institution = new Institution($command->institution);
178
179
        $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated: Should be used until existing institution configurations have been migrated to using normalized ids ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

179
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor($institution);

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

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

Loading history...
180
        $institutionConfiguration->removeRaLocation(new RaLocationId($command->raLocationId));
181
182
        $this->repository->save($institutionConfiguration);
183
    }
184
185
    public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand(
186
        RemoveInstitutionConfigurationByUnnormalizedIdCommand $command,
187
    ): void {
188
        $institution = new Institution($command->institution);
189
190
        $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
191
        /** @var InstitutionConfiguration $institutionConfiguration */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
192
        $institutionConfiguration = $this->repository->load(
193
            $institutionConfigurationId->getInstitutionConfigurationId(),
194
        );
195
        $institutionConfiguration->destroy();
196
197
        $this->repository->save($institutionConfiguration);
198
    }
199
200
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $institution should have a doc-comment as per coding-style.
Loading history...
201
     * @return InstitutionConfiguration
0 ignored issues
show
Coding Style introduced by
Tag value for @return tag indented incorrectly; expected 5 spaces but found 1
Loading history...
202
     * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids
203
     *
204
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
205
    private function loadInstitutionConfigurationFor(Institution $institution): InstitutionConfiguration
0 ignored issues
show
Coding Style introduced by
Private method name "InstitutionConfigurationCommandHandler::loadInstitutionConfigurationFor" must be prefixed with an underscore
Loading history...
206
    {
207
        try {
208
            $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
209
            /** @var InstitutionConfiguration $institutionConfiguration */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
210
            $institutionConfiguration = $this->repository->load(
211
                $institutionConfigurationId->getInstitutionConfigurationId(),
212
            );
213
        } catch (AggregateNotFoundException) {
214
            $institutionConfigurationId = InstitutionConfigurationId::from($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\Stepup\Configura...ConfigurationId::from() has been deprecated: To be removed in next release; use normalizedFrom method to account for case-(in)sensitivity issues ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

214
            $institutionConfigurationId = /** @scrutinizer ignore-deprecated */ InstitutionConfigurationId::from($institution);

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

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

Loading history...
215
            /** @var InstitutionConfiguration $institutionConfiguration */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
216
            $institutionConfiguration = $this->repository->load(
217
                $institutionConfigurationId->getInstitutionConfigurationId(),
218
            );
219
        }
220
221
        return $institutionConfiguration;
222
    }
223
}
224