Completed
Pull Request — develop (#236)
by
unknown
05:12 queued 02:32
created

InstitutionAuthorizationOptionMap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 24 5
A fromInstitutionAuthorizations() 0 4 1
A getAuthorizationOptionsByRole() 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
            // Skip if the authorization is from a different institution
40
            if (!$authorization->institution->equals($institution)) {
41
                continue;
42
            }
43
44
            $role = $authorization->institutionRole;
45
            if (!isset($roles[$role->getType()])) {
46
                $roles[$role->getType()] = $role;
47
                $institutions[$role->getType()] = [];
48
            }
49
            $institutions[$role->getType()][] = $authorization->institutionRelation;
50
        }
51
52
        $this->institutionOptions = [];
53
        foreach ($roles as $role) {
54
            $institutionAuthorizationOption = InstitutionAuthorizationOption::fromInstitutions($role, $institution, $institutions[$role->getType()]);
55
            $this->institutionOptions[$role->getType()] = $institutionAuthorizationOption;
56
        }
57
    }
58
59
    /**
60
     * @param Institution $institution
61
     * @param InstitutionAuthorization[]|null
62
     * @return InstitutionAuthorizationOptionMap
63
     */
64
    public static function fromInstitutionAuthorizations(Institution $institution, array $institutionAuthorizations)
65
    {
66
        return new self($institution, $institutionAuthorizations);
67
    }
68
69
    /**
70
     * InstitutionAuthorizationOption
71
     * @param InstitutionRole $role
72
     * @return InstitutionAuthorizationOption
73
     */
74
    public function getAuthorizationOptionsByRole(InstitutionRole $role)
75
    {
76
        if (!isset($this->institutionOptions[$role->getType()])) {
77
            return InstitutionAuthorizationOption::getEmpty($role);
78
        }
79
80
        return $this->institutionOptions[$role->getType()];
81
    }
82
}
83