Completed
Push — master ( 544b12...8f0eb0 )
by Jacques
02:05
created

Client::purchaseProduct()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2.0469

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
ccs 17
cts 22
cp 0.7727
rs 8.8571
cc 2
eloc 22
nc 3
nop 7
crap 2.0469
1
<?php
2
/**
3
 * Client for Smartcall's Restful Proxy.
4
 *
5
 * @author    Jacques Marneweck <[email protected]>
6
 * @copyright 2016 Jacques Marneweck.  All rights strictly reserved.
7
 * @license   MIT
8
 */
9
10
namespace Jacques\SmartCallProxy;
11
12
class Client extends \GuzzleHttp\Client
13
{
14
    /**
15
     * @const string Version number
16
     */
17
    const VERSION = '0.0.1';
18
19
    /**
20
     * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost
21
     * (127.0.0.1).
22
     *
23
     * @var array[]
24
     */
25
    protected $options = [
26
        'scheme'   => 'http',
27
        'hostname' => 'localhost',
28
        'port'     => '8080',
29
    ];
30
31
    /**
32
     * @param   $options array
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36 2
    public function __construct($options = [])
37
    {
38
        /*
39
         * Allow on instantiation to overwrite the defaults
40
         */
41 2
        $this->options = array_merge(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->options, $options) of type array is incompatible with the declared type array<integer,array> of property $options.

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...
42 2
            $this->options,
43
            $options
44 2
        );
45
46
        $config = [
47 2
            'base_uri' => sprintf(
48 2
                '%s://%s:%s/',
49 2
                $this->options['scheme'],
50 2
                $this->options['hostname'],
51 2
                $this->options['port']
52 2
            ),
53
            'headers' => [
54 2
                'User-Agent' => 'SmartcallRestfulProxyClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(),
55 2
            ],
56 2
        ];
57
58 2
        parent::__construct($config);
59 2
    }
60
61
    /**
62
     * Fetches the Dealer Balance from SmartCall.
63
     *
64
     * @throws Exception
65
     *
66
     * @return array
67
     */
68 1 View Code Duplication
    public function getDealerBalance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        try {
71 1
            $response = $this->get('/SmartcallRestfulProxy/balance');
72
73
            return [
74 1
                'status'    => 'ok',
75 1
                'http_code' => $response->getStatusCode(),
76 1
                'body'      => (string) $response->getBody(),
77 1
            ];
78
        } catch (\GuzzleHttp\Exception\ServerException $e) {
79
            return [
80
                'status'    => 'error',
81
                'http_code' => $e->getResponse()->getStatusCode(),
82
                'body'      => (string) $e->getResponse()->getBody(),
83
            ];
84
        }
85
    }
86
87
    /**
88
     * Fetches the Dealer Balance from SmartCall.
89
     *
90
     * @throws Exception
91
     *
92
     * @return array
93
     */
94 View Code Duplication
    public function isDealerRegistered($msisdn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        try {
97
            $response = $this->get(
98
                sprintf(
99
                    '/SmartcallRestfulProxy/registered/%s',
100
                    $msisdn
101
                )
102
            );
103
104
            return [
105
                'status'    => 'ok',
106
                'http_code' => $response->getStatusCode(),
107
                'body'      => (string) $response->getBody(),
108
            ];
109
        } catch (\GuzzleHttp\Exception\ServerException $e) {
110
            return [
111
                'status'    => 'error',
112
                'http_code' => $e->getResponse()->getStatusCode(),
113
                'body'      => (string) $e->getResponse()->getBody(),
114
            ];
115
        }
116
    }
117
118
    /**
119
     * Fetches the Product from SmartCall.
120
     *
121
     * @param int $productId Product Identifier
122
     *
123
     * @throws Exception
124
     *
125
     * @return array
126
     */
127
    public function getProduct($productId)
128
    {
129
        try {
130
            $response = $this->get(
131
                sprintf(
132
                    '/SmartcallRestfulProxy/product_js/%d',
133
                    $productId
134
                )
135
            );
136
137
            return [
138
                'status'    => 'ok',
139
                'http_code' => $response->getStatusCode(),
140
                'body'      => (string) $response->getBody(),
141
            ];
142
        } catch (\GuzzleHttp\Exception\ServerException $e) {
143
            $body = (string) $e->getResponse()->getBody();
144
145
            preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches);
146
147
            return [
148
                'status'    => 'error',
149
                'http_code' => $e->getResponse()->getStatusCode(),
150
                'body'      => $matches['1'],
151
            ];
152
        }
153
    }
154
155
    /**
156
     * Fetches the Product List from SmartCall.
157
     *
158
     * @throws Exception
159
     *
160
     * @return array
161
     */
162 1 View Code Duplication
    public function getProducts()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        try {
165 1
            $response = $this->get('/SmartcallRestfulProxy/all_networks_js');
166
167
            return [
168 1
                'status'    => 'ok',
169 1
                'http_code' => $response->getStatusCode(),
170 1
                'body'      => (string) $response->getBody(),
171 1
            ];
172
        } catch (\GuzzleHttp\Exception\ServerException $e) {
173
            return [
174
                'status'    => 'error',
175
                'http_code' => $e->getResponse()->getStatusCode(),
176
                'body'      => (string) $e->getResponse()->getBody(),
177
            ];
178
        }
179
    }
180
181
    /**
182
     * Fetches the details of the last transaction processed from SmartCall.
183
     *
184
     * @throws Exception
185
     *
186
     * @return array
187
     */
188 1 View Code Duplication
    public function getLastTransaction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        try {
191 1
            $response = $this->get('/SmartcallRestfulProxy/last_transaction_js');
192
193
            return [
194 1
                'status'    => 'ok',
195 1
                'http_code' => $response->getStatusCode(),
196 1
                'body'      => (string) $response->getBody(),
197 1
            ];
198
        } catch (\GuzzleHttp\Exception\ServerException $e) {
199
            return [
200
                'status'    => 'error',
201
                'http_code' => $e->getResponse()->getStatusCode(),
202
                'body'      => (string) $e->getResponse()->getBody(),
203
            ];
204
        }
205
    }
206
207
    /**
208
     * Fetches the Product List by the specified network identifier from SmartCall.
209
     *
210
     * @param int $networkId identifier for the network
211
     *
212
     * @throws Exception
213
     *
214
     * @return array
215
     */
216 View Code Duplication
    public function getProductsByNetwork($networkId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        try {
219
            $response = $this->get(
220
                sprintf(
221
                    '/SmartcallRestfulProxy/network_js/%d',
222
                    $networkId
223
                )
224
            );
225
226
            return [
227
                'status'    => 'ok',
228
                'http_code' => $response->getStatusCode(),
229
                'body'      => (string) $response->getBody(),
230
            ];
231
        } catch (\GuzzleHttp\Exception\ServerException $e) {
232
            return [
233
                'status'    => 'error',
234
                'http_code' => $e->getResponse()->getStatusCode(),
235
                'body'      => (string) $e->getResponse()->getBody(),
236
            ];
237
        }
238
    }
239
240
    /**
241
     * Purchase a voucher or do a pinless recharge on SmartCall.
242
     *
243
     * @param int $productId identifier for the product
244
     * @param int $amount amount in rands of the product
245
     * @param int $msisdn mobile number of the recipient of the product
246
     * @param int $deviceId mobile number or meter number of the device being recharged
247
     * @param string $clientRef Client Reference (use a UUID)
248
     * @param bool $pinless true = device will be recharged via the network's IN platform | false = pinbased virtual voucher
249
     * @param bool $sendSms true = SmartCall will send the voucher via SMS | false don't send the voucher via SMS
250
     *
251
     * @throws Exception
252
     *
253
     * @return array
254
     */
255 1
    public function purchaseProduct($productId, $amount, $msisdn, $deviceId, $clientRef, $pinless = true, $sendSms = false)
256
    {
257
        try {
258 1
            $response = $this->post(
259 1
                sprintf(
260 1
                    '/SmartcallRestfulProxy/recharge_js/%s',
261
                    $clientRef
262 1
                ),
263
                [
264
                    'json' => [
265 1
                        'amount' => $amount,
266 1
                        'deviceId' => $deviceId,
267 1
                        'pinless' => $pinless,
268 1
                        'productId' => $productId,
269 1
                        'sendSms' => $sendSms,
270 1
                        'smsRecipientMsisdn' => $msisdn,
271 1
                    ],
272
                ]
273 1
            );
274
275
            return [
276 1
                'status'    => 'ok',
277 1
                'http_code' => $response->getStatusCode(),
278 1
                'body'      => (string) $response->getBody(),
279 1
            ];
280
        } catch (\GuzzleHttp\Exception\ServerException $e) {
281
            return [
282
                'status'    => 'error',
283
                'http_code' => $e->getResponse()->getStatusCode(),
284
                'body'      => (string) $e->getResponse()->getBody(),
285
            ];
286
        }
287
    }
288
289
    /**
290
     * Searches SmartCall for a specified transaction using a specified key and string to search
291
     * against at SmartCall.
292
     *
293
     * @param string $field        client_ref | msisdn | order_reference
294
     * @param string $query_string Client Reference when client_ref or a users MSISDN when msisdn
0 ignored issues
show
Bug introduced by
There is no parameter named $query_string. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
295
     *
296
     * @throws Exception
297
     *
298
     * @return array
299
     */
300 3
    public function searchTransaction($field, $search)
301
    {
302
        /**
303
         * Map Smartcall's longer version of the url to shorter param to pass in.
304
         */
305
        $fields = [
306 3
            'client_ref' => 'client_reference',
307 3
            'msisdn'     => 'msisdn',
308 3
            'order_ref'  => 'order_reference',
309 3
        ];
310
311
        try {
312 3
            $response = $this->get(
313 3
                sprintf(
314 3
                    '/SmartcallRestfulProxy/last_transaction_%s_js/%s',
315 3
                    (string) $fields[$field],
316
                    (string) $search
317 3
                )
318 3
            );
319
320
            return [
321 3
                'status'    => 'ok',
322 3
                'http_code' => $response->getStatusCode(),
323 3
                'body'      => (string) $response->getBody(),
324 3
            ];
325
        } catch (\GuzzleHttp\Exception\ServerException $e) {
326
            return [
327
                'status'    => 'error',
328
                'http_code' => $e->getResponse()->getStatusCode(),
329
                'body'      => (string) $e->getResponse()->getBody(),
330
            ];
331
        }
332
    }
333
}
334