Completed
Push — feature/authz-service ( 2625fb...8f3145 )
by
unknown
02:12
created

InstitutionAuthorizationOptionMap   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A fromInstitutionAuthorizations() 0 4 1
A getInstitutionAuthorizationOptionsByRole() 0 8 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\Stepup\Configuration\Value;
19
20
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
21
22
final class InstitutionAuthorizationOptionMap
23
{
24
    /**
25
     * @var InstitutionAuthorizationOption[]
26
     */
27
    private $institutionOptions;
28
29
    /**
30
     * InstitutionAuthorizationOptionMap constructor.
31
     * @param Institution $institution
32
     * @param InstitutionAuthorization[] $institutionAuthorizations
33
     */
34
    private function __construct(Institution $institution, array $institutionAuthorizations)
35
    {
36
        $institutions = [];
37
        $roles = [];
38
        foreach ($institutionAuthorizations as $authorization) {
39
            $role = $authorization->institutionRole;
40
            if (!isset($roles[$role->getType()])) {
41
                $roles[$role->getType()] = $role;
42
                $institutions[$role->getType()] = [];
43
            }
44
            $institutions[$role->getType()][] = $authorization->institutionRelation;
45
        }
46
47
        $this->institutionOptions = [];
48
        foreach ($roles as $role) {
49
            $institutionAuthorizationOption = InstitutionAuthorizationOption::fromInstitutions($role, $institution, $institutions[$role->getType()]);
50
            $this->institutionOptions[$role->getType()] = $institutionAuthorizationOption;
51
        }
52
    }
53
54
    /**
55
     * @param Institution $institution
56
     * @param InstitutionAuthorization[]|null
57
     * @return InstitutionAuthorizationOptionMap
58
     */
59
    public static function fromInstitutionAuthorizations(Institution $institution, array $institutionAuthorizations)
60
    {
61
        return new self($institution, $institutionAuthorizations);
62
    }
63
64
    /**
65
     * InstitutionAuthorizationOption
66
     * @param InstitutionRole $role
67
     * @return InstitutionAuthorizationOption
68
     */
69
    public function getInstitutionAuthorizationOptionsByRole(InstitutionRole $role)
70
    {
71
        if (!isset($this->institutionOptions[$role->getType()])) {
72
            return InstitutionAuthorizationOption::getEmpty($role);
73
        }
74
75
        return $this->institutionOptions[$role->getType()];
76
    }
77
}
78