Completed
Pull Request — develop (#236)
by
unknown
11:02 queued 07:49
created

InstitutionAuthorizationOptionMap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

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