1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediumart\Orange\SMS\Http; |
4
|
|
|
|
5
|
|
|
use Mediumart\Orange\SMS\Http\Requests\AuthorizationRequest; |
6
|
|
|
|
7
|
|
|
class SMSClient |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Access token. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $token; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Expires time. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $expiresIn; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SMSCLient singleton instance. |
25
|
|
|
* |
26
|
|
|
* @var static |
27
|
|
|
*/ |
28
|
|
|
protected static $instance; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* SMSClient constructor. |
32
|
|
|
* |
33
|
|
|
* @throws \Error |
34
|
|
|
*/ |
35
|
1 |
|
protected function __construct() |
36
|
|
|
{ |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the access token. |
41
|
|
|
* |
42
|
|
|
* @param $token |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
12 |
|
public function setToken($token) |
46
|
|
|
{ |
47
|
12 |
|
$this->token = $token; |
48
|
|
|
|
49
|
12 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the access token. |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
10 |
|
public function getToken() |
58
|
|
|
{ |
59
|
10 |
|
return $this->token; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the expires_in in seconds |
64
|
|
|
* |
65
|
|
|
* @param $expiresIn |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
12 |
|
public function setTokenExpiresIn($expiresIn) |
69
|
|
|
{ |
70
|
12 |
|
$this->expiresIn = $expiresIn; |
71
|
|
|
|
72
|
12 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the expire_in in seconds |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getTokenExpiresIn() |
81
|
|
|
{ |
82
|
3 |
|
return $this->expiresIn; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Configure the instance. |
87
|
|
|
* |
88
|
|
|
* @param array $options |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
13 |
|
public function configure() |
92
|
|
|
{ |
93
|
13 |
|
switch (count($options = func_get_args())) { |
94
|
13 |
|
case 0: |
95
|
2 |
|
break; |
96
|
|
|
|
97
|
13 |
|
case 1: |
98
|
11 |
|
$this->configureInstance($options[0]); |
99
|
11 |
|
break; |
100
|
|
|
|
101
|
2 |
|
case 2: |
102
|
1 |
|
$this->configureInstanceAssoc( |
103
|
1 |
|
static::authorize($options[0], $options[1]) |
104
|
|
|
); |
105
|
1 |
|
break; |
106
|
|
|
|
107
|
|
|
default: |
108
|
1 |
|
throw new \InvalidArgumentException('invalid argument count'); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
|
112
|
12 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Configure instance using options. |
117
|
|
|
* |
118
|
|
|
* @param mixed $options |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
11 |
|
protected function configureInstance($options) |
122
|
|
|
{ |
123
|
11 |
|
if (is_string($options)) { |
124
|
10 |
|
$this->setToken($options)->setTokenExpiresIn(null); |
125
|
2 |
|
} elseif (is_array($options)) { |
126
|
2 |
|
$this->configureInstanceAssoc($options); |
127
|
|
|
} |
128
|
11 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Configure instance using assoc array options. |
132
|
|
|
* |
133
|
|
|
* @param array $options |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
3 |
|
protected function configureInstanceAssoc(array $options) |
137
|
|
|
{ |
138
|
3 |
|
if (array_key_exists('access_token', $options)) { |
139
|
3 |
|
$this->setToken($options['access_token']); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
if (array_key_exists('expires_in', $options)) { |
143
|
3 |
|
$this->setTokenExpiresIn($options['expires_in']); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Execute a request against the Api server |
151
|
|
|
* |
152
|
|
|
* @param SMSClientRequest $request |
153
|
|
|
* @param bool $decodeJson |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
7 |
|
public function executeRequest(SMSClientRequest $request, $decodeJson = true) |
157
|
|
|
{ |
158
|
7 |
|
$options = $request->options(); |
159
|
|
|
|
160
|
7 |
|
if (! isset($options['headers']["Authorization"])) { |
161
|
7 |
|
$options['headers']["Authorization"] = "Bearer ". $this->getToken(); |
162
|
|
|
} |
163
|
|
|
|
164
|
7 |
|
$response = $request->execute($options)->getBody(); |
165
|
|
|
|
166
|
7 |
|
return $decodeJson ? json_decode($response, true) : $response; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the client access token |
171
|
|
|
* |
172
|
|
|
* @param $clientID |
173
|
|
|
* @param $clientSecret |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
2 |
|
public static function authorize($clientID, $clientSecret) |
177
|
|
|
{ |
178
|
2 |
|
return json_decode( |
179
|
2 |
|
(new AuthorizationRequest($clientID, $clientSecret))->execute()->getBody(), true |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the prepared singleton instance of the client. |
185
|
|
|
* |
186
|
|
|
* @return SMSClient |
187
|
|
|
*/ |
188
|
13 |
|
public static function getInstance() |
189
|
|
|
{ |
190
|
13 |
|
if (! static::$instance) { |
191
|
1 |
|
static::$instance = new static(); |
192
|
|
|
} |
193
|
|
|
|
194
|
13 |
|
return static::$instance->configure(...func_get_args()); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|