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

mlocati / nexi / 8551769251

04 Apr 2024 08:40AM UTC coverage: 2.179% (+2.2%) from 0.0%
8551769251

push

github

mlocati
Improve JSON generation, add ways to enable insecure HTTPS connections

14 of 153 new or added lines in 7 files covered. (9.15%)

2 existing lines in 2 files now uncovered.

37 of 1698 relevant lines covered (2.18%)

0.02 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
    /**
13
     * @var int
14
     */
15
    private $flags;
16

17
    public function __construct(int $flags = 0)
18
    {
NEW
19
        $this->flags = $flags;
×
NEW
20
    }
×
21

22
    public static function isAvailable(): bool
23
    {
24
        return in_array('http', stream_get_wrappers(), true);
×
25
    }
26

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

58
        return new Response($statusCode, $responseBody);
×
59
    }
60

61
    /**
62
     * @return resource
63
     */
64
    protected function createContext(string $method, array $headers, string $rawBody)
65
    {
66
        $options = [
NEW
67
            'https' => $this->createHttpContextOptions($method, $headers, $rawBody),
×
NEW
68
            'ssl' => $this->createSslContextOptions(),
×
69
        ];
70

NEW
71
        return stream_context_create($options);
×
72
    }
73

74
    protected function createHttpContextOptions(string $method, array $headers, string $rawBody): array
75
    {
76
        $options = [
77
            'method' => $method,
×
78
            'ignore_errors' => true,
79
        ];
80
        if ($rawBody !== '') {
×
81
            $options['content'] = $rawBody;
×
82
        }
83
        if ($headers !== '') {
×
84
            $options['header'] = [];
×
85
            foreach ($headers as $key => $value) {
×
86
                $options['header'][] = "{$key}: {$value}";
×
87
            }
88
        }
89

90
        return $options;
×
91
    }
92

93
    protected function createSslContextOptions(): array
94
    {
NEW
95
        $options = [];
×
NEW
96
        if (($this->flags & static::FLAG_ALLOWINSECUREHTTPS) === static::FLAG_ALLOWINSECUREHTTPS) {
×
97
            $options += [
NEW
98
                'verify_peer' => false,
×
99
                'verify_peer_name' => false,
100
                'allow_self_signed' => true,
101
            ];
102
        }
103

NEW
104
        return $options;
×
105
    }
106

107
    protected function extractStatusCode(array $httpResponseHeaders): ?int
108
    {
109
        $chunks = $httpResponseHeaders === [] ? [] : explode(' ', $httpResponseHeaders[0], 3);
×
110

111
        return isset($chunks[1]) && is_numeric($chunks[1]) ? (int) $chunks[1] : null;
×
112
    }
113
}
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