• 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/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
    public static function isAvailable(): bool
13
    {
14
        return extension_loaded('curl');
×
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
        $options = $this->buildOptions($method, $url, $headers, $rawBody);
×
25
        $ch = curl_init();
×
26
        if ($ch === false) {
×
27
            throw new HttpRequestFailed('curl_init() failed');
×
28
        }
29
        try {
30
            if (curl_setopt_array($ch, $options) === false) {
×
31
                $err = curl_error($ch);
×
32

33
                throw new HttpRequestFailed('curl_setopt_array() failed' . ($err ? ": {$err}" : ''));
×
34
            }
35
            $responseBody = curl_exec($ch);
×
36
            if ($responseBody === false) {
×
37
                $err = curl_error($ch);
×
38

39
                throw new HttpRequestFailed('curl_exec() failed' . ($err ? ": {$err}" : ''));
×
40
            }
41
            $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
×
42
            if (!is_numeric($statusCode)) {
×
43
                $err = curl_error($ch);
×
44

45
                throw new HttpRequestFailed('curl_getinfo() failed' . ($err ? ": {$err}" : ''));
×
46
            }
47
        } finally {
×
48
            curl_close($ch);
×
49
        }
50

51
        return new Response((int) $statusCode, $responseBody);
×
52
    }
53

54
    protected function buildOptions(string $method, string $url, array $headers, string $rawBody): array
55
    {
56
        $options = [
57
            CURLOPT_URL => $url,
×
58
            CURLOPT_RETURNTRANSFER => true,
×
59
            CURLOPT_FAILONERROR => false,
×
60
        ];
61
        if ($rawBody !== '') {
×
62
            $options[CURLOPT_POSTFIELDS] = $rawBody;
×
63
        }
64
        if ($headers !== '') {
×
65
            $join = [];
×
66
            foreach ($headers as $key => $value) {
×
67
                $join[] = "{$key}: {$value}";
×
68
            }
69
            $options[CURLOPT_HTTPHEADER] = $join;
×
70
        }
71
        switch ($method) {
×
72
            case 'GET':
×
73
                $options[CURLOPT_HTTPGET] = true;
×
74
                break;
×
75
            case 'POST':
×
76
                $options[CURLOPT_POST] = true;
×
77
                break;
×
78
            default:
79
                $options[CURLOPT_CUSTOMREQUEST] = $method;
×
80
                break;
×
81
        }
82

83
        return $options;
×
84
    }
85
}
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