Client   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpConfig() 0 3 1
A withAccessTokenMiddleware() 0 23 3
A withRetryMiddleware() 0 21 5
A __construct() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the mingyoung/dingtalk.
5
 *
6
 * (c) 张铭阳 <[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 EasyDingTalk\Kernel\Http;
13
14
use GuzzleHttp\Middleware;
15
use Overtrue\Http\Client as BaseClient;
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
19
class Client extends BaseClient
20
{
21
    /**
22
     * @var \EasyDingTalk\Application
23
     */
24
    protected $app;
25
26
    /**
27
     * @var array
28
     */
29
    protected static $httpConfig = [
30
        'base_uri' => 'https://oapi.dingtalk.com',
31
    ];
32
33
    /**
34
     * @param \EasyDingTalk\Application $app
35
     */
36
    public function __construct($app)
37
    {
38
        $this->app = $app;
39
40
        parent::__construct(array_merge(static::$httpConfig, $this->app['config']->get('http', [])));
41
    }
42
43
    /**
44
     * @param array $config
45
     */
46
    public function setHttpConfig(array $config)
47
    {
48
        static::$httpConfig = array_merge(static::$httpConfig, $config);
49
    }
50
51
    /**
52
     * @return $this
53
     */
54
    public function withAccessTokenMiddleware()
55
    {
56
        if (isset($this->getMiddlewares()['access_token'])) {
57
            return $this;
58
        }
59
60
        $middleware = function (callable $handler) {
61
            return function (RequestInterface $request, array $options) use ($handler) {
62
                if ($this->app['access_token']) {
63
                    parse_str($request->getUri()->getQuery(), $query);
64
65
                    $request = $request->withUri(
66
                        $request->getUri()->withQuery(http_build_query(['access_token' => $this->app['access_token']->getToken()] + $query))
67
                    );
68
                }
69
70
                return $handler($request, $options);
71
            };
72
        };
73
74
        $this->pushMiddleware($middleware, 'access_token');
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return $this
81
     */
82
    public function withRetryMiddleware()
83
    {
84
        if (isset($this->getMiddlewares()['retry'])) {
85
            return $this;
86
        }
87
88
        $middleware = Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null) {
89
            if (is_null($response) || $retries < 1) {
90
                return false;
91
            }
92
93
            if (in_array(json_decode($response->getBody(), true)['errcode'] ?? null, [40001])) {
94
                $this->app['access_token']->refresh();
95
96
                return true;
97
            }
98
        });
99
100
        $this->pushMiddleware($middleware, 'retry');
101
102
        return $this;
103
    }
104
}
105