Completed
Pull Request — develop (#166)
by Daan van
22:27 queued 19:06
created

inferAddRaLocationCommands()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
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
class InstitutionConfigurationState
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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
        $raLocationInstitutions = array_map(function (RaLocation $raLocation) {
50
            return $raLocation->institution->getInstitution();
51
        }, $raLocations);
52
        $mappedRaLocations = array_combine($raLocationInstitutions, $raLocations);
53
54
        $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...
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)
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...
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