Completed
Push — master ( fc4337...4557f7 )
by Janusz
02:20
created

HttpClient::validateMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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
        try {
61
            $bodyArr = \GuzzleHttp\json_decode($bodyJson, true);
62 2
63 1
        } catch (\InvalidArgumentException $e) {
64
            throw new InvalidResponseException('Invalid JSON data');
65
        }
66 1
67
        if (!array_key_exists('data', $bodyArr)) {
68
            throw new InvalidResponseException('Missing data property in response');
69
        }
70
71
        return $bodyArr['data'];
72
    }
73 4
74
    /**
75 4
     * @param string $method
76 1
     * @return bool
77
     */
78
    private function validateMethod($method)
79 3
    {
80
        if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) {
81
            throw new \InvalidArgumentException('Invalid method');
82
        }
83
84
        return true;
85 4
    }
86
87 4
    /**
88 1
     * @return Client
89 1
     */
90
    public function getClient()
91 4
    {
92
        if ($this->client === null) {
93
            $this->client = new Client();
94
        }
95
96
        return $this->client;
97 3
    }
98
99 3
    /**
100 3
     * @param Client $client
101
     */
102
    public function setClient(Client $client)
103
    {
104
        $this->client = $client;
105 3
    }
106
107 3
    /**
108
     * @return string
109
     */
110
    public function getAppKey()
111
    {
112
        return $this->appKey;
113 1
    }
114
115 1
    /**
116 1
     * @param string $appKey
117
     */
118
    public function setAppKey($appKey)
119
    {
120
        $this->appKey = $appKey;
121 3
    }
122
123 3
    /**
124
     * @return string
125
     */
126
    public function getSecretKey()
127
    {
128
        return $this->secretKey;
129 1
    }
130
131 1
    /**
132 1
     * @param string $secretKey
133
     */
134
    public function setSecretKey($secretKey)
135
    {
136
        $this->secretKey = $secretKey;
137
    }
138
}
139