Completed
Push — master ( 4557f7...a13687 )
by Janusz
02:26 queued 12s
created

HttpClient::getAppKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 20
    public function __construct($appKey, $secretKey)
33
    {
34 20
        $this->appKey    = $appKey;
35 20
        $this->secretKey = $secretKey;
36 20
    }
37
38
    /**
39
     * @param string $url
40
     * @param array $data
41
     * @param string $method
42
     * @return array
43
     * @throws InvalidResponseException
44
     */
45 5
    public function sendRequest($url, $data = null, $method = 'GET')
46
    {
47 5
        $this->validateMethod($method);
48
49 4
        $response = $this->getClient()->request($method, self::API_URL.$url, [
50 4
            'auth' => [$this->appKey, $this->secretKey],
51 4
            'form_params' => $data,
52 4
        ]);
53
54 4
        if ($response->getStatusCode() != 200) {
55 1
            throw new InvalidResponseException('Invalid HTTP code');
56
        }
57
58 3
        $bodyJson = (string)$response->getBody();
59
60
        try {
61 3
            $bodyArr = \GuzzleHttp\json_decode($bodyJson, true);
62 3
        } catch (\InvalidArgumentException $e) {
63 1
            throw new InvalidResponseException('Invalid JSON data');
64
        }
65
66 2
        if (!array_key_exists('data', $bodyArr)) {
67 1
            throw new InvalidResponseException('Missing data property in response');
68
        }
69
70 1
        return $bodyArr['data'];
71
    }
72
73
    /**
74
     * @param string $method
75
     * @return bool
76
     */
77 5
    private function validateMethod($method)
78
    {
79 5
        if (!in_array($method, ['POST', 'PUT', 'GET', 'DELETE'])) {
80 1
            throw new \InvalidArgumentException('Invalid method');
81
        }
82
83 4
        return true;
84
    }
85
86
    /**
87
     * @return Client
88
     */
89 5
    public function getClient()
90
    {
91 5
        if ($this->client === null) {
92 1
            $this->client = new Client();
93 1
        }
94
95 5
        return $this->client;
96
    }
97
98
    /**
99
     * @param Client $client
100
     */
101 4
    public function setClient(Client $client)
102
    {
103 4
        $this->client = $client;
104 4
    }
105
106
    /**
107
     * @return string
108
     */
109 3
    public function getAppKey()
110
    {
111 3
        return $this->appKey;
112
    }
113
114
    /**
115
     * @param string $appKey
116
     */
117 1
    public function setAppKey($appKey)
118
    {
119 1
        $this->appKey = $appKey;
120 1
    }
121
122
    /**
123
     * @return string
124
     */
125 3
    public function getSecretKey()
126
    {
127 3
        return $this->secretKey;
128
    }
129
130
    /**
131
     * @param string $secretKey
132
     */
133 1
    public function setSecretKey($secretKey)
134
    {
135 1
        $this->secretKey = $secretKey;
136 1
    }
137
}
138