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 |
||
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)) { |
||
63 | $options['form_params'] = $params; |
||
64 | } else { |
||
65 | $options['body'] = $params; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | if ($method == 'GET' && is_array($params)) { |
||
70 | $options['query'] = array_merge($options['query'], $params); |
||
71 | } |
||
72 | |||
73 | if ($headers) { |
||
0 ignored issues
–
show
|
|||
74 | $options['headers'] = $headers; |
||
75 | } |
||
76 | |||
77 | $response = $this->client->request($method, $url, $options); |
||
78 | |||
79 | return $response->getBody()->getContents(); |
||
80 | } |
||
81 | } |
||
82 |
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.