|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2017 SURFnet bv |
|
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\MiddlewareBundle\Migrations\InstitutionConfiguration; |
|
20
|
|
|
|
|
21
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\ConfiguredInstitution; |
|
22
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionConfigurationOptions; |
|
23
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation; |
|
24
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
final class InstitutionConfigurationState |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var MappedInstitutionConfiguration[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $mappedInstitutionConfigurations; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ConfiguredInstitution[] $configuredInstitutions |
|
35
|
|
|
* @param InstitutionConfigurationOptions[] $institutionConfigurationOptions |
|
36
|
|
|
* @param RaLocation[] $raLocations |
|
37
|
|
|
* @return InstitutionConfigurationState |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function load( |
|
40
|
|
|
$configuredInstitutions, |
|
41
|
|
|
$institutionConfigurationOptions, |
|
|
|
|
|
|
42
|
|
|
$raLocations |
|
43
|
|
|
) { |
|
44
|
|
|
$optionInstitutions = array_map(function (InstitutionConfigurationOptions $options) { |
|
45
|
|
|
return $options->institution->getInstitution(); |
|
46
|
|
|
}, $institutionConfigurationOptions); |
|
47
|
|
|
$mappedConfigurationOptions = array_combine($optionInstitutions, $institutionConfigurationOptions); |
|
48
|
|
|
|
|
49
|
|
|
$raLocationInstitutions = array_map(function (RaLocation $raLocation) { |
|
50
|
|
|
return $raLocation->institution->getInstitution(); |
|
51
|
|
|
}, $raLocations); |
|
52
|
|
|
$mappedRaLocations = array_combine($raLocationInstitutions, $raLocations); |
|
53
|
|
|
|
|
54
|
|
|
$mappedInstitutionConfigurations = []; |
|
|
|
|
|
|
55
|
|
|
foreach ($configuredInstitutions as $institution) { |
|
56
|
|
|
$institutionName = $institution->institution->getInstitution(); |
|
57
|
|
|
if (!array_key_exists($institutionName, $mappedConfigurationOptions)) { |
|
58
|
|
|
throw new RuntimeException(sprintf( |
|
59
|
|
|
'Institution "%s" has been configured, but does not have options.', |
|
60
|
|
|
$institutionName |
|
61
|
|
|
)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var InstitutionConfigurationOptions $options */ |
|
65
|
|
|
$options = $mappedConfigurationOptions[$institutionName]; |
|
66
|
|
|
$locations = isset($mappedRaLocations[$institutionName]) ? $mappedRaLocations[$institutionName] : []; |
|
67
|
|
|
|
|
68
|
|
|
$mappedInstitutionConfigurations[] = new MappedInstitutionConfiguration( |
|
69
|
|
|
$institution->institution, |
|
70
|
|
|
$options->useRaLocationsOption, |
|
71
|
|
|
$options->showRaaContactInformationOption, |
|
72
|
|
|
$locations |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new self($mappedInstitutionConfigurations); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param MappedInstitutionConfiguration[] $mappedInstitutionConfigurations |
|
81
|
|
|
*/ |
|
82
|
|
|
private function __construct(array $mappedInstitutionConfigurations) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->mappedInstitutionConfigurations = $mappedInstitutionConfigurations; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \Generator |
|
89
|
|
|
*/ |
|
90
|
|
|
public function inferRemovalCommands() |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) { |
|
93
|
|
|
yield $mappedInstitutionConfiguration->inferRemoveInstitutionConfigurationByIdCommand(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return \Generator |
|
99
|
|
|
*/ |
|
100
|
|
|
public function inferCreateCommands() |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) { |
|
103
|
|
|
yield $mappedInstitutionConfiguration->inferCreateInstitutionConfigurationCommand(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \Generator |
|
109
|
|
|
*/ |
|
110
|
|
|
public function inferReconfigureCommands() |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) { |
|
113
|
|
|
yield $mappedInstitutionConfiguration->inferReconfigureInstitutionConfigurationCommand(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return \Generator |
|
119
|
|
|
*/ |
|
120
|
|
|
public function inferAddRaLocationCommands() |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) { |
|
123
|
|
|
foreach ($mappedInstitutionConfiguration->inferAddRaLocationCommands() as $command) { |
|
124
|
|
|
yield $command; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.