Completed
Push — master ( d2aa89...bbfa5f )
by Mihail
34:12 queued 24:23
created

CurlClient::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 2
b 1
f 0
nc 1
nop 4
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Http\Client;
14
15
use Koded\Http\{ClientRequest, ServerResponse};
16
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus, Response};
17
use Psr\Http\Message\UriInterface;
18
use function Koded\Http\create_stream;
19
use function Koded\Stdlib\json_serialize;
20
21
/**
22
 * @link http://php.net/manual/en/context.curl.php
23
 */
24
class CurlClient extends ClientRequest implements HttpRequestClient
25
{
26
    use EncodingTrait, Psr18ClientTrait;
27
28
    private array $options = [
29
        CURLOPT_MAXREDIRS      => 20,
30
        CURLOPT_RETURNTRANSFER => true,
31
        CURLOPT_FOLLOWLOCATION => true,
32
        CURLOPT_SSL_VERIFYPEER => 1,
33
        CURLOPT_SSL_VERIFYHOST => 2,
34
        CURLOPT_USERAGENT      => self::USER_AGENT,
35
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
36
        CURLOPT_FAILONERROR    => 0,
37
    ];
38
39
    private array $responseHeaders = [];
40
41
    public function __construct(
42
        string $method,
43 19
        string|UriInterface $uri,
44
        string|iterable $body = null,
45 19
        array $headers = [])
46 19
    {
47 19
        parent::__construct($method, $uri, $body, $headers);
48
        $this->options[CURLOPT_TIMEOUT] = (\ini_get('default_socket_timeout') ?: 10.0) * 1.0;
49 15
    }
50
51 15
    public function read(): Response
52 1
    {
53
        if ($resource = $this->assertSafeMethod()) {
54
            return $resource;
55 14
        }
56 14
        $this->prepareRequestBody();
57
        $this->prepareOptions();
58
        try {
59 14
            if (false === $resource = $this->createResource()) {
0 ignored issues
show
introduced by
The condition false === $resource = $this->createResource() is always false.
Loading history...
60 1
                return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY,
61 1
                    'The HTTP client is not created therefore cannot read anything');
62 1
            }
63
            \curl_setopt_array($resource, $this->options);
64
            $response = \curl_exec($resource);
65 12
            if ($this->hasError($resource)) {
66 11
                return $this->getCurlError(HttpStatus::FAILED_DEPENDENCY, $resource);
67
            }
68 11
            return new ServerResponse(
69 4
                $response,
70 4
                \curl_getinfo($resource, CURLINFO_RESPONSE_CODE),
71
                $this->responseHeaders);
72
        } catch (\TypeError $e) {
73 7
            return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY,
74 7
                $e->getMessage());
75 7
        } catch (\Throwable $e) {
76 7
            return $this->getPhpError(HttpStatus::INTERNAL_SERVER_ERROR, $e->getMessage());
77
        } finally {
78 2
            unset($response);
79 2
        }
80
    }
81 14
82
    public function userAgent(string $value): HttpRequestClient
83 14
    {
84 14
        $this->options[CURLOPT_USERAGENT] = $value;
85
        return $this;
86
    }
87
88
    public function followLocation(bool $value): HttpRequestClient
89 1
    {
90
        $this->options[CURLOPT_FOLLOWLOCATION] = $value;
91 1
        return $this;
92
    }
93 1
94
    public function maxRedirects(int $value): HttpRequestClient
95
    {
96 1
        $this->options[CURLOPT_MAXREDIRS] = $value;
97
        return $this;
98 1
    }
99
100 1
    public function timeout(float $value): HttpRequestClient
101
    {
102
        $this->options[CURLOPT_TIMEOUT] = $value;
103 2
        return $this;
104
    }
105 2
106
    public function ignoreErrors(bool $value): HttpRequestClient
107 2
    {
108
        // false = do not fail on error
109
        $this->options[CURLOPT_FAILONERROR] = (int)!$value;
110 11
        return $this;
111
    }
112 11
113
    public function verifySslHost(bool $value): HttpRequestClient
114 11
    {
115
        $this->options[CURLOPT_SSL_VERIFYHOST] = $value ? 2 : 0;
116
        return $this;
117 1
    }
118
119
    public function verifySslPeer(bool $value): HttpRequestClient
120 1
    {
121
        $this->options[CURLOPT_SSL_VERIFYPEER] = $value ? 1 : 0;
122 1
        return $this;
123
    }
124
125 1
    public function withProtocolVersion($version): static
126
    {
127 1
        $instance = parent::withProtocolVersion($version);
128
        $instance->options[CURLOPT_HTTP_VERSION] =
129 1
            ['1.1' => CURL_HTTP_VERSION_1_1,
130
             '1.0' => CURL_HTTP_VERSION_1_0][$version];
131
        return $instance;
132 1
    }
133
134 1
    protected function createResource(): \CurlHandle|bool
135
    {
136 1
        return \curl_init((string)$this->getUri());
0 ignored issues
show
Bug Best Practice introduced by
The expression return curl_init((string)$this->getUri()) could return the type resource which is incompatible with the type-hinted return CurlHandle|boolean. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
139 1
    protected function hasError($resource): bool
140
    {
141 1
        return \curl_errno($resource) > 0;
142
    }
143 1
144 1
    protected function prepareOptions(): void
145 1
    {
146 1
        $this->options[CURLOPT_HEADERFUNCTION] = [$this, 'extractFromResponseHeaders'];
147
        $this->options[CURLOPT_CUSTOMREQUEST]  = $this->getMethod();
148 1
        $this->options[CURLOPT_HTTPHEADER]     = $this->getFlattenedHeaders();
149
        unset($this->options[CURLOPT_HTTPHEADER][0]); // Host header is always present and first
150
    }
151
152
    protected function prepareRequestBody(): void
153
    {
154 11
        if (!$this->stream->getSize()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->stream->getSize() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
155
            return;
156 11
        }
157
        $this->stream->rewind();
158
        if (0 === $this->encoding) {
159 10
            $this->options[CURLOPT_POSTFIELDS] = $this->stream->getContents();
160
        } elseif ($content = \json_decode($this->stream->getContents() ?: '[]', true)) {
161 10
            $this->normalizeHeader('Content-type', self::X_WWW_FORM_URLENCODED, true);
162
            $this->options[CURLOPT_POSTFIELDS] = \http_build_query($content, null, '&', $this->encoding);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $numeric_prefix of http_build_query(). ( Ignorable by Annotation )

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

162
            $this->options[CURLOPT_POSTFIELDS] = \http_build_query($content, /** @scrutinizer ignore-type */ null, '&', $this->encoding);
Loading history...
163
        }
164 14
        $this->stream = create_stream($this->options[CURLOPT_POSTFIELDS]);
165
    }
166 14
167 14
    protected function getCurlError(int $status, $resource): Response
168 14
    {
169 14
        //see https://tools.ietf.org/html/rfc7807
170 14
        return new ServerResponse(json_serialize([
171
            'title'    => \curl_error($resource),
172 14
            'detail'   => \curl_strerror(\curl_errno($resource)),
173
            'instance' => \curl_getinfo($resource, CURLINFO_EFFECTIVE_URL),
174 14
            'type'     => 'https://httpstatuses.com/' . $status,
175 11
            'status'   => $status,
176
        ]), $status, ['Content-type' => 'application/problem+json']);
177
    }
178 3
179
    /**
180 3
     * Extracts the headers from curl response.
181 1
     *
182 2
     * @param resource $_      curl instance
183 2
     * @param string   $header Current header line
184 2
     *
185
     * @return int Header length
186
     */
187 3
    protected function extractFromResponseHeaders($_, string $header): int
188 3
    {
189
        try {
190 4
            [$k, $v] = \explode(':', $header, 2) + [1 => null];
191
            null === $v || $this->responseHeaders[$k] = $v;
192 4
        } catch (\Throwable) {
193 4
            /** NOOP **/
194 4
        } finally {
195 4
            return \mb_strlen($header);
196 4
        }
197
    }
198
}
199