SymfonySession::clearAuthorizationState()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 14
c 0
b 0
f 0
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
namespace OAuth\Common\Storage;
4
5
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
6
use OAuth\Common\Storage\Exception\TokenNotFoundException;
7
use OAuth\Common\Token\TokenInterface;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
10
class SymfonySession implements TokenStorageInterface
11
{
12
    private $session;
13
14
    private $sessionVariableName;
15
16
    private $stateVariableName;
17
18
    /**
19
     * @param bool $startSession
20
     * @param string $sessionVariableName
21
     * @param string $stateVariableName
22
     */
23
    public function __construct(
24
        SessionInterface $session,
25
        $startSession = true,
26
        $sessionVariableName = 'lusitanian_oauth_token',
27
        $stateVariableName = 'lusitanian_oauth_state'
28
    ) {
29
        $this->session = $session;
30
        $this->sessionVariableName = $sessionVariableName;
31
        $this->stateVariableName = $stateVariableName;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function retrieveAccessToken($service)
38
    {
39
        if ($this->hasAccessToken($service)) {
40
            // get from session
41
            $tokens = $this->session->get($this->sessionVariableName);
42
43
            // one item
44
            return $tokens[$service];
45
        }
46
47
        throw new TokenNotFoundException('Token not found in session, are you sure you stored it?');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function storeAccessToken($service, TokenInterface $token)
54
    {
55
        // get previously saved tokens
56
        $tokens = $this->session->get($this->sessionVariableName);
57
58
        if (!is_array($tokens)) {
59
            $tokens = [];
60
        }
61
62
        $tokens[$service] = $token;
63
64
        // save
65
        $this->session->set($this->sessionVariableName, $tokens);
66
67
        // allow chaining
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function hasAccessToken($service)
75
    {
76
        // get from session
77
        $tokens = $this->session->get($this->sessionVariableName);
78
79
        return is_array($tokens)
80
            && isset($tokens[$service])
81
            && $tokens[$service] instanceof TokenInterface;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function clearToken($service)
88
    {
89
        // get previously saved tokens
90
        $tokens = $this->session->get($this->sessionVariableName);
91
92
        if (is_array($tokens) && array_key_exists($service, $tokens)) {
93
            unset($tokens[$service]);
94
95
            // Replace the stored tokens array
96
            $this->session->set($this->sessionVariableName, $tokens);
97
        }
98
99
        // allow chaining
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function clearAllTokens()
107
    {
108
        $this->session->remove($this->sessionVariableName);
109
110
        // allow chaining
111
        return $this;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function retrieveAuthorizationState($service)
118
    {
119
        if ($this->hasAuthorizationState($service)) {
120
            // get from session
121
            $states = $this->session->get($this->stateVariableName);
122
123
            // one item
124
            return $states[$service];
125
        }
126
127
        throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?');
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function storeAuthorizationState($service, $state)
134
    {
135
        // get previously saved tokens
136
        $states = $this->session->get($this->stateVariableName);
137
138
        if (!is_array($states)) {
139
            $states = [];
140
        }
141
142
        $states[$service] = $state;
143
144
        // save
145
        $this->session->set($this->stateVariableName, $states);
146
147
        // allow chaining
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function hasAuthorizationState($service)
155
    {
156
        // get from session
157
        $states = $this->session->get($this->stateVariableName);
158
159
        return is_array($states)
160
        && isset($states[$service])
161
        && null !== $states[$service];
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function clearAuthorizationState($service)
168
    {
169
        // get previously saved tokens
170
        $states = $this->session->get($this->stateVariableName);
171
172
        if (is_array($states) && array_key_exists($service, $states)) {
173
            unset($states[$service]);
174
175
            // Replace the stored tokens array
176
            $this->session->set($this->stateVariableName, $states);
177
        }
178
179
        // allow chaining
180
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function clearAllAuthorizationStates()
187
    {
188
        $this->session->remove($this->stateVariableName);
189
190
        // allow chaining
191
        return $this;
192
    }
193
194
    /**
195
     * @return Session
196
     */
197
    public function getSession()
198
    {
199
        return $this->session;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->session returns the type Symfony\Component\HttpFo...ession\SessionInterface which is incompatible with the documented return type OAuth\Common\Storage\Session.
Loading history...
200
    }
201
}
202