Passed
Push — master ( 5319ee...e6b641 )
by Dani
02:13
created

src/Http/Request.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Postpay\Http;
4
5
use Postpay\Postpay;
6
7
class Request
8
{
9
    /**
10
     * @const string API URL.
11
     */
12
    const API_URL = 'https://<subdomain>.postpay.io';
13
14
    /**
15
     * @const string Accept HTTP header.
16
     */
17
    const ACCEPT_HEADER = 'application/vnd.postpay+json; version=';
18
19
    /**
20
     * @var string The HTTP method.
21
     */
22
    protected $method;
23
24
    /**
25
     * @var string The path URL.
26
     */
27
    protected $path;
28
29
    /**
30
     * @var array The basic auth credentials.
31
     */
32
    protected $auth;
33
34
    /**
35
     * @var array The headers to send with the request.
36
     */
37
    protected $headers = [];
38
39
    /**
40
     * @var array The parameters to send with the request.
41
     */
42
    protected $params = [];
43
44
    /**
45
     * @var string API version.
46
     */
47
    protected $apiVersion;
48
49
    /**
50
     * @var bool Set to true for sandbox request.
51
     */
52
    protected $sandbox;
53
54
    /**
55
     * Creates a new Request entity.
56
     *
57
     * @param string|null $method
58
     * @param string|null $path
59
     * @param array|null  $params
60
     * @param array|null  $auth
61
     * @param bool        $sandbox
62
     */
63 7
    public function __construct(
64
        $method = null,
65
        $path = null,
66
        array $params = [],
67
        $auth = null,
68
        $apiVersion = null,
69
        $sandbox = false
70
    ) {
71 7
        $this->auth = $auth;
0 ignored issues
show
Documentation Bug introduced by
It seems like $auth can be null. However, the property $auth is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
72 7
        $this->setMethod($method);
73 7
        $this->path = $path;
74 7
        $this->params = $params;
75 7
        $this->apiVersion = $apiVersion ?: Postpay::DEFAULT_API_VERSION;
76 7
        $this->sandbox = $sandbox;
77 7
    }
78
79
    /**
80
     * Return the basic auth credentials.
81
     *
82
     * @return array|null
83
     */
84
    public function getAuth()
85
    {
86
        return $this->auth;
87
    }
88
89
    /**
90
     * Return the HTTP method.
91
     *
92
     * @return string
93
     */
94 6
    public function getMethod()
95
    {
96 6
        return $this->method;
97
    }
98
99
    /**
100
     * Set the HTTP method.
101
     *
102
     * @param string
103
     */
104 7
    public function setMethod($method)
105
    {
106 7
        $this->method = strtoupper($method);
107 7
    }
108
109
    /**
110
     * Return the path URL.
111
     *
112
     * @return string
113
     */
114
    public function getPath()
115
    {
116
        return $this->path;
117
    }
118
119
    /**
120
     * Generate and return HTTP headers.
121
     *
122
     * @return array
123
     */
124
    public function getHeaders()
125
    {
126
        return array_merge($this->headers, $this->getDefaultHeaders());
127
    }
128
129
    /**
130
     * Set the headers.
131
     *
132
     * @param array $headers
133
     */
134
    public function setHeaders(array $headers)
135
    {
136
        $this->headers = array_merge($this->headers, $headers);
137
    }
138
139
    /**
140
     * Return the default headers.
141
     *
142
     * @return array
143
     */
144
    public function getDefaultHeaders()
145
    {
146
        return [
147
            'Accept-Encoding' => '*',
148
            'Accept' => static::ACCEPT_HEADER . $this->apiVersion,
149
            'Content-Type' => 'application/json',
150
            'User-Agent' => 'postpay-php:' . Postpay::VERSION,
151
        ];
152
    }
153
154
    /**
155
     * Return the params.
156
     *
157
     * @return array
158
     */
159
    public function getParams()
160
    {
161
        return $this->params;
162
    }
163
164
    /**
165
     * Return JSON params on POST requests.
166
     *
167
     * @return array
168
     */
169
    public function json()
170
    {
171
        if ($this->method !== 'GET') {
172
            return $this->params;
173
        }
174
        return [];
175
    }
176
177
    /**
178
     * The API version.
179
     *
180
     * @return string
181
     */
182
    public function getApiVersion()
183
    {
184
        return $this->apiVersion;
185
    }
186
187
    /**
188
     * Return true if it's a GraphQL request.
189
     *
190
     * @return bool
191
     */
192 7
    public function isGraphQL()
193
    {
194 7
        return $this->apiVersion === Postpay::GRAPHQL_VERSION;
195
    }
196
197
    public function getApiUrl()
198
    {
199
        $subdomain = $this->sandbox ? 'sandbox' : 'api';
200
        return str_replace('<subdomain>', $subdomain, static::API_URL);
201
    }
202
203
    /**
204
     * Generate and return the URL.
205
     *
206
     * @return string
207
     */
208
    public function getUrl()
209
    {
210
        $path = Url::slashPrefix($this->path);
211
212
        if ($this->method === 'GET') {
213
            $path = Url::addParamsToUrl($path, $this->params);
214
        }
215
        return $this->getApiUrl() . $path;
216
    }
217
}
218