ShibbolethMetadataScopeList   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 0
loc 169
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A inScope() 0 12 3
A add() 0 4 1
A contains() 0 10 3
A indexOf() 0 10 3
A get() 0 14 3
A find() 0 12 3
A equals() 0 14 4
A toArray() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A deserialize() 0 10 1
A serialize() 0 6 1
A __toString() 0 4 1
1
<?php
2
3
namespace OpenConext\Value\Saml\Metadata;
4
5
use ArrayIterator;
6
use Countable;
7
use IteratorAggregate;
8
use OpenConext\Value\Assert\Assertion;
9
use OpenConext\Value\Exception\IndexOutOfBoundsException;
10
use OpenConext\Value\Serializable;
11
12
/**
13
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) consistent list interface dictates quite a few
14
 */
15
final class ShibbolethMetadataScopeList implements Countable, IteratorAggregate, Serializable
16
{
17
    /**
18
     * @var ShibbolethMetadataScope[]
19
     */
20
    private $scopes = array();
21
22
    /**
23
     * @param ShibbolethMetadataScope[] $scopes
24
     */
25
    public function __construct(array $scopes = array())
26
    {
27
        Assertion::allIsInstanceOf($scopes, '\OpenConext\Value\Saml\Metadata\ShibbolethMetadataScope');
28
29
        $this->scopes = array_values($scopes);
30
    }
31
32
    /**
33
     * @param string $string
34
     * @return bool
35
     */
36
    public function inScope($string)
37
    {
38
        Assertion::string($string, 'Scope to check must be a string, "%s" given', 'string');
39
40
        foreach ($this->scopes as $scope) {
41
            if ($scope->allows($string)) {
42
                return true;
43
            }
44
        }
45
46
        return false;
47
    }
48
49
    /**
50
     * @param ShibbolethMetadataScope $scope
51
     * @return ShibbolethMetadataScopeList
52
     */
53
    public function add(ShibbolethMetadataScope $scope)
54
    {
55
        return new ShibbolethMetadataScopeList(array_merge($this->scopes, array($scope)));
56
    }
57
58
    /**
59
     * @param ShibbolethMetadataScope $shibbolethMetadataScope
60
     * @return bool
61
     */
62
    public function contains(ShibbolethMetadataScope $shibbolethMetadataScope)
63
    {
64
        foreach ($this->scopes as $scope) {
65
            if ($scope->equals($shibbolethMetadataScope)) {
66
                return true;
67
            }
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * @param ShibbolethMetadataScope $shibbolethMetadataScope
75
     * @return int
76
     */
77
    public function indexOf(ShibbolethMetadataScope $shibbolethMetadataScope)
78
    {
79
        foreach ($this->scopes as $index => $scope) {
80
            if ($scope->equals($shibbolethMetadataScope)) {
81
                return $index;
82
            }
83
        }
84
85
        return -1;
86
    }
87
88
    /**
89
     * @param int $index
90
     * @return ShibbolethMetadataScope
91
     */
92
    public function get($index)
93
    {
94
        Assertion::integer($index);
95
96
        if ($index < 0) {
97
            throw IndexOutOfBoundsException::tooLow($index, 0);
98
        }
99
100
        if ($index > count($this->scopes) - 1) {
101
            throw IndexOutOfBoundsException::tooHigh($index, count($this->scopes) - 1);
102
        }
103
104
        return $this->scopes[$index];
105
    }
106
107
    /**
108
     * @param Callable $predicate
109
     * @return null|ShibbolethMetadataScope
110
     */
111
    public function find($predicate)
112
    {
113
        Assertion::isCallable($predicate, null, 'predicate');
114
115
        foreach ($this->scopes as $shibbolethMetadataScope) {
116
            if (call_user_func($predicate, $shibbolethMetadataScope) === true) {
117
                return $shibbolethMetadataScope;
118
            }
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * @param ShibbolethMetadataScopeList $other
126
     * @return bool
127
     */
128
    public function equals(ShibbolethMetadataScopeList $other)
129
    {
130
        if (count($this->scopes) !== count($other->scopes)) {
131
            return false;
132
        }
133
134
        foreach ($this->scopes as $index => $scope) {
135
            if (!$scope->equals($other->scopes[$index])) {
136
                return false;
137
            }
138
        }
139
140
        return true;
141
    }
142
143
    /**
144
     * @return ShibbolethMetadataScope[]
145
     */
146
    public function toArray()
147
    {
148
        return $this->scopes;
149
    }
150
151
    public function getIterator()
152
    {
153
        return new ArrayIterator($this->scopes);
154
    }
155
156
    public function count()
157
    {
158
        return count($this->scopes);
159
    }
160
161
    public static function deserialize($data)
162
    {
163
        Assertion::isArray($data);
164
165
        $scopes = array_map(function ($scope) {
166
            return ShibbolethMetadataScope::deserialize($scope);
167
        }, $data);
168
169
        return new self($scopes);
170
    }
171
172
    public function serialize()
173
    {
174
        return array_map(function (ShibbolethMetadataScope $shibbolethMetadataScope) {
175
            return $shibbolethMetadataScope->serialize();
176
        }, $this->scopes);
177
    }
178
179
    public function __toString()
180
    {
181
        return sprintf('ShibbolethMetadataScopeList(%s)', join(', ', array_map('strval', $this->scopes)));
182
    }
183
}
184