• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / nexi / 8571051831

05 Apr 2024 02:05PM UTC coverage: 5.917% (+0.8%) from 5.079%
8571051831

push

github

mlocati
Document some of the features

111 of 1876 relevant lines covered (5.92%)

0.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/HttpClient/StreamWrapper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MLocati\Nexi\HttpClient;
6

7
use MLocati\Nexi\Exception\HttpRequestFailed;
8
use MLocati\Nexi\HttpClient;
9

10
class StreamWrapper implements HttpClient
11
{
12
    public static function isAvailable(): bool
13
    {
14
        return in_array('http', stream_get_wrappers(), true);
×
15
    }
16

17
    /**
18
     * {@inheritdoc}
19
     *
20
     * @see \MLocati\Nexi\HttpClient::invoke()
21
     */
22
    public function invoke(string $method, string $url, array $headers, string $rawBody): Response
23
    {
24
        $context = $this->createContext($method, $headers, $rawBody);
×
25
        $whyNot = '';
×
26
        $http_response_header = [];
×
27
        set_error_handler(
×
28
            static function ($errno, $errstr) use (&$whyNot) {
29
                if ($whyNot === '' && is_string($errstr)) {
×
30
                    $whyNot = trim($errstr);
×
31
                }
32
            },
×
33
            -1
×
34
        );
35
        try {
36
            $responseBody = file_get_contents($url, false, $context);
×
37
        } finally {
×
38
            restore_error_handler();
×
39
        }
40
        if ($responseBody === false) {
×
41
            throw new HttpRequestFailed($whyNot ?: 'file_get_contents() failed');
×
42
        }
43
        $statusCode = $this->extractStatusCode($http_response_header);
×
44
        if ($statusCode === null) {
×
45
            throw new HttpRequestFailed('Failed to retrieve the HTTP status code');
×
46
        }
47

48
        return new Response($statusCode, $responseBody);
×
49
    }
50

51
    /**
52
     * @return resource
53
     */
54
    protected function createContext(string $method, array $headers, string $rawBody)
55
    {
56
        $options = [
57
            'https' => $this->createHttpContextOptions($method, $headers, $rawBody),
×
58
            'ssl' => $this->createSslContextOptions(),
×
59
        ];
60

61
        return stream_context_create($options);
×
62
    }
63

64
    protected function createHttpContextOptions(string $method, array $headers, string $rawBody): array
65
    {
66
        $options = [
67
            'method' => $method,
×
68
            'ignore_errors' => true,
69
        ];
70
        if ($rawBody !== '') {
×
71
            $options['content'] = $rawBody;
×
72
        }
73
        if ($headers !== '') {
×
74
            $options['header'] = [];
×
75
            foreach ($headers as $key => $value) {
×
76
                $options['header'][] = "{$key}: {$value}";
×
77
            }
78
        }
79

80
        return $options;
×
81
    }
82

83
    protected function createSslContextOptions(): array
84
    {
85
        return [];
×
86
    }
87

88
    protected function extractStatusCode(array $httpResponseHeaders): ?int
89
    {
90
        $chunks = $httpResponseHeaders === [] ? [] : explode(' ', $httpResponseHeaders[0], 3);
×
91

92
        return isset($chunks[1]) && is_numeric($chunks[1]) ? (int) $chunks[1] : null;
×
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc