Completed
Pull Request — master (#14)
by xu
04:01
created

Client::transformMultipartUploadData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 23
rs 9.7998
cc 4
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the overtrue/http.
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 Overtrue\Http;
13
14
use GuzzleHttp\Client as GuzzleClient;
15
use Overtrue\Http\Traits\HasHttpRequests;
16
17
/**
18
 * Class BaseClient.
19
 *
20
 * @author overtrue <[email protected]>
21
 */
22
class Client
23
{
24
    use HasHttpRequests {
25
        request as performRequest;
26
    }
27
28
    /**
29
     * @var \Overtrue\Http\Config
30
     */
31
    protected $config;
32
33
    /**
34
     * @var
35
     */
36
    protected $baseUri;
37
38
    /**
39
     * @return static
40
     */
41
    public static function create(): self
42
    {
43
        return new static(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $config of Overtrue\Http\Client::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

43
        return new static(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
44
    }
45
46
    /**
47
     * Client constructor.
48
     *
49
     * @param \Overtrue\Http\Config|array $config
50
     */
51
    public function __construct($config = [])
52
    {
53
        $this->config = $this->normalizeConfig($config);
54
    }
55
56
    /**
57
     * GET request.
58
     *
59
     * @param string $url
60
     * @param array  $query
61
     *
62
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
63
     */
64
    public function get(string $url, array $query = [])
65
    {
66
        return $this->request($url, 'GET', ['query' => $query]);
67
    }
68
69
    /**
70
     * POST request.
71
     *
72
     * @param string $url
73
     * @param array  $data
74
     *
75
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
76
     */
77
    public function post(string $url, array $data = [])
78
    {
79
        return $this->request($url, 'POST', ['form_params' => $data]);
80
    }
81
82
    /**
83
     * JSON request.
84
     *
85
     * @param string       $url
86
     * @param string|array $data
87
     * @param array        $query
88
     *
89
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
90
     */
91
    public function postJson(string $url, array $data = [], array $query = [])
92
    {
93
        return $this->request($url, 'POST', ['query' => $query, 'json' => $data]);
94
    }
95
96
    /**
97
     * Upload file.
98
     *
99
     * @param string $url
100
     * @param array  $files
101
     * @param array  $form
102
     * @param array  $query
103
     *
104
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
105
     */
106
    public function upload(string $url, array $files = [], array $form = [], array $query = [])
107
    {
108
        $multipart = [];
109
110
        foreach ($files as $name => $path) {
111
            $multipart[] = [
112
                'name' => $name,
113
                'contents' => fopen($path, 'r'),
114
            ];
115
        }
116
117
        foreach ($form as $name => $contents) {
118
            $multipart = array_merge($multipart, $this->transformMultipartUploadData($name, $contents));
119
        }
120
121
        return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart]);
122
    }
123
124
    /**
125
     * @param string $uri
126
     * @param string $method
127
     * @param array  $options
128
     * @param bool   $returnRaw
129
     *
130
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
131
     */
132
    public function request(string $uri, string $method = 'GET', array $options = [], $returnRaw = false)
133
    {
134
        if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) {
135
            $options['base_uri'] = $this->baseUri;
136
        }
137
138
        if ((!empty($options['base_uri']) || $this->config->getBaseUri()) && $this->config->needAutoTrimEndpointSlash()) {
139
            $uri = ltrim($uri, '/');
140
        }
141
142
        $response = $this->performRequest($uri, $method, $options);
143
144
        return $this->castResponseToType(
145
            $response,
146
            $returnRaw ? 'raw' : $this->config->getOption('response_type')
147
        );
148
    }
149
150
    /**
151
     * @param string $url
152
     * @param string $method
153
     * @param array  $options
154
     *
155
     * @return \Overtrue\Http\Responses\Response
156
     */
157
    public function requestRaw(string $url, string $method = 'GET', array $options = [])
158
    {
159
        return $this->request($url, $method, $options, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->request($u...method, $options, true) returns the type Overtrue\Http\Support\Collection which is incompatible with the documented return type Overtrue\Http\Responses\Response.
Loading history...
160
    }
161
162
    /**
163
     * Return GuzzleHttp\Client instance.
164
     *
165
     * @return \GuzzleHttp\ClientInterface
166
     */
167
    public function getHttpClient(): \GuzzleHttp\ClientInterface
168
    {
169
        if (!$this->httpClient) {
170
            $this->httpClient = new GuzzleClient($this->config->toArray());
171
        }
172
173
        return $this->httpClient;
174
    }
175
176
    /**
177
     * @return \Overtrue\Http\Config
178
     */
179
    public function getConfig(): \Overtrue\Http\Config
180
    {
181
        return $this->config;
182
    }
183
184
    /**
185
     * @param \Overtrue\Http\Config $config
186
     *
187
     * @return \Overtrue\Http\Client
188
     */
189
    public function setConfig(\Overtrue\Http\Config $config): \Overtrue\Http\Client
190
    {
191
        $this->config = $config;
192
193
        return $this;
194
    }
195
196
    /**
197
     * Transform Multipart Upload Data.
198
     *
199
     * @param string|int         $name
200
     * @param string|array|mixed $contents
201
     *
202
     * @return array
203
     */
204
    protected function transformMultipartUploadData($name, $contents)
205
    {
206
        if (!is_array($contents)) {
207
            return [[
208
                'name' => $name,
209
                'contents' => $contents,
210
            ]];
211
        }
212
213
        $datas = [];
214
        foreach ($contents as $key => $value) {
215
            $key = $name.'['.$key.']';
216
            if (is_array($value)) {
217
                $datas = array_merge($datas, $this->transformMultipartUploadData($key, $value));
218
            } else {
219
                $datas[] = [
220
                    'name' => $key,
221
                    'contents' => $value,
222
                ];
223
            }
224
        }
225
226
        return $datas;
227
    }
228
229
    /**
230
     * @param mixed $config
231
     *
232
     * @return \Overtrue\Http\Config
233
     */
234
    protected function normalizeConfig($config): \Overtrue\Http\Config
235
    {
236
        if (\is_array($config)) {
237
            $config = new Config($config);
238
        }
239
240
        if (!($config instanceof Config)) {
241
            throw new \InvalidArgumentException('config must be array or instance of Overtrue\Http\Config.');
242
        }
243
244
        return $config;
245
    }
246
}
247