1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\SmsIntel\Api\Requests; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use seregazhuk\SmsIntel\Exceptions\BaseSmsIntelException; |
7
|
|
|
|
8
|
|
|
abstract class AbstractRequest |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
public static $allowedMethod = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
protected $guzzle; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $login; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $password; |
29
|
|
|
|
30
|
|
|
public function __construct(ClientInterface $client) |
31
|
|
|
{ |
32
|
|
|
$this->guzzle = $client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $login |
38
|
|
|
* @param string $password |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setCredentials($login, $password) |
42
|
|
|
{ |
43
|
|
|
$this->login = $login; |
44
|
|
|
$this->password = $password; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Make the request to API |
51
|
|
|
* |
52
|
|
|
* @param string $action |
53
|
|
|
* @param array $params |
54
|
|
|
* @param string $method |
55
|
|
|
* @return array|null |
56
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
57
|
|
|
*/ |
58
|
|
|
public function exec($action, $params = [], $method = 'GET') |
59
|
|
|
{ |
60
|
|
|
$endPoint = $this->makeEndPoint($action); |
61
|
|
|
$requestBody = $this->createRequestBody($params); |
62
|
|
|
|
63
|
|
|
$options = $this->makeOptions($method, $requestBody); |
64
|
|
|
|
65
|
|
|
$response = $this->guzzle->request($method, $endPoint, $options); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$respArr = $this->parseResponse($response->getBody()->getContents()); |
68
|
|
|
|
69
|
|
|
if (is_array($respArr) && array_key_exists('error_descr', $respArr)) { |
70
|
|
|
$respArr = array_merge(['code' => 0, 'descr' => '', 'error_descr' => []], $respArr); // screw the notices! |
71
|
|
|
throw new BaseSmsIntelException($respArr['descr'], $respArr['code'], $respArr['error_descr']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $respArr; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $params |
79
|
|
|
* @return string|array |
80
|
|
|
*/ |
81
|
|
|
protected function createRequestBody(array $params) |
82
|
|
|
{ |
83
|
|
|
$params = array_merge( |
84
|
|
|
[ |
85
|
|
|
'login' => $this->login, |
86
|
|
|
'password' => $this->password, |
87
|
|
|
], |
88
|
|
|
$params |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
ksort($params); |
92
|
|
|
|
93
|
|
|
return $this->formatRequestBody($params); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $method |
98
|
|
|
* @param string|array $requestBody |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
private function makeOptions($method, $requestBody) |
102
|
|
|
{ |
103
|
|
|
switch (strtoupper($method)) { |
104
|
|
|
case 'GET': |
105
|
|
|
return ['query' => $requestBody]; |
106
|
|
|
case 'POST': |
107
|
|
|
$body = is_array($requestBody) ? \GuzzleHttp\json_encode($requestBody) : $requestBody; |
108
|
|
|
return ['body' => $body]; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $requestBody |
115
|
|
|
* @return string|array |
116
|
|
|
*/ |
117
|
|
|
abstract protected function formatRequestBody(array $requestBody); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $response |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
abstract protected function parseResponse($response); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $action |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
abstract protected function makeEndPoint($action); |
130
|
|
|
} |
131
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.