|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\SmsIntel\Api\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use seregazhuk\SmsIntel\Contracts\HttpInterface; |
|
7
|
|
|
use seregazhuk\SmsIntel\Contracts\RequestInterface; |
|
8
|
|
|
use seregazhuk\SmsIntel\Exceptions\WrongRequestException; |
|
9
|
|
|
|
|
10
|
|
|
class RequestsContainer |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var HttpInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $http; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $login; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $password; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RequestInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $requests = []; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(HttpInterface $http, $login, $password) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->http = $http; |
|
36
|
|
|
$this->login = $login; |
|
37
|
|
|
$this->password = $password; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function getRequestsActionsMap() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
XMLRequest::class => XMLRequest::$allowedMethods, |
|
47
|
|
|
JSONRequest::class => JSONRequest::$allowedMethods, |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Gets request object by name. If there is no such request |
|
53
|
|
|
* in requests array, it will try to create it, then save |
|
54
|
|
|
* it, and then return. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $requestClass |
|
57
|
|
|
* |
|
58
|
|
|
* @throws WrongRequestException |
|
59
|
|
|
* |
|
60
|
|
|
* @return RequestInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getRequest($requestClass) |
|
63
|
|
|
{ |
|
64
|
|
|
// Check if an instance has already been initiated |
|
65
|
|
|
if (!isset($this->requests[$requestClass])) { |
|
66
|
|
|
$this->addRequest($requestClass); |
|
67
|
|
|
} |
|
68
|
|
|
return $this->requests[$requestClass]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $action |
|
73
|
|
|
* @return string |
|
74
|
|
|
* @throws WrongRequestException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function resolveRequestByAction($action) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->getRequestsActionsMap() as $requestClass => $actions) { |
|
79
|
|
|
if(in_array($action, $actions)) { |
|
80
|
|
|
return $this->getRequest($requestClass); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
throw new WrongRequestException("Action $action doesn't exist!"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates request by class name, and if success saves |
|
89
|
|
|
* it to requests array. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $requestClass |
|
92
|
|
|
* |
|
93
|
|
|
* @throws WrongRequestException |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function addRequest($requestClass) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!class_exists($requestClass)) { |
|
98
|
|
|
throw new WrongRequestException("Request $requestClass not found."); |
|
99
|
|
|
} |
|
100
|
|
|
$this->requests[$requestClass] = $this->buildRequest($requestClass); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Build RequestInterface object with reflection API. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $className |
|
107
|
|
|
* |
|
108
|
|
|
* @return object |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function buildRequest($className) |
|
111
|
|
|
{ |
|
112
|
|
|
return (new ReflectionClass($className)) |
|
113
|
|
|
->newInstanceArgs([$this->http]) |
|
114
|
|
|
->setCredentials($this->login, $this->password); |
|
115
|
|
|
} |
|
116
|
|
|
} |