Passed
Push — master ( 277b3a...5ee2c0 )
by Dani
01:31
created

Postpay::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 \Postpay\Http\Response|null Stores the last request.
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 7
    public function __construct(array $config = [])
75
    {
76 7
        $config = array_merge([
77 7
            'client' => null,
78 7
            'merchant_id' => getenv(static::MERCHANT_ID_ENV_NAME),
79 7
            'secret_key' => getenv(static::SECRET_KEY_ENV_NAME),
80 7
            'api_version' => static::DEFAULT_API_VERSION,
81
            'sandbox' => false,
82 7
        ], $config);
83
84 7
        if ((!$config['merchant_id']) or (!$config['secret_key'])) {
85
            throw new PostpayException('Basic credentials required.');
86
        }
87 7
        $this->client = new Client($config['client'] ?: new GuzzleClient());
88 7
        $this->auth = [$config['merchant_id'], $config['secret_key']];
89 7
        $this->apiVersion = $config['api_version'];
90 7
        $this->sandbox = $config['sandbox'];
91 7
    }
92
93
    /**
94
     * Returns the client service.
95
     *
96
     * @return Client
97
     */
98 1
    public function getClient()
99
    {
100 1
        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 1
    public function get($path, array $params = [])
124
    {
125 1
        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 1
    public function post($path, array $params = [])
139
    {
140 1
        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 1
    public function put($path, array $params = [])
154
    {
155 1
        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 1
    public function patch($path, array $params = [])
169
    {
170 1
        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 1
    public function delete($path, array $params = [])
184
    {
185 1
        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 1
    public function query($query, array $variables = [])
199
    {
200
        $params = [
201 1
            'query' => $query,
202 1
            'variables' => $variables,
203
        ];
204 1
        return $this->request(
205 1
            'POST',
206 1
            self::GRAPHQL_PATH,
207 1
            $params,
208 1
            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 6
    public function request(
225
        $method,
226
        $path,
227
        array $params = [],
228
        $apiVersion = null
229
    ) {
230 6
        $request = new Request(
231 6
            $method,
232 6
            $path,
233 6
            $params,
234 6
            $this->auth,
235 6
            $apiVersion ?: $this->apiVersion,
236 6
            $this->sandbox
237
        );
238 6
        return $this->lastResponse = $this->client->request($request);
239
    }
240
}
241