Passed
Push — master ( abd03a...b605c3 )
by Carlos
03:31
created

Authorization::handleAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 1
rs 9.4285

1 Method

Rating   Name   Duplication   Size   Complexity  
A Authorization::removeAuthorizerAccessToken() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[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
/**
13
 * Authorization.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    lixiao <[email protected]>
21
 * @author    mingyoung <[email protected]>
22
 * @copyright 2016
23
 *
24
 * @see      https://github.com/overtrue
25
 * @see      http://overtrue.me
26
 */
27
28
namespace EasyWeChat\OpenPlatform;
29
30
use Doctrine\Common\Cache\Cache;
31
use EasyWeChat\Core\Exception;
32
use EasyWeChat\OpenPlatform\Api\BaseApi;
33
34
class Authorization
35
{
36
    const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token';
37
    const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token';
38
39
    /**
40
     * Cache.
41
     *
42
     * @var \Doctrine\Common\Cache\Cache
43
     */
44
    protected $cache;
45
46
    /**
47
     * Base API.
48
     *
49
     * @var \EasyWeChat\OpenPlatform\Api\BaseApi
50
     */
51
    protected $api;
52
53
    /**
54
     * Open Platform App Id, aka, Component App Id.
55
     *
56
     * @var string
57
     */
58
    protected $appId;
59
60
    /**
61
     * Authorizer App Id.
62
     *
63
     * @var string
64
     */
65
    protected $authorizerAppId;
66
67
    /**
68
     * Authorization Constructor.
69
     *
70
     * @param \EasyWeChat\OpenPlatform\Api\BaseApi $api
71
     * @param string                               $appId
72
     * @param \Doctrine\Common\Cache\Cache         $cache
73
     */
74 6
    public function __construct(BaseApi $api, $appId, Cache $cache)
75
    {
76 6
        $this->api = $api;
77 6
        $this->appId = $appId;
78 6
        $this->cache = $cache;
79 6
    }
80
81
    /**
82
     * Gets the base api.
83
     *
84
     * @return \EasyWeChat\OpenPlatform\Api\BaseApi
85
     */
86 3
    public function getApi()
87
    {
88 3
        return $this->api;
89
    }
90
91
    /**
92
     * Sets the authorizer app id.
93
     *
94
     * @param string $authorizerAppId
95
     *
96
     * @return $this
97
     */
98 6
    public function setAuthorizerAppId($authorizerAppId)
99
    {
100 6
        $this->authorizerAppId = $authorizerAppId;
101
102 6
        return $this;
103
    }
104
105
    /**
106
     * Gets the authorizer app id, or throws if not found.
107
     *
108
     * @return string
109
     *
110
     * @throws \EasyWeChat\Core\Exception
111
     */
112 3
    public function getAuthorizerAppId()
113
    {
114 3
        if (!$this->authorizerAppId) {
115
            throw new Exception(
116
                'Authorizer App Id is not present, you may not make the authorization yet.'
117
            );
118
        }
119
120 3
        return $this->authorizerAppId;
121
    }
122
123
    /**
124
     * Saves the authorizer access token in cache.
125
     *
126
     * @param string $token
127
     *
128
     * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
129
     */
130 1
    public function setAuthorizerAccessToken($token, $expires = 7200)
131
    {
132 1
        return $this->cache->save($this->getAuthorizerAccessTokenKey(), $token, $expires);
133
    }
134
135
    /**
136
     * Gets the authorizer access token.
137
     *
138
     * @return string
139
     */
140 1
    public function getAuthorizerAccessToken()
141
    {
142 1
        return $this->cache->fetch($this->getAuthorizerAccessTokenKey());
143
    }
144
145
    /**
146
     * Saves the authorizer refresh token in cache.
147
     *
148
     * @param string $refreshToken
149
     *
150
     * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
151
     */
152 2
    public function setAuthorizerRefreshToken($refreshToken)
153
    {
154 2
        return $this->cache->save($this->getAuthorizerRefreshTokenKey(), $refreshToken);
155
    }
156
157
    /**
158
     * Gets the authorizer refresh token.
159
     *
160
     * @return string
161
     *
162
     * @throws \EasyWeChat\Core\Exception when refresh token is not present
163
     */
164 1
    public function getAuthorizerRefreshToken()
165
    {
166 1
        if ($token = $this->cache->fetch($this->getAuthorizerRefreshTokenKey())) {
167 1
            return $token;
168
        }
169
170
        throw new Exception(
171
            'Authorizer Refresh Token is not present, you may not make the authorization yet.'
172
        );
173
    }
174
175
    /**
176
     * Removes the authorizer access token from cache.
177
     *
178
     * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
179
     *              Deleting a non-existing entry is considered successful
180
     */
181
    public function removeAuthorizerAccessToken()
182
    {
183
        return $this->cache->delete($this->getAuthorizerAccessTokenKey());
184
    }
185
186
    /**
187
     * Removes the authorizer refresh token from cache.
188
     *
189
     * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
190
     *              Deleting a non-existing entry is considered successful
191
     */
192
    public function removeAuthorizerRefreshToken()
193
    {
194
        return $this->cache->delete($this->getAuthorizerRefreshTokenKey());
195
    }
196
197
    /**
198
     * Gets the authorizer access token cache key.
199
     *
200
     * @return string
201
     */
202 1
    public function getAuthorizerAccessTokenKey()
203
    {
204 1
        return self::CACHE_KEY_ACCESS_TOKEN.$this->appId.$this->getAuthorizerAppId();
205
    }
206
207
    /**
208
     * Gets the authorizer refresh token cache key.
209
     *
210
     * @return string
211
     */
212 2
    public function getAuthorizerRefreshTokenKey()
213
    {
214 2
        return self::CACHE_KEY_REFRESH_TOKEN.$this->appId.$this->getAuthorizerAppId();
215
    }
216
}
217