InstitutionSet   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toScalarArray() 0 3 1
A getInstitutions() 0 3 1
A equals() 0 3 1
A create() 0 17 2
A isOption() 0 3 1
A __construct() 0 7 2
A sort() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright 2018 SURFnet B.V.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\Stepup\Configuration\Value;
22
23
use Surfnet\Stepup\Exception\InvalidArgumentException;
24
25
final class InstitutionSet
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class InstitutionSet
Loading history...
26
{
27
    /**
28
     * @var Institution[]
29
     */
30
    private readonly array $institutions;
31
32
    /**
33
     * @param Institution[] $institutions
34
     */
35
    private function __construct(array $institutions)
36
    {
37
        if ($institutions !== array_unique($institutions)) {
38
            throw new InvalidArgumentException('Duplicate entries are not allowed in the InstitutionSet');
39
        }
40
41
        $this->institutions = $this->sort($institutions);
0 ignored issues
show
Bug introduced by
The property institutions is declared read-only in Surfnet\Stepup\Configuration\Value\InstitutionSet.
Loading history...
42
    }
43
44
    /**
45
     * @param Institution[] $institutions
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    public static function create(array $institutions): self
48
    {
49
        // Verify only institutions are collected in the set
50
        array_walk(
51
            $institutions,
52
            function ($institution, $key) use ($institutions): void {
53
                if (!$institution instanceof Institution) {
54
                    throw InvalidArgumentException::invalidType(
55
                        Institution::class,
56
                        'institutions',
57
                        $institutions[$key],
58
                    );
59
                }
60
            },
61
        );
62
63
        return new self($institutions);
64
    }
65
66
    public function equals(InstitutionSet $other): bool
67
    {
68
        return $this->toScalarArray() === $other->toScalarArray();
69
    }
70
71
    public function isOption(Institution $institution): bool
72
    {
73
        return in_array($institution->getInstitution(), $this->institutions);
74
    }
75
76
    /**
77
     * @return Institution[]
78
     */
79
    public function getInstitutions(): array
80
    {
81
        return $this->institutions;
82
    }
83
84
    public function toScalarArray(): array
85
    {
86
        return array_map('strval', $this->institutions);
87
    }
88
89
    /**
90
     * @param Institution[] $institutions
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
91
     * @return Institution[]
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
92
     */
93
    private function sort(array $institutions): array
0 ignored issues
show
Coding Style introduced by
Private method name "InstitutionSet::sort" must be prefixed with an underscore
Loading history...
94
    {
95
        usort(
96
            $institutions,
97
            fn(Institution $a, Institution $b): int => strcmp($a->getInstitution(), $b->getInstitution()),
98
        );
99
100
        return $institutions;
101
    }
102
}
103