InstitutionCollection::contains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Identity\Collection;
20
21
use ArrayIterator;
22
use Broadway\Serializer\Serializable as SerializableInterface;
23
use Iterator;
24
use IteratorAggregate;
25
use JsonSerializable;
26
use Surfnet\Stepup\Exception\RuntimeException;
27
use Surfnet\Stepup\Identity\Value\Institution;
28
29
/**
30
 * @implements IteratorAggregate<Institution>
31
 * @phpstan-type InstitutionArray array<int, Institution>
32
 */
33
final class InstitutionCollection implements IteratorAggregate, JsonSerializable, SerializableInterface
34
{
35
    private array $elements = [];
36
37
    /**
38
     * @param list<Institution> $institutions
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Collection\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
     */
40
    public function __construct(array $institutions = [])
41
    {
42
        foreach ($institutions as $institution) {
43
            $this->add($institution);
44
        }
45
    }
46
47
    public function contains(Institution $institution): bool
48
    {
49
        return in_array($institution, $this->elements);
50
    }
51
52
    /**
53
     * Adds the institution to this collection
54
     *
55
     * @throws RuntimeException when the institution already exists in this collection
56
     */
57
    public function add(Institution $institution): void
58
    {
59
        if (in_array($institution, $this->elements)) {
60
            throw new RuntimeException(
61
                sprintf(
62
                    'Institution "%s" is already in this collection',
63
                    $institution,
64
                ),
65
            );
66
        }
67
68
        $this->elements[] = $institution;
69
    }
70
71
    /**
72
     * Adds all institutions from the given collection to this collection
73
     */
74
    public function addAllFrom(InstitutionCollection $institutionCollection): void
75
    {
76
        foreach ($institutionCollection as $institution) {
77
            $this->add($institution);
78
        }
79
    }
80
81
    /**
82
     * Removes an institution from this collection
83
     *
84
     * @throws RuntimeException when the institution to remove is not in this collection
85
     */
86
    public function remove(Institution $institution): void
87
    {
88
        if (!in_array($institution, $this->elements)) {
89
            throw new RuntimeException(
90
                sprintf(
91
                    'Cannot remove Institution "%s" from the collection as it is not in the collection',
92
                    $institution,
93
                ),
94
            );
95
        }
96
97
        $elements = array_filter($this->elements, fn($inst): bool => !$institution->equals($inst));
98
        $this->elements = $elements;
99
    }
100
101
    /**
102
     * Removes all Institutions in the given collection from this collection
103
     */
104
    public function removeAllIn(InstitutionCollection $institutionCollection): void
105
    {
106
        foreach ($institutionCollection as $institution) {
107
            $this->remove($institution);
108
        }
109
    }
110
111
    public function jsonSerialize(): array
112
    {
113
        return ['institutions' => $this->elements];
114
    }
115
116
    public static function deserialize(array $data): self
117
    {
118
        $institutions = array_values(array_map(static fn($institution): Institution => new Institution($institution), $data));
119
120
        return new self($institutions);
121
    }
122
123
    public function serialize(): array
124
    {
125
        return array_map(fn(Institution $institution): string => (string)$institution, $this->elements);
126
    }
127
128
    /**
129
     * @return Iterator<int, Institution>
130
     */
131
    public function getIterator(): Iterator
132
    {
133
        return new ArrayIterator($this->elements);
134
    }
135
}
136