Completed
Push — feature/authz-service ( 8cb37e...7c943d )
by
unknown
02:28
created

InstitutionAuthorizationOptionMap::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
/**
3
 * Copyright 2018 SURFnet B.V.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace Surfnet\StepupMiddleware\ApiBundle\Configuration\Service;
19
20
use Surfnet\Stepup\Configuration\Value\Institution;
21
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption;
22
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
23
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
24
25
final class InstitutionAuthorizationOptionMap
26
{
27
    /**
28
     * @var InstitutionAuthorizationOption[]
29
     */
30
    private $institutionOptions;
31
32
    /**
33
     * InstitutionAuthorizationOptionMap constructor.
34
     * @param Institution $institution
35
     * @param InstitutionAuthorization[] $institutionAuthorizations
36
     */
37
    private function __construct(Institution $institution, array $institutionAuthorizations)
38
    {
39
        $institutions = [];
40
        $roles = [];
41
        foreach ($institutionAuthorizations as $authorization) {
42
            // Skip if the authorization is from a different institution
43
            if (!$authorization->institution->equals($institution)) {
44
                continue;
45
            }
46
47
            $role = $authorization->institutionRole;
48
            if (!isset($roles[$role->getType()])) {
49
                $roles[$role->getType()] = $role;
50
                $institutions[$role->getType()] = [];
51
            }
52
            $institutions[$role->getType()][] = $authorization->institutionRelation;
53
        }
54
55
        $this->institutionOptions = [];
56
        foreach ($roles as $role) {
57
            $institutionAuthorizationOption = InstitutionAuthorizationOption::fromInstitutions($role, $institution, $institutions[$role->getType()]);
58
            $this->institutionOptions[$role->getType()] = $institutionAuthorizationOption;
59
        }
60
    }
61
62
    /**
63
     * @param Institution $institution
64
     * @param InstitutionAuthorization[]|null
65
     * @return InstitutionAuthorizationOptionMap
66
     */
67
    public static function fromInstitutionAuthorizations(Institution $institution, array $institutionAuthorizations)
68
    {
69
        return new self($institution, $institutionAuthorizations);
70
    }
71
72
    /**
73
     * InstitutionAuthorizationOption
74
     * @param InstitutionRole $role
75
     * @return InstitutionAuthorizationOption
76
     */
77
    public function getAuthorizationOptionsByRole(InstitutionRole $role)
78
    {
79
        if (!isset($this->institutionOptions[$role->getType()])) {
80
            return InstitutionAuthorizationOption::getEmpty($role);
81
        }
82
83
        return $this->institutionOptions[$role->getType()];
84
    }
85
}
86