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; |
|
|
|
|
39
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveInstitutionConfigurationByUnnormalizedIdCommand; |
|
|
|
|
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); |
|
|
|
|
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) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$institution = new Institution($command->institution); |
104
|
|
|
|
105
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
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) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$institution = new Institution($command->institution); |
119
|
|
|
|
120
|
|
|
$institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
|
|
|
|
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); |
|
|
|
|
136
|
|
|
$institutionConfiguration->removeRaLocation(new RaLocationId($command->raLocationId)); |
137
|
|
|
|
138
|
|
|
$this->repository->save($institutionConfiguration); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand( |
|
|
|
|
142
|
|
|
RemoveInstitutionConfigurationByUnnormalizedIdCommand $command |
143
|
|
|
) { |
144
|
|
|
$institution = new Institution($command->institution); |
145
|
|
|
|
146
|
|
|
$institutionConfigurationId = InstitutionConfigurationId::from($institution); |
|
|
|
|
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) |
|
|
|
|
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); |
|
|
|
|
170
|
|
|
$institutionConfiguration = $this->repository->load( |
171
|
|
|
$institutionConfigurationId->getInstitutionConfigurationId() |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $institutionConfiguration; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.