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