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

fromInstitutionConfig()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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