Completed
Push — master ( bfba9a...5df3d7 )
by Janusz
03:22
created

HttpClient   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 126
ccs 33
cts 39
cp 0.8462
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAppKey() 0 4 1
A setAppKey() 0 4 1
A getSecretKey() 0 4 1
A setSecretKey() 0 4 1
A sendRequest() 0 23 3
A validateMethod() 0 8 2
A getClient() 0 8 2
A setClient() 0 4 1
1
<?php
2
3
namespace Ittoolspl\Smslabs;
4
5
use GuzzleHttp\Client;
6
use Ittoolspl\Smslabs\Exception\InvalidResponseException;
7
8
class HttpClient
9
{
10
    const API_URL = 'https://api.smslabs.net.pl/apiSms';
11
12
    /**
13
     * @var string
14
     */
15
    private $appKey = null;
16
17
    /**
18
     * @var string
19
     */
20
    private $secretKey = null;
21
22
    /**
23
     * @var Client
24
     */
25
    private $client = null;
26
27
    /**
28
     * HttpClient constructor.
29
     * @param string $appKey
30
     * @param string $secretKey
31
     */
32 16
    public function __construct($appKey, $secretKey)
33
    {
34 16
        $this->appKey    = $appKey;
35 16
        $this->secretKey = $secretKey;
36 16
    }
37
38
    /**
39
     * @param string $url
40
     * @param array $data
41
     * @param string $method
42
     * @return array
43
     * @throws InvalidResponseException
44
     */
45 4
    public function sendRequest($url, $data = null, $method = 'GET')
46
    {
47 4
        $this->validateMethod($method);
48
49 3
        $response = $this->getClient()->request($method, self::API_URL.$url, [
50 3
            'auth' => [$this->appKey, $this->secretKey],
51 3
            'form_params' => $data,
52 3
        ]);
53
54 3
        if ($response->getStatusCode() != 200) {
55 1
            throw new InvalidResponseException('Invalid HTTP code');
56
        }
57
58 2
        $bodyJson = (string)$response->getBody();
59
60 2
        $bodyArr = \GuzzleHttp\json_decode($bodyJson, true);
61
62 2
        if (!array_key_exists('data', $bodyArr)) {
63 1
            throw new InvalidResponseException('Missing data property in response');
64
        }
65
66 1
        return $bodyArr['data'];
67
    }
68
69
    /**
70
     * @param string $method
71
     * @return bool
72
     */
73 4
    private function validateMethod($method)
74
    {
75 4
        if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) {
76 1
            throw new \InvalidArgumentException('Invalid method');
77
        }
78
79 3
        return true;
80
    }
81
82
    /**
83
     * @return Client
84
     */
85 4
    public function getClient()
86
    {
87 4
        if ($this->client === null) {
88 1
            $this->client = new Client();
89 1
        }
90
91 4
        return $this->client;
92
    }
93
94
    /**
95
     * @param Client $client
96
     */
97 3
    public function setClient($client)
98
    {
99 3
        $this->client = $client;
100 3
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    public function getAppKey()
106
    {
107 1
        return $this->appKey;
108
    }
109
110
    /**
111
     * @param string $appKey
112
     */
113
    public function setAppKey(string $appKey)
114
    {
115
        $this->appKey = $appKey;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 1
    public function getSecretKey()
122
    {
123 1
        return $this->secretKey;
124
    }
125
126
    /**
127
     * @param string $secretKey
128
     */
129
    public function setSecretKey(string $secretKey)
130
    {
131
        $this->secretKey = $secretKey;
132
    }
133
}
134