AccessToken::requestToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the strays/baidu-ai.
5
 *
6
 * (c) strays <[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 Strays\BaiDuAi\Kernel;
13
14
use Pimple\Container;
15
use Strays\BaiDuAi\Kernel\Contracts\AccessTokenInterface;
16
use Strays\BaiDuAi\Kernel\Traits\HttpRequests;
17
use Strays\BaiDuAi\Kernel\Traits\InteractsWithCache;
18
19
class AccessToken implements AccessTokenInterface
20
{
21
    use InteractsWithCache, HttpRequests;
22
23
    /**
24
     * @var Container
25
     */
26
    protected $app;
27
28
    /**
29
     * @var
30
     */
31
    protected $accessToken;
32
33
    /**
34
     * @var string
35
     */
36
    protected $requestUrl = 'https://aip.baidubce.com/oauth/2.0/token';
37
38
    /**
39
     * @var string
40
     */
41
    protected $cacheKey = 'BaiDuAi.kernel.access_token';
42
43
    /**
44
     * @var bool
45
     */
46
    protected static $status = false;
47
48
    /**
49
     * AccessToken constructor.
50
     *
51
     * @param Container $app
52
     */
53
    public function __construct(Container $app)
54
    {
55
        $this->app = $app;
56
    }
57
58
    /**
59
     * @return mixed|null
60
     *
61
     * @throws \Psr\SimpleCache\InvalidArgumentException
62
     */
63
    public function getToken()
64
    {
65
        if ($this->accessToken) {
66
            return $this->accessToken;
67
        }
68
69
        $cache = $this->getCache();
70
        if (!self::$status && $cache->has($this->cacheKey)) {
71
            return $cache->get($this->cacheKey);
72
        }
73
74
        $token = $this->requestToken($this->getDefaultConfig());
75
76
        $this->setAccessToken($token);
77
78
        return $this->accessToken;
79
    }
80
81
    /**
82
     * @param array $config
83
     *
84
     * @return mixed
85
     */
86
    public function requestToken(array $config)
87
    {
88
        $response = $this->sendRequest($config);
89
90
        $body = json_decode($response->getBody()->getContents());
91
92
        return $body;
93
    }
94
95
    /**
96
     * @param $token
97
     *
98
     * @return array
99
     *
100
     * @throws \Psr\SimpleCache\InvalidArgumentException
101
     */
102
    protected function setAccessToken($token)
103
    {
104
        $cache = [
105
            'access_token' => $token->access_token,
106
        ];
107
108
        $this->getCache()->set($this->cacheKey, $cache, $token->expires_in);
109
110
        return $this->accessToken = $cache;
111
    }
112
113
    /**
114
     * @return mixed|null
115
     *
116
     * @throws \Psr\SimpleCache\InvalidArgumentException
117
     */
118
    public function applyToRequest()
119
    {
120
        $token = $this->getToken();
121
122
        return $token;
123
    }
124
125
    /**
126
     * @param array $config
127
     *
128
     * @return mixed|\Psr\Http\Message\ResponseInterface
129
     */
130
    public function sendRequest(array $config)
131
    {
132
        $baseUrl = $this->requestUrl.'?'.http_build_query($config);
133
134
        return $this->setHttpClient($this->app['http_client'])->request($baseUrl, 'POST', []);
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    protected function getDefaultConfig()
141
    {
142
        return [
143
            'grant_type' => 'client_credentials',
144
            'client_id' => $this->app['config']->get('apiKey'),
145
            'client_secret' => $this->app['config']->get('secretKey'),
146
        ];
147
    }
148
}
149