RaLocationList::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
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\Configuration\Value;
20
21
use ArrayIterator;
22
use Iterator;
23
use IteratorAggregate;
24
use Surfnet\Stepup\Configuration\Entity\RaLocation;
25
use Surfnet\Stepup\Exception\LogicException;
26
27
/**
28
 * @implements IteratorAggregate<RaLocation>
29
 */
30
final class RaLocationList implements IteratorAggregate
31
{
32
    /**
33
     * @var RaLocation[]
34
     */
35
    private array $raLocations = [];
36
37
    public function __construct(array $raLocations)
38
    {
39
        foreach ($raLocations as $raLocation) {
40
            $this->add($raLocation);
41
        }
42
    }
43
44
    public function containsWithId(RaLocationId $raLocationId): bool
45
    {
46
        foreach ($this->raLocations as $raLocation) {
47
            if ($raLocation->hasId($raLocationId)) {
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    public function add(RaLocation $raLocation): void
56
    {
57
        if ($this->containsWithId($raLocation->getId())) {
58
            throw new LogicException(
59
                sprintf(
60
                    'Cannot add RaLocation with id "%s" to RaLocationList: it is already present',
61
                    $raLocation->getId(),
62
                ),
63
            );
64
        }
65
66
        $this->raLocations[] = $raLocation;
67
    }
68
69
    public function removeWithId(RaLocationId $raLocationId): void
70
    {
71
        foreach ($this->raLocations as $key => $raLocation) {
72
            if ($raLocation->hasId($raLocationId)) {
73
                unset($this->raLocations[$key]);
74
                $this->raLocations = array_values($this->raLocations);
75
76
                return;
77
            }
78
        }
79
80
        throw new LogicException(
81
            sprintf(
82
                'Cannot remove RaLocation with id "%s" from RaLocationList: it is not present',
83
                $raLocationId,
84
            ),
85
        );
86
    }
87
88
    public function getById(RaLocationId $raLocationId): RaLocation
89
    {
90
        foreach ($this->raLocations as $raLocation) {
91
            if ($raLocation->hasId($raLocationId)) {
92
                return $raLocation;
93
            }
94
        }
95
96
        throw new LogicException(
97
            sprintf(
98
                'Cannot get RaLocation by id "%s" from RaLocationList: RaLocationId not found',
99
                $raLocationId,
100
            ),
101
        );
102
    }
103
104
    public function getIterator(): Iterator
105
    {
106
        return new ArrayIterator($this->raLocations);
107
    }
108
}
109