Completed
Push — feature/authz-service ( 007e40...3c7a6c )
by
unknown
02:53
created

InstitutionOption::getInstitutionRole()   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 0
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 InstitutionOption
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 InstitutionOption
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
        return new self($role, InstitutionSet::createFromStringArray($institutions), false);
77
    }
78
79
    /**
80
     * @param InstitutionRole $role
81
     * @param Institution[] $institutions
82
     * @return InstitutionOption
83
     */
84
    public static function fromInstitutions(InstitutionRole $role, array $institutions)
85
    {
86
        return new self($role, InstitutionSet::create($institutions), false);
87
    }
88
89
    /**
90
     * @param InstitutionRole $role
91
     * @param Institution $institution
92
     * @param string[]|null
93
     * @return InstitutionOption
94
     */
95
    public static function getDefault(InstitutionRole $role, Institution $institution)
96
    {
97
        return new self($role, InstitutionSet::create([$institution]), true);
98
    }
99
100
    /**
101
     * @return null
102
     */
103
    public static function blank()
104
    {
105
        return null;
106
    }
107
108
    /**
109
     * @param InstitutionOption $option
110
     * @return bool
111
     */
112
    public function equals(InstitutionOption $option)
113
    {
114
        return
115
            $this->institutionRole->equals($option->getInstitutionRole()) &&
116
            $this->institutionSet->equals($option->getInstitutionSet()) &&
117
            $this->isDefault === $option->isDefault();
118
    }
119
120
    /**
121
     * @return InstitutionRole
122
     */
123
    public function getInstitutionRole()
124
    {
125
        return $this->institutionRole;
126
    }
127
128
    /**
129
     * @return InstitutionSet
130
     */
131
    public function getInstitutionSet()
132
    {
133
        return $this->institutionSet;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function isDefault()
140
    {
141
        return $this->isDefault;
142
    }
143
}
144