Issues (52)

src/Kernel/Traits/HasHttpRequests.php (1 issue)

1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Traits;
10
11
use GuzzleHttp\ClientInterface;
12
use GuzzleHttp\Client;
13
use GuzzleHttp\HandlerStack;
14
use Psr\Http\Message\ResponseInterface;
15
16
trait HasHttpRequests
17
{
18
    use ResponseCastable;
19
20
    protected $httpClient;
21
22
    protected $middlewares = [];
23
24
    protected $handlerStack;
25
26
    protected static $defaults = [
27
        'curl' => [
28
            CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
29
        ],
30
    ];
31
32
    public static function setDefaultOptions($defaults = [])
33
    {
34
        self::$defaults = $defaults;
35
    }
36
37
    public static function getDefaultOptions(): array
38
    {
39
        return self::$defaults;
40
    }
41
42
    public function setHttpClient(ClientInterface $httpClient)
43
    {
44
        $this->httpClient = $httpClient;
45
46
        return $this;
47
    }
48
49
    public function getHttpClient(): Client
50
    {
51
        if (!($this->httpClient instanceof ClientInterface)) {
52
            $this->httpClient = new Client();
53
        }
54
55
        return $this->httpClient;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->httpClient could return the type GuzzleHttp\ClientInterface which includes types incompatible with the type-hinted return GuzzleHttp\Client. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    /**
59
     * @param callable $middleware
60
     * @param string|null $name
61
     * @return $this
62
     */
63
    public function pushMiddleware(callable $middleware, string $name = null)
64
    {
65
        if (!is_null($name)) {
66
            $this->middlewares[$name] = $middleware;
67
        } else {
68
            array_push($this->middlewares, $middleware);
69
        }
70
71
        return $this;
72
    }
73
74
    public function getMiddleWares(): array
75
    {
76
        return $this->middlewares;
77
    }
78
79
    /**
80
     * @param $url
81
     * @param string $method
82
     * @param array $options
83
     * @return ResponseInterface
84
     * @throws \GuzzleHttp\Exception\GuzzleException
85
     */
86
    public function request($url, $method = 'GET', $options = []): ResponseInterface
87
    {
88
        $method = strtoupper($method);
89
90
        $options = array_merge(self::$defaults, $options, ['handler' => $this->getHandlerStack()]);
91
92
        $options = $this->fixJsonIssue($options);
93
94
        if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) {
95
            $options['base_uri'] = $this->baseUri;
96
        }
97
98
        $response = $this->getHttpClient()->request($method, $url, $options);
99
        $response->getBody()->rewind();
100
101
        return $response;
102
    }
103
104
    public function setHandlerStack(HandlerStack $handlerStack)
105
    {
106
        $this->handlerStack = $handlerStack;
107
108
        return $this;
109
    }
110
111
    public function getHandlerStack(): HandlerStack
112
    {
113
        if ($this->handlerStack) {
114
            return $this->handlerStack;
115
        }
116
117
        $this->handlerStack = HandlerStack::create();
118
119
        foreach ($this->middlewares as $name => $middleware) {
120
            $this->handlerStack->push($middleware, $name);
121
        }
122
123
        return $this->handlerStack;
124
    }
125
126
    protected function fixJsonIssue(array $options): array
127
    {
128
        if (isset($options['json']) && is_array($options['json'])) {
129
            $options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']);
130
131
            if (empty($options['json'])) {
132
                $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT);
133
            } else {
134
                $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE);
135
            }
136
137
            unset($options['json']);
138
        }
139
140
        return $options;
141
    }
142
}
143