Completed
Push — release-2.x ( 370844...c278f6 )
by A.
05:18
created

InstitutionConfigurationState::load()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 5
eloc 27
nc 8
nop 3
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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
        $mappedRaLocations = [];
50
        foreach ($raLocations as $raLocation) {
51
            $institution = $raLocation->institution->getInstitution();
52
            $mappedRaLocations[$institution][] = $raLocation;
53
        }
54
55
        $mappedInstitutionConfigurations = [];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $mappedInstitutionConfigurations exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
        foreach ($configuredInstitutions as $institution) {
57
            $institutionName = $institution->institution->getInstitution();
58
            if (!array_key_exists($institutionName, $mappedConfigurationOptions)) {
59
                throw new RuntimeException(sprintf(
60
                    'Institution "%s" has been configured, but does not have options.',
61
                    $institutionName
62
                ));
63
            }
64
65
            /** @var InstitutionConfigurationOptions $options */
66
            $options = $mappedConfigurationOptions[$institutionName];
67
            $locations = isset($mappedRaLocations[$institutionName]) ? $mappedRaLocations[$institutionName] : [];
68
69
            $mappedInstitutionConfigurations[] = new MappedInstitutionConfiguration(
70
                $institution->institution,
71
                $options->useRaLocationsOption,
72
                $options->showRaaContactInformationOption,
73
                $locations
74
            );
75
        }
76
77
        return new self($mappedInstitutionConfigurations);
78
    }
79
80
    /**
81
     * @param MappedInstitutionConfiguration[] $mappedInstitutionConfigurations
82
     */
83
    private function __construct(array $mappedInstitutionConfigurations)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $mappedInstitutionConfigurations exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
84
    {
85
        $this->mappedInstitutionConfigurations = $mappedInstitutionConfigurations;
86
    }
87
88
    /**
89
     * @return \Generator
90
     */
91
    public function inferRemovalCommands()
92
    {
93
        foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) {
94
            yield $mappedInstitutionConfiguration->inferRemoveInstitutionConfigurationByIdCommand();
95
        }
96
    }
97
98
    /**
99
     * @return \Generator
100
     */
101
    public function inferCreateCommands()
102
    {
103
        foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) {
104
            yield $mappedInstitutionConfiguration->inferCreateInstitutionConfigurationCommand();
105
        }
106
    }
107
108
    /**
109
     * @return \Generator
110
     */
111
    public function inferReconfigureCommands()
112
    {
113
        foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) {
114
            yield $mappedInstitutionConfiguration->inferReconfigureInstitutionConfigurationCommand();
115
        }
116
    }
117
118
    /**
119
     * @return \Generator
120
     */
121
    public function inferAddRaLocationCommands()
122
    {
123
        foreach ($this->mappedInstitutionConfigurations as $mappedInstitutionConfiguration) {
124
            foreach ($mappedInstitutionConfiguration->inferAddRaLocationCommands() as $command) {
125
                yield $command;
126
            }
127
        }
128
    }
129
}
130