1 | <?php |
||
9 | class ApiDriver extends DriverAbstract |
||
10 | { |
||
11 | protected $callback; |
||
12 | |||
13 | protected $config; |
||
14 | |||
15 | protected $client; |
||
16 | |||
17 | protected $url; |
||
18 | |||
19 | protected $key; |
||
20 | |||
21 | protected $ua; |
||
22 | |||
23 | protected $data = []; |
||
24 | |||
25 | protected $chunks = []; |
||
26 | |||
27 | public $destination; |
||
28 | |||
29 | public $content; |
||
30 | |||
31 | public $apiStatusCode; |
||
32 | |||
33 | public $apiResponseBody; |
||
34 | |||
35 | 14 | public function __construct(Repository $config, Client $client) |
|
40 | |||
41 | 1 | public function send($phoneNumber, $content, $sender = null, $callback = null) |
|
42 | { |
||
43 | 1 | $this->getUrl(); |
|
44 | 1 | $this->setCallback($callback); |
|
45 | 1 | $this->setKey(); |
|
46 | 1 | $this->setUserAgent(); |
|
47 | 1 | $this->setDestination($phoneNumber); |
|
48 | 1 | $this->setContent($content); |
|
49 | |||
50 | 1 | $response = $this->client->post($this->url, [ |
|
51 | 'json' => [ |
||
52 | 1 | 'key' => $this->key, |
|
53 | 1 | 'to' => $this->destination, |
|
54 | 1 | 'message' => $this->content, |
|
55 | 1 | 'callback' => $this->callback, |
|
56 | 1 | ], |
|
57 | 'headers' => [ |
||
58 | 1 | 'user-agent' => $this->ua, |
|
59 | 1 | ], |
|
60 | 1 | ]); |
|
61 | |||
62 | 1 | $this->apiStatusCode = $response->getStatusCode(); |
|
63 | 1 | $this->apiResponseBody = $response->getBody()->getContents(); |
|
64 | 1 | } |
|
65 | |||
66 | 1 | public function setCallback($callback) |
|
72 | |||
73 | 3 | public function setDestination($phoneNumber) |
|
83 | |||
84 | 2 | public function getDestination() |
|
92 | |||
93 | 3 | public function setContent($content) |
|
103 | |||
104 | 2 | public function getContent() |
|
112 | |||
113 | 1 | public function setUrl($url) |
|
114 | { |
||
115 | 1 | if (empty($url)) { |
|
116 | $this->url = $this->getDefaultUrl(); |
||
117 | } |
||
118 | |||
119 | 1 | $this->url = $url; |
|
120 | |||
121 | 1 | return $this; |
|
122 | } |
||
123 | |||
124 | 5 | public function getUrl() |
|
132 | |||
133 | 3 | public function setKey() |
|
143 | |||
144 | 2 | public function getKey() |
|
152 | |||
153 | 1 | public function setUserAgent() |
|
154 | { |
||
155 | $userAgents = [ |
||
156 | 1 | $this->getSignature(), |
|
157 | 1 | 'GuzzleHttp/'.Client::VERSION, |
|
158 | 1 | 'PHP/'.PHP_VERSION, |
|
159 | 1 | ]; |
|
160 | |||
161 | 1 | if (extension_loaded('curl') && function_exists('curl_version')) { |
|
162 | 1 | array_push($userAgents, 'curl/'.\curl_version()['version']); |
|
163 | 1 | } |
|
164 | |||
165 | 1 | $this->ua = collect($userAgents)->implode(' '); |
|
166 | |||
167 | 1 | return $this; |
|
168 | } |
||
169 | |||
170 | 4 | private function getDefaultUrl() |
|
182 | } |
||
183 |