1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\SmsIntel\Api\Requests; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use seregazhuk\SmsIntel\Api\GuzzleHttpClient; |
8
|
|
|
use seregazhuk\SmsIntel\Exceptions\WrongRequest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method send(string|array $phoneNumber, string $from, string $message) To send message to one phone number |
12
|
|
|
* @method getGroups |
13
|
|
|
* @method editGroup |
14
|
|
|
* @method addContact |
15
|
|
|
* @method getContacts |
16
|
|
|
* @method createGroup |
17
|
|
|
* @method getPhoneInfo |
18
|
|
|
* @method requestSource |
19
|
|
|
* @method removeContact |
20
|
|
|
* |
21
|
|
|
* @method cancel(int $smsId) Cancel sms by id |
22
|
|
|
* @method getBalance |
23
|
|
|
* @method checkCoupon |
24
|
|
|
* @method getReportBySms |
25
|
|
|
* @method getReportBySource |
26
|
|
|
* @method getReportByNumber |
27
|
|
|
*/ |
28
|
|
|
class RequestsContainer |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Client |
32
|
|
|
*/ |
33
|
|
|
protected $http; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $login; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $password; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Request[] |
47
|
|
|
*/ |
48
|
|
|
protected $requests = []; |
49
|
|
|
|
50
|
|
|
public function __construct(GuzzleHttpClient $http, $login, $password) |
51
|
|
|
{ |
52
|
|
|
$this->http = $http; |
|
|
|
|
53
|
|
|
$this->login = $login; |
54
|
|
|
$this->password = $password; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function getRequestsActionsMap() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
XMLRequest::class => XMLRequest::$allowedMethods, |
64
|
|
|
JSONRequest::class => JSONRequest::$allowedMethods, |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Proxies all methods to the appropriate Request object |
70
|
|
|
* |
71
|
|
|
* @param string $method |
72
|
|
|
* @param array $arguments |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function __call($method, $arguments) |
76
|
|
|
{ |
77
|
|
|
$request = $this->resolveRequestByAction($method); |
78
|
|
|
|
79
|
|
|
return $request->$method(...$arguments); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets request object by name. If there is no such request |
84
|
|
|
* in requests array, it will try to create it, then save |
85
|
|
|
* it, and then return. |
86
|
|
|
* |
87
|
|
|
* @param string $requestClass |
88
|
|
|
* |
89
|
|
|
* @throws WrongRequest |
90
|
|
|
* |
91
|
|
|
* @return Request |
92
|
|
|
*/ |
93
|
|
|
public function getRequest($requestClass) |
94
|
|
|
{ |
95
|
|
|
// Check if an instance has already been initiated |
96
|
|
|
if (!isset($this->requests[$requestClass])) { |
97
|
|
|
$this->addRequest($requestClass); |
98
|
|
|
} |
99
|
|
|
return $this->requests[$requestClass]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $action |
104
|
|
|
* @return string |
105
|
|
|
* @throws WrongRequest |
106
|
|
|
*/ |
107
|
|
|
public function resolveRequestByAction($action) |
108
|
|
|
{ |
109
|
|
|
foreach ($this->getRequestsActionsMap() as $requestClass => $actions) { |
110
|
|
|
if(in_array($action, $actions)) { |
111
|
|
|
return $this->getRequest($requestClass); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
throw new WrongRequest("Action $action doesn't exist!"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates request by class name, and if success saves |
120
|
|
|
* it to requests array. |
121
|
|
|
* |
122
|
|
|
* @param string $requestClass |
123
|
|
|
* |
124
|
|
|
* @throws WrongRequest |
125
|
|
|
*/ |
126
|
|
|
protected function addRequest($requestClass) |
127
|
|
|
{ |
128
|
|
|
if (!class_exists($requestClass)) { |
129
|
|
|
throw new WrongRequest("Request $requestClass not found."); |
130
|
|
|
} |
131
|
|
|
$this->requests[$requestClass] = $this->buildRequest($requestClass); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Build RequestInterface object with reflection API. |
136
|
|
|
* |
137
|
|
|
* @param string $className |
138
|
|
|
* |
139
|
|
|
* @return object |
140
|
|
|
*/ |
141
|
|
|
protected function buildRequest($className) |
142
|
|
|
{ |
143
|
|
|
return (new ReflectionClass($className)) |
144
|
|
|
->newInstanceArgs([$this->http]) |
145
|
|
|
->setCredentials($this->login, $this->password); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..