NetgsmApiClient::setClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\Netgsm;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
8
class NetgsmApiClient
9
{
10
    /**
11
     * @var ClientInterface
12
     */
13
    protected $client;
14
15
    /**
16
     * @var array
17
     */
18
    protected $credentials = [];
19
20
    /**
21
     * @param  mixed  $client
22
     * @return self
23
     */
24
    public function setClient(ClientInterface $client): self
25
    {
26
        $this->client = $client;
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param  array  $credentials
33
     * @return $this
34
     */
35
    public function setCredentials(array $credentials): self
36
    {
37
        $this->credentials = $credentials;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Sends requests to netgsm api endpoints with specified credentials.
44
     *
45
     * @param $method
46
     * @param $url
47
     * @param  null  $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
48
     * @param  array  $headers
49
     * @return string
50
     * @throws GuzzleException
51
     */
52
    protected function callApi($method, $url, $params = null, $headers = [])
53
    {
54
        $options = [
55
            'query' => [
56
                'usercode' => $this->credentials['user_code'],
57
                'password' => $this->credentials['secret'],
58
            ],
59
        ];
60
61
        if ($method == 'POST') {
62
            if (is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always false.
Loading history...
63
                $options['form_params'] = $params;
64
            } else {
65
                $options['body'] = $params;
66
            }
67
        }
68
69
        if ($method == 'GET' && is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always false.
Loading history...
70
            $options['query'] = array_merge($options['query'], $params);
71
        }
72
73
        if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74
            $options['headers'] = $headers;
75
        }
76
77
        $response = $this->client->request($method, $url, $options);
78
79
        return $response->getBody()->getContents();
80
    }
81
}
82