SsoState::filter()   C
last analyzed

Complexity

Conditions 12
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 6.9666
cc 12
nc 3
nop 5
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\State\Sso;
13
14
use LightSaml\Meta\ParameterBag;
15
16
class SsoState implements \Serializable
17
{
18
    /** @var string */
19
    private $localSessionId;
20
21
    /** @var ParameterBag */
22
    private $parameters;
23
24
    /** @var SsoSessionState[] */
25
    private $ssoSessions = array();
26
27 15
    public function __construct()
28
    {
29 15
        $this->parameters = new ParameterBag();
30 15
    }
31
32
    /**
33
     * @return string
34
     */
35 2
    public function getLocalSessionId()
36
    {
37 2
        return $this->localSessionId;
38
    }
39
40
    /**
41
     * @param string $localSessionId
42
     *
43
     * @return SsoState
44
     */
45 3
    public function setLocalSessionId($localSessionId)
46
    {
47 3
        $this->localSessionId = $localSessionId;
48
49 3
        return $this;
50
    }
51
52
    /**
53
     * @return ParameterBag
54
     */
55 2
    public function getParameters()
56
    {
57 2
        return $this->parameters;
58
    }
59
60
    /**
61
     * @deprecated Since 1.2, to be removed in 2.0. Use getParameters() instead
62
     *
63
     * @return array
64
     */
65 2
    public function getOptions()
66
    {
67 2
        return $this->parameters->all();
68
    }
69
70
    /**
71
     * @deprecated Since 1.2, to be removed in 2.0. Use getParameters() instead
72
     *
73
     * @param string $name
74
     * @param mixed  $value
75
     *
76
     * @return SsoState
77
     */
78 2
    public function addOption($name, $value)
79
    {
80 2
        $this->parameters->set($name, $value);
81
82 2
        return $this;
83
    }
84
85
    /**
86
     * @deprecated Since 1.2, to be removed in 2.0. Use getParameters() instead
87
     *
88
     * @param string $name
89
     *
90
     * @return SsoState
91
     */
92 1
    public function removeOption($name)
93
    {
94 1
        $this->parameters->remove($name);
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @deprecated Since 1.2, to be removed in 2.0. Use getParameters() instead
101
     *
102
     * @param string $name
103
     *
104
     * @return bool
105
     */
106 1
    public function hasOption($name)
107
    {
108 1
        return $this->parameters->has($name);
109
    }
110
111
    /**
112
     * @return SsoSessionState[]
113
     */
114 7
    public function getSsoSessions()
115
    {
116 7
        return $this->ssoSessions;
117
    }
118
119
    /**
120
     * @param SsoSessionState[] $ssoSessions
121
     *
122
     * @return SsoState
123
     */
124 1
    public function setSsoSessions(array $ssoSessions)
125
    {
126 1
        $this->ssoSessions = array();
127 1
        foreach ($ssoSessions as $ssoSession) {
128 1
            $this->addSsoSession($ssoSession);
129
        }
130
131 1
        return $this;
132
    }
133
134
    /**
135
     * @param SsoSessionState $ssoSessionState
136
     *
137
     * @return SsoState
138
     */
139 8
    public function addSsoSession(SsoSessionState $ssoSessionState)
140
    {
141 8
        $this->ssoSessions[] = $ssoSessionState;
142
143 8
        return $this;
144
    }
145
146
    /**
147
     * @param $idpEntityId
148
     * @param $spEntityId
149
     * @param $nameId
150
     * @param $nameIdFormat
151
     * @param $sessionIndex
152
     *
153
     * @return SsoSessionState[]
154
     */
155 3
    public function filter($idpEntityId, $spEntityId, $nameId, $nameIdFormat, $sessionIndex)
156
    {
157 3
        $result = array();
158
159 3
        foreach ($this->ssoSessions as $ssoSession) {
160 2
            if ((!$idpEntityId || $ssoSession->getIdpEntityId() === $idpEntityId) &&
161 2
                (!$spEntityId || $ssoSession->getSpEntityId() === $spEntityId) &&
162 2
                (!$nameId || $ssoSession->getNameId() === $nameId) &&
163 2
                (!$nameIdFormat || $ssoSession->getNameIdFormat() === $nameIdFormat) &&
164 2
                (!$sessionIndex || $ssoSession->getSessionIndex() === $sessionIndex)
165
            ) {
166 2
                $result[] = $ssoSession;
167
            }
168
        }
169
170 3
        return $result;
171
    }
172
173
    /**
174
     * @param callable $callback
175
     *
176
     * @return SsoState
177
     */
178 1
    public function modify($callback)
179
    {
180 1
        $this->ssoSessions = array_values(array_filter($this->ssoSessions, $callback));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values(array_filte...soSessions, $callback)) of type array<integer,?> is incompatible with the declared type array<integer,object<Lig...e\Sso\SsoSessionState>> of property $ssoSessions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * @return string the string representation of the object or null
187
     */
188 2
    public function serialize()
189
    {
190 2
        return serialize(array(
191 2
            $this->localSessionId,
192 2
            $this->ssoSessions,
193
            [],
194 2
            $this->parameters,
195
        ));
196
    }
197
198
    /**
199
     * @param string $serialized
200
     *
201
     * @return void
202
     */
203 2
    public function unserialize($serialized)
204
    {
205 2
        $data = unserialize($serialized);
206
207
        // add a few extra elements in the array to ensure that we have enough keys when unserializing
208
        // older data which does not include all properties.
209 2
        $data = array_merge($data, array_fill(0, 5, null));
210 2
        $oldOptions = null;
211
212
        list(
213 2
            $this->localSessionId,
214 2
            $this->ssoSessions,
215
            $oldOptions, // old deprecated options
216 2
            $this->parameters) = $data;
217
218
        // in case it was serialized in old way, copy old options to parameters
219 2
        if ($oldOptions && 0 == $this->parameters->count()) {
220
            $this->parameters->add($oldOptions);
221
        }
222 2
    }
223
}
224