• 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/Curl.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 Curl 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 extension_loaded('curl');
×
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
        $options = $this->buildOptions($method, $url, $headers, $rawBody);
×
35
        $ch = curl_init();
×
36
        if ($ch === false) {
×
37
            throw new HttpRequestFailed('curl_init() failed');
×
38
        }
39
        try {
40
            if (curl_setopt_array($ch, $options) === false) {
×
41
                $err = curl_error($ch);
×
42

43
                throw new HttpRequestFailed('curl_setopt_array() failed' . ($err ? ": {$err}" : ''));
×
44
            }
45
            $responseBody = curl_exec($ch);
×
46
            if ($responseBody === false) {
×
47
                $err = curl_error($ch);
×
48

49
                throw new HttpRequestFailed('curl_exec() failed' . ($err ? ": {$err}" : ''));
×
50
            }
51
            $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
×
52
            if (!is_numeric($statusCode)) {
×
53
                $err = curl_error($ch);
×
54

55
                throw new HttpRequestFailed('curl_getinfo() failed' . ($err ? ": {$err}" : ''));
×
56
            }
57
        } finally {
×
58
            curl_close($ch);
×
59
        }
60

61
        return new Response((int) $statusCode, $responseBody);
×
62
    }
63

64
    protected function buildOptions(string $method, string $url, array $headers, string $rawBody): array
65
    {
66
        $options = [
67
            CURLOPT_URL => $url,
×
68
            CURLOPT_RETURNTRANSFER => true,
×
69
            CURLOPT_FAILONERROR => false,
×
70
        ];
NEW
71
        if (($this->flags & static::FLAG_ALLOWINSECUREHTTPS) === static::FLAG_ALLOWINSECUREHTTPS) {
×
72
            $options += [
NEW
73
                CURLOPT_SSL_VERIFYHOST => 0,
×
NEW
74
                CURLOPT_SSL_VERIFYPEER => 0,
×
75
            ];
76
        }
77
        if ($rawBody !== '') {
×
78
            $options[CURLOPT_POSTFIELDS] = $rawBody;
×
79
        }
80
        if ($headers !== '') {
×
81
            $join = [];
×
82
            foreach ($headers as $key => $value) {
×
83
                $join[] = "{$key}: {$value}";
×
84
            }
85
            $options[CURLOPT_HTTPHEADER] = $join;
×
86
        }
87
        switch ($method) {
×
88
            case 'GET':
×
89
                $options[CURLOPT_HTTPGET] = true;
×
90
                break;
×
91
            case 'POST':
×
92
                $options[CURLOPT_POST] = true;
×
93
                break;
×
94
            default:
95
                $options[CURLOPT_CUSTOMREQUEST] = $method;
×
96
                break;
×
97
        }
98

99
        return $options;
×
100
    }
101
}
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