Completed
Push — master ( 273759...c312d5 )
by Janusz
02:32
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 27
    public function __construct($appKey, $secretKey)
33
    {
34 27
        $this->appKey    = $appKey;
35 27
        $this->secretKey = $secretKey;
36 27
    }
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
        if ($this->validateHttpMethod($method) === false) {
48 1
            throw new \InvalidArgumentException('Invalid method');
49
        }
50
51 4
        $response = $this->getClient()->request($method, self::API_URL.$url, [
52 4
            'auth' => [$this->appKey, $this->secretKey],
53 4
            'form_params' => $data,
54 4
        ]);
55
56 4
        if ($response->getStatusCode() != 200) {
57 1
            throw new InvalidResponseException('Invalid HTTP code');
58
        }
59
60 3
        $bodyJson = (string)$response->getBody();
61
62 3
        return $this->parseJsonData($bodyJson);
63
    }
64
65
    /**
66
     * @param string $json
67
     * @return array
68
     * @throws InvalidResponseException
69
     */
70 3
    private function parseJsonData($json)
71
    {
72
        try {
73 3
            $bodyArr = \GuzzleHttp\json_decode($json, true);
74 3
        } catch (\InvalidArgumentException $e) {
75 1
            throw new InvalidResponseException('Invalid JSON data');
76
        }
77
78 2
        if (!array_key_exists('data', $bodyArr)) {
79 1
            throw new InvalidResponseException('Missing data array key in response');
80
        }
81
82 1
        return $bodyArr['data'];
83
    }
84
85
    /**
86
     * @param string $method
87
     * @return bool
88
     */
89 5
    private function validateHttpMethod($method)
90
    {
91 5
        return in_array($method, ['POST', 'PUT', 'GET', 'DELETE']);
92
    }
93
94
    /**
95
     * @return Client
96
     */
97 5
    public function getClient()
98
    {
99 5
        if ($this->client === null) {
100 1
            $this->client = new Client();
101 1
        }
102
103 5
        return $this->client;
104
    }
105
106
    /**
107
     * @param Client $client
108
     */
109 4
    public function setClient(Client $client)
110
    {
111 4
        $this->client = $client;
112 4
    }
113
114
    /**
115
     * @return string
116
     */
117 3
    public function getAppKey()
118
    {
119 3
        return $this->appKey;
120
    }
121
122
    /**
123
     * @param string $appKey
124
     * @return HttpClient $this
125
     */
126 1
    public function setAppKey($appKey)
127
    {
128 1
        $this->appKey = $appKey;
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 3
    public function getSecretKey()
137
    {
138 3
        return $this->secretKey;
139
    }
140
141
    /**
142
     * @param string $secretKey
143
     * @return HttpClient $this
144
     */
145 1
    public function setSecretKey($secretKey)
146
    {
147 1
        $this->secretKey = $secretKey;
148
149 1
        return $this;
150
    }
151
}
152