Token   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getToken() 0 13 2
A requestToken() 0 15 4
A __construct() 0 3 1
A applyToRequest() 0 7 1
A getQuery() 0 3 1
A getCacheKey() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the docodeit/wechat.
5
 *
6
 * (c) docodeit <[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 JinWeChat\Kernel;
13
14
use JinWeChat\Kernel\Contracts\TokenInterface;
15
use JinWeChat\Kernel\Exceptions\HttpException;
16
use JinWeChat\Kernel\Traits\HasHttpRequests;
17
use JinWeChat\Kernel\Traits\InteractsWithCache;
18
use Pimple\Container;
19
use Psr\Http\Message\RequestInterface;
20
21
class Token implements TokenInterface
22
{
23
    use HasHttpRequests, InteractsWithCache;
0 ignored issues
show
Bug introduced by
The trait JinWeChat\Kernel\Traits\HasHttpRequests requires the property $baseUri which is not provided by JinWeChat\Kernel\Token.
Loading history...
24
25
    protected $app;
26
27
    protected $requestMethod = 'GET';
28
29
    protected $token;
30
31
    protected $queryName;
32
33
    protected $safeSeconds = 500;
34
35
    /**
36
     * @var string
37
     */
38
    protected $tokenKey = 'token';
39
40
    /**
41
     * @var string
42
     */
43
    protected $cachePrefix = 'JinWeChat.kernel.token.';
44
45
    public function __construct(Container $app)
46
    {
47
        $this->app = $app;
48
    }
49
50
    public function getToken(): array
51
    {
52
        $cacheKey = $this->getCacheKey();
53
        $cache = $this->getCache();
54
        if ($cache->has($cacheKey)) {
55
            return $cache->get($cacheKey);
56
        }
57
58
        $token = $this->requestToken($this->getCredentials(), true);
0 ignored issues
show
Bug introduced by
The method getCredentials() does not exist on JinWeChat\Kernel\Token. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $token = $this->requestToken($this->/** @scrutinizer ignore-call */ getCredentials(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60
        $this->setToken($token[$this->tokenKey], $token['expires_in'] ?? 57600);
0 ignored issues
show
Bug introduced by
The method setToken() does not exist on JinWeChat\Kernel\Token. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $this->/** @scrutinizer ignore-call */ 
61
               setToken($token[$this->tokenKey], $token['expires_in'] ?? 57600);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        return $token;
63
    }
64
65
    public function applyToRequest(RequestInterface $request, array $requestOptions = []): RequestInterface
66
    {
67
        parse_str($request->getUri()->getQuery(), $query);
68
69
        $query = http_build_query(array_merge($this->getQuery(), $query));
70
71
        return $request->withUri($request->getUri()->withQuery($query));
72
    }
73
74
    protected function getQuery(): array
75
    {
76
        return [$this->queryName ?? $this->token => $this->getToken()[$this->token]];
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function getCacheKey()
83
    {
84
        return $this->cachePrefix.$this->app['config']['username'];
85
    }
86
87
    public function requestToken(array $credentials, $toArray = false)
88
    {
89
        $response = $this->sendRequest($credentials);
0 ignored issues
show
Bug introduced by
The method sendRequest() does not exist on JinWeChat\Kernel\Token. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        /** @scrutinizer ignore-call */ 
90
        $response = $this->sendRequest($credentials);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $result = json_decode($response->getBody()->getContents(), true);
91
        $formatted = $this->castResponseToType($response, $this->app['config']->get('response_type'));
0 ignored issues
show
Bug introduced by
The method castResponseToType() does not exist on JinWeChat\Kernel\Token. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        /** @scrutinizer ignore-call */ 
92
        $formatted = $this->castResponseToType($response, $this->app['config']->get('response_type'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
        if (preg_match('/token=([\d]+)/i', $result['redirect_url'], $match)) {
93
            $this->token = $match[1];
94
        }
95
        $this->baseRefer = $result['redirect_url'];
0 ignored issues
show
Bug Best Practice introduced by
The property baseRefer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
97
        if (empty($result['redirect_url'])) {
98
            throw new HttpException('Request token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE), $response, $formatted);
99
        }
100
101
        return $toArray ? $result : $formatted;
102
    }
103
}
104