Completed
Push — feature/authz-service ( 32c4b7...df9aff )
by
unknown
03:22
created

InstitutionAuthorizationOption::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Stepup\Configuration\Value;
19
20
use Surfnet\Stepup\Exception\InvalidArgumentException;
21
22
final class InstitutionAuthorizationOption
23
{
24
    /**
25
     * @var InstitutionRole
26
     */
27
    private $institutionRole;
28
29
    /**
30
     * @var InstitutionSet
31
     */
32
    private $institutionSet;
33
34
    /**
35
     * @var boolean
36
     */
37
    private $isDefault;
38
39
    /**
40
     * AbstractRoleOption constructor.
41
     * @param InstitutionRole $role
42
     * @param InstitutionSet $institutionSet
43
     * @param bool $isDefault
44
     */
45
    private function __construct(InstitutionRole $role, InstitutionSet $institutionSet, $isDefault)
46
    {
47
        $this->institutionRole = $role;
48
        $this->institutionSet = $institutionSet;
49
        $this->isDefault = (bool)$isDefault;
50
    }
51
52
    /**
53
     * @param InstitutionRole $role
54
     * @param Institution $institution
55
     * @param string[]|null
56
     * @return InstitutionAuthorizationOption
57
     */
58
    public static function fromInstitutionConfig(InstitutionRole $role, Institution $institution, $institutions = null)
59
    {
60
        if (is_null($institutions)) {
61
            return self::getDefault($role, $institution);
62
        }
63
64
        if (!is_array($institutions)) {
65
            throw InvalidArgumentException::invalidType(
66
                'array',
67
                'institutions',
68
                $institutions
69
            );
70
        }
71
72
        if (count($institutions) === 1 && current($institutions) === $institution->getInstitution()) {
73
            return self::getDefault($role, $institution);
74
        }
75
76
        array_walk(
77
            $institutions,
78
            function ($institution, $key) use ($institutions) {
79
                if (!is_string($institution)  || strlen(trim($institution)) === 0) {
80
                    throw InvalidArgumentException::invalidType(
81
                        'string',
82
                        'institutions',
83
                        $institutions[$key]
84
                    );
85
                }
86
            }
87
        );
88
89
        $set = [];
90
        foreach ($institutions as $institutionTitle) {
91
            $set[] = new Institution($institutionTitle);
92
        }
93
94
        $institutionSet = InstitutionSet::create($set);
95
96
        return new self($role, $institutionSet, false);
97
    }
98
99
    /**
100
     * @param InstitutionRole $role
101
     * @param Institution[] $institutions
102
     * @return InstitutionAuthorizationOption
103
     */
104
    public static function fromInstitutions(InstitutionRole $role, array $institutions)
105
    {
106
        return new self($role, InstitutionSet::create($institutions), false);
107
    }
108
109
    /**
110
     * @param InstitutionRole $role
111
     * @param Institution $institution
112
     * @param string[]|null
113
     * @return InstitutionAuthorizationOption
114
     */
115
    public static function getDefault(InstitutionRole $role, Institution $institution)
116
    {
117
        return new self($role, InstitutionSet::create([$institution]), true);
118
    }
119
120
    /**
121
     * @return null
122
     */
123
    public static function blank()
124
    {
125
        return null;
126
    }
127
128
    /**
129
     * @param InstitutionAuthorizationOption $option
130
     * @return bool
131
     */
132
    public function equals(InstitutionAuthorizationOption $option)
133
    {
134
        return
135
            $this->institutionRole->equals($option->getInstitutionRole()) &&
136
            $this->institutionSet->equals($option->getInstitutionSet()) &&
137
            $this->isDefault === $option->isDefault();
138
    }
139
140
    /**
141
     * @return InstitutionRole
142
     */
143
    public function getInstitutionRole()
144
    {
145
        return $this->institutionRole;
146
    }
147
148
    /**
149
     * @return InstitutionSet
150
     */
151
    public function getInstitutionSet()
152
    {
153
        return $this->institutionSet;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function isDefault()
160
    {
161
        return $this->isDefault;
162
    }
163
}
164