Passed
Push — master ( ab28ee...e2645a )
by Tobias
02:45
created

FileGetContents   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 74
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 19 2
A filterHeaders() 0 12 3
A getStreamContextArray() 0 30 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Client;
6
7
use Buzz\Configuration\ParameterBag;
8
use Buzz\Message\HeaderConverter;
9
use Buzz\Exception\NetworkException;
10
use Buzz\Message\ResponseBuilder;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
class FileGetContents extends AbstractClient implements BuzzClientInterface
15
{
16 118
    public function sendRequest(RequestInterface $request, array $options = []): ResponseInterface
17
    {
18 118
        $options = $this->validateOptions($options);
19 117
        $context = stream_context_create($this->getStreamContextArray($request, $options));
20
21 117
        $level = error_reporting(0);
22 117
        $content = file_get_contents($request->getUri()->__toString(), false, $context);
23 117
        error_reporting($level);
24 117
        if (false === $content) {
25 5
            $error = error_get_last();
26
27 5
            throw new NetworkException($request, $error['message']);
28
        }
29
30 112
        $requestBuilder = new ResponseBuilder($this->responseFactory);
31 112
        $requestBuilder->parseHttpHeaders($this->filterHeaders((array) $http_response_header));
32 112
        $requestBuilder->writeBody($content);
33
34 112
        return $requestBuilder->getResponse();
35
    }
36
37
    /**
38
     * Converts a request into an array for stream_context_create().
39
     *
40
     * @param RequestInterface $request A request object
41
     *
42
     * @return array An array for stream_context_create()
43
     */
44 118
    protected function getStreamContextArray(RequestInterface $request, ParameterBag $options): array
45
    {
46 118
        $headers = $request->getHeaders();
47 118
        unset($headers['Host']);
48
        $context = [
49 118
            'http' => [
50
                // values from the request
51 118
                'method' => $request->getMethod(),
52 118
                'header' => implode("\r\n", HeaderConverter::toBuzzHeaders($headers)),
53 118
                'content' => $request->getBody()->__toString(),
54 118
                'protocol_version' => $request->getProtocolVersion(),
55
56
                // values from the current client
57
                'ignore_errors' => true,
58 118
                'follow_location' => $options->get('allow_redirects') && $options->get('max_redirects') > 0,
59 118
                'max_redirects' => $options->get('max_redirects') + 1,
60 118
                'timeout' => $options->get('timeout'),
61
            ],
62
            'ssl' => [
63 118
                'verify_peer' => $options->get('verify'),
64 118
                'verify_host' => $options->get('verify'),
65
            ],
66
        ];
67
68 118
        if (null !== $options->get('proxy')) {
69
            $context['http']['proxy'] = $options->get('proxy');
70
            $context['http']['request_fulluri'] = true;
71
        }
72
73 118
        return $context;
74
    }
75
76 112
    private function filterHeaders(array $headers): array
77
    {
78 112
        $filtered = [];
79 112
        foreach ($headers as $header) {
80 112
            if (0 === stripos($header, 'http/')) {
81 112
                $filtered = [];
82
            }
83
84 112
            $filtered[] = $header;
85
        }
86
87 112
        return $filtered;
88
    }
89
}
90