Passed
Push — master ( f070bb...4ac1e0 )
by Marcel
10:45
created

SsoState::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
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
17
{
18
    /** @var string */
19
    private $localSessionId;
20
21
    /** @var ParameterBag */
22
    private $parameters;
23
24
    /** @var SsoSessionState[] */
25
    private $ssoSessions = [];
26
27
    public function __construct()
28
    {
29
        $this->parameters = new ParameterBag();
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getLocalSessionId()
36
    {
37
        return $this->localSessionId;
38
    }
39
40
    /**
41
     * @param string $localSessionId
42
     *
43
     * @return SsoState
44
     */
45
    public function setLocalSessionId($localSessionId)
46
    {
47
        $this->localSessionId = $localSessionId;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return ParameterBag
54
     */
55
    public function getParameters()
56
    {
57
        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
    public function getOptions()
66
    {
67
        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
    public function addOption($name, $value)
79
    {
80
        $this->parameters->set($name, $value);
81
82
        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
    public function removeOption($name)
93
    {
94
        $this->parameters->remove($name);
95
96
        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
    public function hasOption($name)
107
    {
108
        return $this->parameters->has($name);
109
    }
110
111
    /**
112
     * @return SsoSessionState[]
113
     */
114
    public function getSsoSessions()
115
    {
116
        return $this->ssoSessions;
117
    }
118
119
    /**
120
     * @param SsoSessionState[] $ssoSessions
121
     *
122
     * @return SsoState
123
     */
124
    public function setSsoSessions(array $ssoSessions)
125
    {
126
        $this->ssoSessions = [];
127
        foreach ($ssoSessions as $ssoSession) {
128
            $this->addSsoSession($ssoSession);
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return SsoState
136
     */
137
    public function addSsoSession(SsoSessionState $ssoSessionState)
138
    {
139
        $this->ssoSessions[] = $ssoSessionState;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param $idpEntityId
146
     * @param $spEntityId
147
     * @param $nameId
148
     * @param $nameIdFormat
149
     * @param $sessionIndex
150
     *
151
     * @return SsoSessionState[]
152
     */
153
    public function filter($idpEntityId, $spEntityId, $nameId, $nameIdFormat, $sessionIndex)
154
    {
155
        $result = [];
156
157
        foreach ($this->ssoSessions as $ssoSession) {
158
            if ((!$idpEntityId || $ssoSession->getIdpEntityId() === $idpEntityId) &&
159
                (!$spEntityId || $ssoSession->getSpEntityId() === $spEntityId) &&
160
                (!$nameId || $ssoSession->getNameId() === $nameId) &&
161
                (!$nameIdFormat || $ssoSession->getNameIdFormat() === $nameIdFormat) &&
162
                (!$sessionIndex || $ssoSession->getSessionIndex() === $sessionIndex)
163
            ) {
164
                $result[] = $ssoSession;
165
            }
166
        }
167
168
        return $result;
169
    }
170
171
    /**
172
     * @param callable $callback
173
     *
174
     * @return SsoState
175
     */
176
    public function modify($callback)
177
    {
178
        $this->ssoSessions = array_values(array_filter($this->ssoSessions, $callback));
179
180
        return $this;
181
    }
182
183
    public function __serialize(): array {
184
        return [
185
            'id' => $this->localSessionId,
186
            'sessions' => $this->ssoSessions,
187
            'parameters' => $this->parameters->all(),
188
        ];
189
    }
190
191
    public function __unserialize(array $data): void {
192
        $this->localSessionId = $data['id'];
193
        $this->ssoSessions = $data['sessions'];
194
        $this->parameters = new ParameterBag($data['parameters']);
195
    }
196
}
197