Issues (31)

src/Kernel/AccessToken.php (1 issue)

Labels
Severity
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
namespace EasyIM\Kernel;
13
14
use EasyIM\Kernel\Contracts\AccessTokenInterface;
15
use EasyIM\Kernel\Exceptions\RuntimeException;
16
use EasyIM\Kernel\Traits\HasHttpRequests;
17
use EasyIM\Kernel\Traits\InteractsWithCache;
18
use Psr\Http\Message\RequestInterface;
19
20
/**
21
 * Class AccessToken.
22
 *
23
 * @author overtrue <[email protected]>
24
 */
25
abstract class AccessToken implements AccessTokenInterface
26
{
27
    use HasHttpRequests;
0 ignored issues
show
The trait EasyIM\Kernel\Traits\HasHttpRequests requires the property $baseUri which is not provided by EasyIM\Kernel\AccessToken.
Loading history...
28
    use InteractsWithCache;
29
30
    /**
31
     * @var \EasyIM\Kernel\ServiceContainer
32
     */
33
    protected $app;
34
35
    /**
36
     * @var string
37
     */
38
    protected $queryName;
39
40
    /**
41
     * @var array
42
     */
43
    protected $token;
44
45
    /**
46
     * @var string
47
     */
48
    protected $tokenKey = 'access_token';
49
50
    /**
51
     * @var string
52
     */
53
    protected $cachePrefix = 'EasyIM.kernel.access_token.';
54
55
    /**
56
     * AccessToken constructor.
57
     *
58
     * @param \EasyIM\Kernel\ServiceContainer $app
59
     */
60 16
    public function __construct(ServiceContainer $app)
61
    {
62 16
        $this->app = $app;
63 16
    }
64
65
    /**
66
     * @return array
67
     *
68
     * @throws \EasyIM\Kernel\Exceptions\HttpException
69
     * @throws \Psr\SimpleCache\InvalidArgumentException
70
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
71
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
72
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
73
     */
74
    public function getRefreshedToken(): array
75
    {
76
        return $this->getToken(true);
77
    }
78
79
    /**
80
     * @param bool $refresh
81
     *
82
     * @return array
83
     *
84
     * @throws \EasyIM\Kernel\Exceptions\HttpException
85
     * @throws \Psr\SimpleCache\InvalidArgumentException
86
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
87
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
88
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
89
     */
90
    public function getToken(bool $refresh = false): array
91
    {
92
        $cacheKey = $this->getCacheKey();
93
        $cache = $this->getCache();
94
95
        if (!$refresh && $cache->has($cacheKey)) {
96
            return $cache->get($cacheKey);
97
        }
98
99
        /** @var array $token */
100
        $token = $this->requestToken();
101
102
103
        $this->setToken($token[$this->tokenKey], $token['expires_in']);
104
105
        $this->app->events->dispatch(new Events\AccessTokenRefreshed($this));
106
107
        return $token;
108
    }
109
110
    /**
111
     * @param string $token
112
     * @param int    $lifetime
113
     *
114
     * @return \EasyIM\Kernel\Contracts\AccessTokenInterface
115
     *
116
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
117
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
118
     * @throws \Psr\SimpleCache\InvalidArgumentException
119
     */
120 1
    public function setToken(string $token, int $lifetime = 7200): AccessTokenInterface
121
    {
122 1
        $this->getCache()->set($this->getCacheKey(), [
123 1
            $this->tokenKey => $token,
124 1
            'expires_in' => $lifetime,
125 1
        ], $lifetime);
126
127 1
        if (!$this->getCache()->has($this->getCacheKey())) {
128 1
            throw new RuntimeException('Failed to cache access token.');
129
        }
130
131 1
        return $this;
132
    }
133
134
    /**
135
     * @return \EasyIM\Kernel\Contracts\AccessTokenInterface
136
     *
137
     * @throws \EasyIM\Kernel\Exceptions\HttpException
138
     * @throws \Psr\SimpleCache\InvalidArgumentException
139
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
140
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
141
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
142
     */
143 1
    public function refresh(): AccessTokenInterface
144
    {
145 1
        $this->getToken(true);
146
147 1
        return $this;
148
    }
149
150
    /**
151
     *
152
     * @return array
153
     */
154
    abstract public function requestToken(): array;
155
156
    /**
157
     * @param \Psr\Http\Message\RequestInterface $request
158
     * @param array                              $requestOptions
159
     *
160
     * @return \Psr\Http\Message\RequestInterface
161
     *
162
     * @throws \EasyIM\Kernel\Exceptions\HttpException
163
     * @throws \Psr\SimpleCache\InvalidArgumentException
164
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
165
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
166
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
167
     */
168
    abstract public function applyToRequest(RequestInterface $request, array $requestOptions = []): RequestInterface;
169
170
    /**
171
     * @return string
172
     */
173 1
    protected function getCacheKey()
174
    {
175 1
        return $this->cachePrefix.md5(json_encode($this->getCredentials()));
176
    }
177
178
    /**
179
     * The request query will be used to add to the request.
180
     *
181
     * @return array
182
     *
183
     * @throws \EasyIM\Kernel\Exceptions\HttpException
184
     * @throws \Psr\SimpleCache\InvalidArgumentException
185
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
186
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
187
     * @throws \EasyIM\Kernel\Exceptions\RuntimeException
188
     */
189
    protected function getQuery(): array
190
    {
191
        return [$this->queryName ?? $this->tokenKey => $this->getToken()[$this->tokenKey]];
192
    }
193
194
195
    /**
196
     * @return string
197
     */
198
    public function getTokenKey()
199
    {
200
        return $this->tokenKey;
201
    }
202
203
    /**
204
     * Credential for get token.
205
     *
206
     * @return array
207
     */
208
    abstract protected function getCredentials(): array;
209
}
210