Completed
Push — master ( 45bee2...97600c )
by Jasper
07:14
created

Client::getBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Swis\JsonApi\Client;
4
5
use Http\Discovery\Psr17FactoryDiscovery;
6
use Http\Discovery\Psr18ClientDiscovery;
7
use Psr\Http\Client\ClientInterface as HttpClientInterface;
8
use Psr\Http\Message\RequestFactoryInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamFactoryInterface;
12
use Psr\Http\Message\StreamInterface;
13
use Swis\JsonApi\Client\Interfaces\ClientInterface;
14
15
class Client implements ClientInterface
16
{
17
    /**
18
     * @var \Psr\Http\Client\ClientInterface
19
     */
20
    private $client;
21
22
    /**
23
     * @var \Psr\Http\Message\RequestFactoryInterface
24
     */
25
    private $requestFactory;
26
27
    /**
28
     * @var \Psr\Http\Message\StreamFactoryInterface
29
     */
30
    private $streamFactory;
31
32
    /**
33
     * @var string
34
     */
35
    private $baseUri = '';
36
37
    /**
38
     * @var array
39
     */
40
    private $defaultHeaders = [
41
        'Accept' => 'application/vnd.api+json',
42
        'Content-Type' => 'application/vnd.api+json',
43
    ];
44
45
    /**
46
     * @param \Psr\Http\Client\ClientInterface|null          $client
47
     * @param \Psr\Http\Message\RequestFactoryInterface|null $requestFactory
48
     * @param \Psr\Http\Message\StreamFactoryInterface|null  $streamFactory
49
     */
50 126
    public function __construct(
51
        HttpClientInterface $client = null,
52
        RequestFactoryInterface $requestFactory = null,
53
        StreamFactoryInterface $streamFactory = null
54
    ) {
55 126
        $this->client = $client ?: Psr18ClientDiscovery::find();
56 126
        $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
57 126
        $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
58 126
    }
59
60
    /**
61
     * @return string
62
     */
63 9
    public function getBaseUri(): string
64
    {
65 9
        return $this->baseUri;
66
    }
67
68
    /**
69
     * @param string $baseUri
70
     */
71 27
    public function setBaseUri(string $baseUri): void
72
    {
73 27
        $this->baseUri = $baseUri;
74 27
    }
75
76
    /**
77
     * @return array
78
     */
79 9
    public function getDefaultHeaders(): array
80
    {
81 9
        return $this->defaultHeaders;
82
    }
83
84
    /**
85
     * @param array $defaultHeaders
86
     */
87 9
    public function setDefaultHeaders(array $defaultHeaders): void
88
    {
89 9
        $this->defaultHeaders = $defaultHeaders;
90 9
    }
91
92
    /**
93
     * @param string $endpoint
94
     * @param array  $headers
95
     *
96
     * @throws \Psr\Http\Client\ClientExceptionInterface
97
     *
98
     * @return \Psr\Http\Message\ResponseInterface
99
     */
100 27
    public function get(string $endpoint, array $headers = []): ResponseInterface
101
    {
102 27
        return $this->request('GET', $endpoint, null, $headers);
103
    }
104
105
    /**
106
     * @param string                                                 $endpoint
107
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body
108
     * @param array                                                  $headers
109
     *
110
     * @throws \Psr\Http\Client\ClientExceptionInterface
111
     *
112
     * @return \Psr\Http\Message\ResponseInterface
113
     */
114 9
    public function post(string $endpoint, $body, array $headers = []): ResponseInterface
115
    {
116 9
        return $this->request('POST', $endpoint, $body, $headers);
117
    }
118
119
    /**
120
     * @param string                                                 $endpoint
121
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body
122
     * @param array                                                  $headers
123
     *
124
     * @throws \Psr\Http\Client\ClientExceptionInterface
125
     *
126
     * @return \Psr\Http\Message\ResponseInterface
127
     */
128 9
    public function patch(string $endpoint, $body, array $headers = []): ResponseInterface
129
    {
130 9
        return $this->request('PATCH', $endpoint, $body, $headers);
131
    }
132
133
    /**
134
     * @param string $endpoint
135
     * @param array  $headers
136
     *
137
     * @throws \Psr\Http\Client\ClientExceptionInterface
138
     *
139
     * @return \Psr\Http\Message\ResponseInterface
140
     */
141 9
    public function delete(string $endpoint, array $headers = []): ResponseInterface
142
    {
143 9
        return $this->request('DELETE', $endpoint, null, $headers);
144
    }
145
146
    /**
147
     * @param string                                                 $method
148
     * @param string                                                 $endpoint
149
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body
150
     * @param array                                                  $headers
151
     *
152
     * @throws \Psr\Http\Client\ClientExceptionInterface
153
     *
154
     * @return \Psr\Http\Message\ResponseInterface
155
     */
156 99
    public function request(string $method, string $endpoint, $body = null, array $headers = []): ResponseInterface
157
    {
158 99
        return $this->client->sendRequest($this->buildRequest($method, $endpoint, $body, $headers));
159
    }
160
161
    /**
162
     * @param string                                                 $method
163
     * @param string                                                 $endpoint
164
     * @param string|resource|\Psr\Http\Message\StreamInterface|null $body
165
     * @param array                                                  $headers
166
     *
167
     * @return \Psr\Http\Message\RequestInterface
168
     */
169 99
    protected function buildRequest(string $method, string $endpoint, $body = null, array $headers = []): RequestInterface
170
    {
171 99
        $request = $this->requestFactory->createRequest($method, $this->getEndpoint($endpoint));
172
173 99
        if ($body !== null) {
174 45
            if (is_resource($body)) {
175 9
                $body = $this->streamFactory->createStreamFromResource($body);
176
            }
177 45
            if (!($body instanceof StreamInterface)) {
178 27
                $body = $this->streamFactory->createStream($body);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type null and resource; however, parameter $content of Psr\Http\Message\StreamF...terface::createStream() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

178
                $body = $this->streamFactory->createStream(/** @scrutinizer ignore-type */ $body);
Loading history...
179
            }
180
181 45
            $request = $request->withBody($body);
182
        }
183
184 99
        foreach ($this->mergeHeaders($headers) as $name => $value) {
185 99
            $request = $request->withHeader($name, $value);
186
        }
187
188 99
        return $request;
189
    }
190
191
    /**
192
     * @param string $endpoint
193
     *
194
     * @return string
195
     */
196 99
    protected function getEndpoint(string $endpoint): string
197
    {
198 99
        if (strpos($endpoint, 'http://') === 0 || strpos($endpoint, 'https://') === 0) {
199 9
            return $endpoint;
200
        }
201
202 90
        return $this->baseUri.$endpoint;
203
    }
204
205
    /**
206
     * @param array $headers
207
     *
208
     * @return array
209
     */
210 99
    protected function mergeHeaders(array $headers): array
211
    {
212 99
        return array_merge($this->defaultHeaders, $headers);
213
    }
214
}
215