Client::fileHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Gemz\HttpClient;
4
5
use Gemz\HttpClient\Contracts\Options;
6
use Symfony\Component\HttpClient\HttpClient;
7
use Symfony\Component\Mime\Part\DataPart;
8
use Symfony\Contracts\HttpClient\HttpClientInterface;
9
use Symfony\Contracts\HttpClient\ResponseInterface;
10
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
11
12
class Client
13
{
14
    use Options;
15
16
    /** @var HttpClientInterface */
17
    protected $client;
18
19
    /** @var Config */
20
    protected $config;
21
22
    /**
23
     * @param Config|null $config
24
     *
25
     * @return Client
26
     */
27
    public static function create(Config $config = null)
28
    {
29
        return new self($config);
30
    }
31
32
    /**
33
     * @param Config|null $config
34
     */
35
    public function __construct(Config $config = null)
36
    {
37
        $this->config = $config ?: new Config();
38
39
        $this->transferBodyFormatFromConfig();
40
        $this->transferThrowErrorsFromConfig();
41
42
        $this->client = $this->buildClient();
43
    }
44
45
    /**
46
     * @return $this
47
     */
48
    protected function transferThrowErrorsFromConfig(): self
49
    {
50
        $this->throwErrors = $this->config->shouldThrowErrors();
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return $this
57
     */
58
    protected function transferBodyFormatFromConfig(): self
59
    {
60
        return $this->bodyFormat(
61
            $this->config->getBodyFormat()
62
        );
63
    }
64
65
    /**
66
     * @param string $endpoint
67
     *
68
     * @return Response|mixed
69
     *
70
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
71
     */
72
    public function get(string $endpoint)
73
    {
74
        return $this->request('GET', $endpoint);
75
    }
76
77
    /**
78
     * @param string $endpoint
79
     *
80
     * @return Response|mixed
81
     *
82
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
83
     */
84
    public function head(string $endpoint)
85
    {
86
        return $this->request('HEAD', $endpoint);
87
    }
88
89
    /**
90
     * @param string $endpoint
91
     *
92
     * @return Response|mixed
93
     *
94
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
95
     */
96
    public function post(string $endpoint)
97
    {
98
        return $this->request('POST', $endpoint);
99
    }
100
101
    /**
102
     * @param string $endpoint
103
     *
104
     * @return Response|mixed
105
     *
106
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
107
     */
108
    public function put(string $endpoint)
109
    {
110
        return $this->request('PUT', $endpoint);
111
    }
112
113
    /**
114
     * @param string $endpoint
115
     *
116
     * @return Response|mixed
117
     *
118
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
119
     */
120
    public function patch(string $endpoint)
121
    {
122
        return $this->request('PATCH', $endpoint);
123
    }
124
125
    /**
126
     * @param string $endpoint
127
     *
128
     * @return Response
129
     *
130
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
131
     */
132
    public function delete(string $endpoint)
133
    {
134
        return $this->request('DELETE', $endpoint);
135
    }
136
137
    /**
138
     * @param string $method
139
     * @param string $endpoint
140
     *
141
     * @return mixed|Response
142
     *
143
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
144
     */
145
    protected function request(string $method, string $endpoint)
146
    {
147
        $this->resolvePayload();
148
149
        return Response::createFromResponse(
150
            $this->client->request($method, $endpoint, $this->options),
151
            $this->throwErrors
152
        );
153
    }
154
155
    /**
156
     * @param string $pathToFile
157
     * @param string|null $name
158
     * @param string|null $contentType
159
     *
160
     * @return DataPart
161
     */
162
    public static function fileHandler(
163
        string $pathToFile,
164
        string $name = null,
165
        string $contentType = null): DataPart
166
    {
167
        return DataPart::fromPath($pathToFile, $name, $contentType);
168
    }
169
170
    /**
171
     * @param ResponseInterface|ResponseInterface[]|iterable $responses
172
     *
173
     * @return ResponseStreamInterface
174
     */
175
    public function stream($responses): ResponseStreamInterface
176
    {
177
        return $this->client->stream(
178
            collect($responses)->transform(function ($item) {
179
                return $item->response();
180
            })
181
        );
182
    }
183
184
    /**
185
     * @return HttpClientInterface
186
     */
187
    protected function buildClient(): HttpClientInterface
188
    {
189
        return HttpClient::create($this->config->toArray());
190
    }
191
192
    /**
193
     * @return array<String|Array>
194
     */
195
    public function config(): array
196
    {
197
        return $this->config->toArray();
198
    }
199
200
    /**
201
     * @return array<mixed>
202
     */
203
    public function options(): array
204
    {
205
        return $this->options;
206
    }
207
208
    /**
209
     * @return HttpClientInterface
210
     */
211
    public function client(): HttpClientInterface
212
    {
213
        return $this->client;
214
    }
215
}
216