Completed
Push — master ( ace74a...38a91c )
by Daan van
04:54
created

RaLocationList::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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