Completed
Pull Request — master (#7)
by
unknown
01:16
created

NetgsmApiClient::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * @param $method
44
     * @param $url
45
     * @param  null  $params
46
     * @param  array  $headers
47
     * @return string
48
     * @throws GuzzleException
49
     */
50
    protected function callApi($method, $url, $params = null, $headers = [])
51
    {
52
        $options = [
53
            'query' => [
54
                'usercode' => $this->credentials['user_code'],
55
                'password' => $this->credentials['secret'],
56
            ],
57
        ];
58
59
        if ($method == 'POST') {
60
            if (is_array($params)) {
61
                $options['form_params'] = $params;
62
            } else {
63
                $options['body'] = $params;
64
            }
65
        }
66
67
        if ($method == 'GET' && is_array($params)) {
68
            $options['query'] = array_merge($options['query'], $params);
69
        }
70
71
        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...
72
            $options['headers'] = $headers;
73
        }
74
75
        $response = $this->client->request($method, $url, $options);
76
77
        return $response->getBody()->getContents();
78
    }
79
}
80