GloBeeCurlConnector::compileHeaders()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace GloBee\PaymentApi\Connectors;
4
5
use GloBee\PaymentApi\PaymentApi;
6
7
class GloBeeCurlConnector extends Connector
8
{
9
    /**
10
     * @var string
11
     */
12
    private $apiKey;
13
14
    /**
15
     * @var string
16
     */
17
    private $baseUrl;
18
19
    /**
20
     * @var CurlWrapper
21
     */
22
    private $client;
23
24
    /**
25
     * @var array
26
     */
27
    private $platforms;
28
29
    /**
30
     * GloBeeCurlConnector constructor.
31
     *
32
     * @param string           $apiKey
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
33
     * @param bool             $live
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
34
     * @param array            $platforms
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
35
     * @param CurlWrapper|null $curlConnector
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
36
     */
37 12
    public function __construct($apiKey, $live = true, array $platforms = [], CurlWrapper $curlConnector = null)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $apiKey
Loading history...
Coding Style introduced by
Type hint "bool" missing for $live
Loading history...
38
    {
39 12
        $this->apiKey = $apiKey;
40 12
        $this->baseUrl = $live ? 'https://globee.com/payment-api' : 'https://test.globee.com/payment-api';
41 12
        $this->platforms = $platforms;
42 12
        $this->client = $curlConnector ?: new CurlWrapper();
43 12
    }
44
45
    /**
46
     * @param string $uri
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
47
     *
48
     * @return array
49
     */
50 10
    public function getJson($uri)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $uri
Loading history...
51
    {
52 10
        return $this->send('GET', $uri);
53
    }
54
55
    /**
56
     * @param string $uri
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
57
     * @param array  $data
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
58
     *
59
     * @return array
60
     */
61 1
    public function postJson($uri, array $data)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $uri
Loading history...
62
    {
63 1
        $json = json_encode($data);
64
65 1
        return $this->send('POST', $uri, $json, [
66 1
            'Content-Type' => 'application/json',
67
        ]);
68
    }
69
70
    /**
71
     * @param string $method
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
72
     * @param string $uri
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
73
     * @param string $body
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
74
     * @param array  $headers
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
75
     *
76
     * @return array
77
     */
78 11
    protected function send($method, $uri, $body = null, array $headers = [])
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $method
Loading history...
Coding Style introduced by
Type hint "string" missing for $uri
Loading history...
Coding Style introduced by
Type hint "string" missing for $body
Loading history...
79
    {
80 11
        $headers = $this->compileHeaders($headers);
81
82 11
        $this->client->setOptions([
83 11
            CURLOPT_RETURNTRANSFER => true,
84 11
            CURLOPT_URL => $this->baseUrl.'/'.$uri,
85 11
            CURLOPT_ACCEPT_ENCODING => 'application/json',
86 11
            CURLOPT_CUSTOMREQUEST => $method,
87 11
            CURLOPT_HTTPHEADER => $headers,
88 11
            CURLOPT_USERAGENT => $this->getUserAgentString(),
89
        ]);
90
91 11
        if ($method !== 'GET' && $body) {
92 1
            $this->client->setOption(CURLOPT_POSTFIELDS, $body);
93
        }
94
95 11
        $response = $this->client->exec();
96 11
        $httpcode = $this->client->getInfo(CURLINFO_HTTP_CODE);
97
98 11
        if ($httpcode >= 400) {
99 6
            $this->handleErrors($httpcode, $response);
100
        }
101
102 5
        return json_decode($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        return json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
103
    }
104
105
    /**
106
     * @param array $headers
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
107
     *
108
     * @return array
109
     */
110 11
    protected function compileHeaders(array $headers = [])
111
    {
112
        $headers = [
113 11
                'X-AUTH-KEY' => $this->apiKey,
114 11
            ] + $headers;
115
116 11
        $return = [];
117 11
        foreach ($headers as $key => $value) {
118 11
            $return[] = $key.': '.$value;
119
        }
120
121 11
        return $return;
122
    }
123
124 11
    protected function getUserAgentString()
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
125
    {
126 11
        $platforms = $this->platforms;
127
        array_walk($platforms, function (&$version, $platform) {
128 1
            $version = $platform.'/'.$version;
129 11
        });
130 11
        $platforms = implode(' ', $platforms);
131 11
        $userAgent = sprintf('GloBeePaymentSdk/%s (%s) PHP/%s', PaymentApi::VERSION, PHP_OS, PHP_VERSION);
132
133 11
        return trim($userAgent.' '.$platforms);
134
    }
135
136
    /**
137
     * @param string $url
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
138
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
139 1
    public function setBaseUrl($url)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $url
Loading history...
140
    {
141 1
        $this->baseUrl = $url;
142 1
    }
143
}
144