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(...$options) |
92
|
|
|
{ |
93
|
13 |
|
if (count($options) <= 0) { |
94
|
2 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
13 |
|
switch (count($options)) { |
98
|
13 |
|
case 1: |
99
|
11 |
|
if (is_string($options[0])) { |
100
|
10 |
|
$this->setTokenExpiresIn(null)->setToken($options[0]); |
101
|
2 |
|
} elseif (is_array($options[0])) { |
102
|
2 |
|
$this->configureArrayOptions($options[0]); |
103
|
|
|
} |
104
|
11 |
|
break; |
105
|
|
|
|
106
|
2 |
|
case 2: |
107
|
1 |
|
$this->configureArrayOptions( |
108
|
1 |
|
static::authorize($options[0], $options[1]) |
109
|
|
|
); |
110
|
1 |
|
break; |
111
|
|
|
|
112
|
|
|
default: |
113
|
1 |
|
throw new \InvalidArgumentException('invalid argument count'); |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
|
117
|
12 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Configure instance using array options. |
122
|
|
|
* |
123
|
|
|
* @param array $options |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
3 |
|
protected function configureArrayOptions(array $options) |
127
|
|
|
{ |
128
|
3 |
|
if (array_key_exists('access_token', $options)) { |
129
|
3 |
|
$this->setToken($options['access_token']); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
if (array_key_exists('expires_in', $options)) { |
133
|
3 |
|
$this->setTokenExpiresIn($options['expires_in']); |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Execute a request against the Api server |
141
|
|
|
* |
142
|
|
|
* @param SMSClientRequest $request |
143
|
|
|
* @param bool $decodeJson |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
7 |
|
public function executeRequest(SMSClientRequest $request, $decodeJson = true) |
147
|
|
|
{ |
148
|
7 |
|
$options = $request->options(); |
149
|
|
|
|
150
|
7 |
|
if (! isset($options['headers']["Authorization"])) { |
151
|
7 |
|
$options['headers']["Authorization"] = "Bearer ". $this->getToken(); |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
$response = $request->execute($options)->getBody(); |
155
|
|
|
|
156
|
7 |
|
return $decodeJson ? json_decode($response, true) : $response; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the client access token |
161
|
|
|
* |
162
|
|
|
* @param $clientID |
163
|
|
|
* @param $clientSecret |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
2 |
|
public static function authorize($clientID, $clientSecret) |
167
|
|
|
{ |
168
|
2 |
|
return json_decode( |
169
|
2 |
|
(new AuthorizationRequest($clientID, $clientSecret))->execute()->getBody(), true |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the prepared singleton instance of the client. |
175
|
|
|
* |
176
|
|
|
* @return SMSClient |
177
|
|
|
*/ |
178
|
13 |
|
public static function getInstance() |
179
|
|
|
{ |
180
|
13 |
|
if (! static::$instance) { |
181
|
1 |
|
static::$instance = new static(); |
182
|
|
|
} |
183
|
|
|
|
184
|
13 |
|
return static::$instance->configure(...func_get_args()); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|