CurlOnlyPostHttpClient::post()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 23
rs 9.7
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Ipag\Classes\Http;
4
5
final class CurlOnlyPostHttpClient implements AuthenticableHttpInterface
6
{
7
    const CLIENT = 'IpagSdkPhp';
8
9
    private $httpHeaders;
10
    private $httpPostFields;
11
    private $userAgent;
12
13
    /**
14
     * @var string
15
     */
16
    private $user;
17
18
    /**
19
     * @var string
20
     */
21
    private $password;
22
23
    public function __invoke($endpoint, array $headers = [], array $fields = [])
24
    {
25
        $this->httpHeaders = $this->formatToHttpHeaders($headers);
26
        $this->httpPostFields = $this->formatToHttpPostFields($fields);
27
        $this->userAgent = sprintf('%s (+https://github.com/jhernandes/ipag-sdk-php/)', self::CLIENT);
28
29
        return $this->post($endpoint);
30
    }
31
32
    protected function post($endpoint)
33
    {
34
        $curl = curl_init();
35
        curl_setopt($curl, CURLOPT_URL, $endpoint);
36
        curl_setopt($curl, CURLOPT_USERPWD, "{$this->user}:{$this->password}");
37
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->httpHeaders);
38
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->httpPostFields);
39
        curl_setopt($curl, CURLOPT_POST, true);
40
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
41
        curl_setopt($curl, CURLOPT_HEADER, false);
42
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
43
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
44
        curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
45
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
46
        curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);
47
48
        $response = curl_exec($curl);
49
50
        $this->curlHasError($curl);
51
52
        curl_close($curl);
53
54
        return $response;
55
    }
56
57
    private function formatToHttpHeaders(array $headers = [])
58
    {
59
        return array_map(
60
            function ($name, $value) {
61
                return "{$name}: {$value}";
62
            },
63
            array_keys($headers),
64
            array_values($headers)
65
        );
66
    }
67
68
    private function formatToHttpPostFields(array $fields = [])
69
    {
70
        $formattedFields = '';
71
        foreach ($fields as $name => $value) {
72
            $formattedFields .= "{$name}={$value}&";
73
        }
74
        rtrim($formattedFields, '&');
75
76
        return $formattedFields;
77
    }
78
79
    private function curlHasError($curl)
80
    {
81
        if (curl_errno($curl)) {
82
            throw new \Exception('Curl error: '.curl_error($curl));
83
        }
84
    }
85
86
    /**
87
     * @param string $user
88
     */
89
    public function setUser($user)
90
    {
91
        $this->user = $user;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $password
98
     */
99
    public function setPassword($password)
100
    {
101
        $this->password = $password;
102
103
        return $this;
104
    }
105
}
106