Completed
Pull Request — master (#484)
by
unknown
04:41
created

Memory::hasAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace OAuth\Common\Storage;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\Common\Storage\Exception\TokenNotFoundException;
7
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
8
9
/*
10
 * Stores a token in-memory only (destroyed at end of script execution).
11
 */
12
class Memory implements TokenStorageInterface
13
{
14
    /**
15
     * @var object|TokenInterface
16
     */
17
    protected $tokens;
18
19
    /**
20
     * @var array
21
     */
22
    protected $states;
23
24
    public function __construct()
25
    {
26
        $this->tokens = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object of property $tokens.

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...
27
        $this->states = array();
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function retrieveAccessToken($service)
34
    {
35
        if ($this->hasAccessToken($service)) {
36
            return $this->tokens[$service];
37
        }
38
39
        throw new TokenNotFoundException('Token not stored');
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function storeAccessToken($service, TokenInterface $token)
46
    {
47
        $this->tokens[$service] = $token;
48
49
        // allow chaining
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function hasAccessToken($service)
57
    {
58
        return isset($this->tokens[$service]) && $this->tokens[$service] instanceof TokenInterface;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function clearToken($service)
65
    {
66
        if (array_key_exists($service, $this->tokens)) {
67
            unset($this->tokens[$service]);
68
        }
69
70
        // allow chaining
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function clearAllTokens()
78
    {
79
        $this->tokens = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object of property $tokens.

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...
80
81
        // allow chaining
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function retrieveAuthorizationState($service)
89
    {
90
        if ($this->hasAuthorizationState($service)) {
91
            return $this->states[$service];
92
        }
93
94
        throw new AuthorizationStateNotFoundException('State not stored');
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function storeAuthorizationState($service, $state)
101
    {
102
        $this->states[$service] = $state;
103
104
        // allow chaining
105
        return $this;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function hasAuthorizationState($service)
112
    {
113
        return isset($this->states[$service]) && null !== $this->states[$service];
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function clearAuthorizationState($service)
120
    {
121
        if (array_key_exists($service, $this->states)) {
122
            unset($this->states[$service]);
123
        }
124
125
        // allow chaining
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function clearAllAuthorizationStates()
133
    {
134
        $this->states = array();
135
136
        // allow chaining
137
        return $this;
138
    }
139
}
140