Completed
Branch master (bc4001)
by Dani
01:13
created

src/Postpay.php (2 issues)

1
<?php
2
3
namespace Postpay;
4
5
use Postpay\Exceptions\PostpayException;
6
use Postpay\Http\Request;
7
use Postpay\HttpClients\Client;
8
use Postpay\HttpClients\GuzzleClient;
9
10
class Postpay
11
{
12
    /**
13
     * @const string Version number of the Postpay SDK.
14
     */
15
    const VERSION = '0.0.1';
16
17
    /**
18
     * @const string Default API version for requests.
19
     */
20
    const DEFAULT_API_VERSION = 'v1';
21
22
    /**
23
     * @const string GraphQL path.
24
     */
25
    const GRAPHQL_PATH = '/graphql';
26
27
    /**
28
     * @const string GraphQL version for requests.
29
     */
30
    const GRAPHQL_VERSION = 'graphql';
31
32
    /**
33
     * @const string The name of the environment variable that contains the merchant ID.
34
     */
35
    const MERCHANT_ID_ENV_NAME = 'POSTPAY_MERCHANT_ID';
36
37
    /**
38
     * @const string The name of the environment variable that contains the secret key.
39
     */
40
    const SECRET_KEY_ENV_NAME = 'POSTPAY_SECRET_KEY';
41
42
    /**
43
     * @var Client The Postpay client service.
44
     */
45
    protected $client;
46
47
    /**
48
     * @var array The basic auth credentials.
49
     */
50
    protected $auth;
51
52
    /**
53
     * @var string|null The default API version.
54
     */
55
    protected $apiVersion;
56
57
    /**
58
     * @var bool Set to true for sandbox requests.
59
     */
60
    protected $sandbox;
61
62
    /**
63
     * @var Response|null Stores the last request.
0 ignored issues
show
The type Postpay\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     */
65
    protected $lastResponse;
66
67
    /**
68
     * Instantiates a new Postpay super-class object.
69
     *
70
     * @param array $config
71
     *
72
     * @throws PostpayException
73
     */
74
    public function __construct(array $config = [])
75
    {
76
        $config = array_merge([
77
            'client' => null,
78
            'merchant_id' => getenv(static::MERCHANT_ID_ENV_NAME),
79
            'secret_key' => getenv(static::SECRET_KEY_ENV_NAME),
80
            'api_version' => static::DEFAULT_API_VERSION,
81
            'sandbox' => false,
82
        ], $config);
83
84
        if ((!$config['merchant_id']) or (!$config['secret_key'])) {
85
            throw new PostpayException('Basic credentials required.');
86
        }
87
        $this->client = new Client($config['client'] ?: new GuzzleClient());
88
        $this->auth = [$config['merchant_id'], $config['secret_key']];
89
        $this->apiVersion = $config['api_version'];
90
        $this->sandbox = $config['sandbox'];
91
    }
92
93
    /**
94
     * Returns the client service.
95
     *
96
     * @return Client
97
     */
98
    public function getClient()
99
    {
100
        return $this->client;
101
    }
102
103
    /**
104
     * Returns the last response returned from API.
105
     *
106
     * @return \Postpay\Http\Response|null
107
     */
108
    public function getLastResponse()
109
    {
110
        return $this->lastResponse;
111
    }
112
113
    /**
114
     * Sends a GET request to API and returns the response.
115
     *
116
     * @param string $path
117
     * @param array  $params
118
     *
119
     * @return \Postpay\Http\Response
120
     *
121
     * @throws PostpayException
122
     */
123
    public function get($path, array $params = [])
124
    {
125
        return $this->request('GET', $path, $params);
126
    }
127
128
    /**
129
     * Sends a POST request to API and returns the response.
130
     *
131
     * @param string $path
132
     * @param array  $params
133
     *
134
     * @return \Postpay\Http\Response
135
     *
136
     * @throws PostpayException
137
     */
138
    public function post($path, array $params = [])
139
    {
140
        return $this->request('POST', $path, $params);
141
    }
142
143
    /**
144
     * Sends a PUT request to API and returns the response.
145
     *
146
     * @param string $path
147
     * @param array  $params
148
     *
149
     * @return \Postpay\Http\Response
150
     *
151
     * @throws PostpayException
152
     */
153
    public function put($path, array $params = [])
154
    {
155
        return $this->request('PUT', $path, $params);
156
    }
157
158
    /**
159
     * Sends a PATCH request to API and returns the response.
160
     *
161
     * @param string $path
162
     * @param array  $params
163
     *
164
     * @return \Postpay\Http\Response
165
     *
166
     * @throws PostpayException
167
     */
168
    public function patch($path, array $params = [])
169
    {
170
        return $this->request('PATCH', $path, $params);
171
    }
172
173
    /**
174
     * Sends a DELETE request to API and returns the response.
175
     *
176
     * @param string $path
177
     * @param array  $params
178
     *
179
     * @return \Postpay\Http\Response
180
     *
181
     * @throws PostpayException
182
     */
183
    public function delete($path, array $params = [])
184
    {
185
        return $this->request('DELETE', $path, $params);
186
    }
187
188
    /**
189
     * Sends a query to GraphQL API and returns the response.
190
     *
191
     * @param string $query
192
     * @param array  $variables
193
     *
194
     * @return \Postpay\Http\Response
195
     *
196
     * @throws PostpayException
197
     */
198
    public function query($query, array $variables = [])
199
    {
200
        $params = [
201
            'query' => $query,
202
            'variables' => $variables,
203
        ];
204
        return $this->request(
205
            'POST',
206
            self::GRAPHQL_PATH,
207
            $params,
208
            self::GRAPHQL_VERSION
209
        );
210
    }
211
212
    /**
213
     * Sends a request to API and returns the response.
214
     *
215
     * @param string      $method
216
     * @param string      $path
217
     * @param array       $params
218
     * @param string|null $apiVersion
219
     *
220
     * @return \Postpay\Http\Response
221
     *
222
     * @throws PostpayException
223
     */
224
    public function request(
225
        $method,
226
        $path,
227
        array $params = [],
228
        $apiVersion = null
229
    ) {
230
        $request = new Request(
231
            $method,
232
            $path,
233
            $params,
234
            $this->auth,
235
            $apiVersion ?: $this->apiVersion,
236
            $this->sandbox
237
        );
238
        return $this->lastResponse = $this->client->request($request);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client->request($request) of type Postpay\Http\Response is incompatible with the declared type Postpay\Response|null of property $lastResponse.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

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

Loading history...
239
    }
240
}
241