Completed
Push — master ( 5e707a...73f162 )
by Stefan
02:28
created

Client::fileHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
     *
158
     * @return DataPart
159
     */
160
    public static function fileHandler(string $pathToFile): DataPart
161
    {
162
        return DataPart::fromPath($pathToFile);
163
    }
164
165
    /**
166
     * @param ResponseInterface|ResponseInterface[]|iterable $responses
167
     *
168
     * @return ResponseStreamInterface
169
     */
170
    public function stream($responses): ResponseStreamInterface
171
    {
172
        return $this->client->stream(
173
            collect($responses)->transform(function ($item) {
174
                return $item->response();
175
            })
176
        );
177
    }
178
179
    /**
180
     * @return HttpClientInterface
181
     */
182
    protected function buildClient(): HttpClientInterface
183
    {
184
        return HttpClient::create($this->config->toArray());
185
    }
186
187
    /**
188
     * @return array<String|Array>
189
     */
190
    public function config(): array
191
    {
192
        return $this->config->toArray();
193
    }
194
195
    /**
196
     * @return array<mixed>
197
     */
198
    public function options(): array
199
    {
200
        return $this->options;
201
    }
202
203
    /**
204
     * @return HttpClientInterface
205
     */
206
    public function client(): HttpClientInterface
207
    {
208
        return $this->client;
209
    }
210
}
211