|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Gammu\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use NotificationChannels\Gammu\Exceptions\CouldNotSendNotification; |
|
8
|
|
|
|
|
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
|
|
|
public function __construct(Repository $config, Client $client) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->config = $config; |
|
38
|
|
|
$this->client = $client; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function send($phoneNumber, $content, $sender = null, $callback = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->getUrl(); |
|
44
|
|
|
$this->setCallback($callback); |
|
45
|
|
|
$this->setKey(); |
|
46
|
|
|
$this->setUserAgent(); |
|
47
|
|
|
$this->setDestination($phoneNumber); |
|
48
|
|
|
$this->setContent($content); |
|
49
|
|
|
|
|
50
|
|
|
$response = $this->client->post($this->url, [ |
|
51
|
|
|
'json' => [ |
|
52
|
|
|
'key' => $this->key, |
|
53
|
|
|
'to' => $this->destination, |
|
54
|
|
|
'message' => $this->content, |
|
55
|
|
|
'callback' => $this->callback, |
|
56
|
|
|
], |
|
57
|
|
|
'headers' => [ |
|
58
|
|
|
'user-agent' => $this->ua, |
|
59
|
|
|
], |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
$this->apiStatusCode = $response->getStatusCode(); |
|
63
|
|
|
$this->apiResponseBody = $response->getBody()->getContents(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setCallback($callback) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->callback = $callback; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setDestination($phoneNumber) |
|
74
|
|
|
{ |
|
75
|
|
|
if (empty($phoneNumber)) { |
|
76
|
|
|
throw CouldNotSendNotification::destinationNotProvided(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->destination = trim($phoneNumber); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getDestination() |
|
85
|
|
|
{ |
|
86
|
|
|
if (empty($this->destination)) { |
|
87
|
|
|
throw CouldNotSendNotification::destinationNotProvided(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->destination; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function setContent($content) |
|
94
|
|
|
{ |
|
95
|
|
|
if (empty($content)) { |
|
96
|
|
|
throw CouldNotSendNotification::contentNotProvided(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->content = $content; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getContent() |
|
105
|
|
|
{ |
|
106
|
|
|
if (empty($this->content)) { |
|
107
|
|
|
throw CouldNotSendNotification::contentNotProvided(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->content; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function setUrl($url) |
|
114
|
|
|
{ |
|
115
|
|
|
if (empty($url)) { |
|
116
|
|
|
$this->url = $this->getDefaultUrl(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->url = $url; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getUrl() |
|
125
|
|
|
{ |
|
126
|
|
|
if (empty($this->url)) { |
|
127
|
|
|
return $this->getDefaultUrl(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->url; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function setKey() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->key = $this->config->get('services.gammu.auth'); |
|
136
|
|
|
|
|
137
|
|
|
if (empty($this->key)) { |
|
138
|
|
|
throw CouldNotSendNotification::authKeyNotProvided(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getKey() |
|
145
|
|
|
{ |
|
146
|
|
|
if (empty($this->key)) { |
|
147
|
|
|
throw CouldNotSendNotification::authKeyNotProvided(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->key; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function setUserAgent() |
|
154
|
|
|
{ |
|
155
|
|
|
$userAgents = [ |
|
156
|
|
|
$this->getSignature(), |
|
157
|
|
|
'GuzzleHttp/'.Client::VERSION, |
|
158
|
|
|
'PHP/'.PHP_VERSION, |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
if (extension_loaded('curl') && function_exists('curl_version')) { |
|
162
|
|
|
array_push($userAgents, 'curl/'.\curl_version()['version']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->ua = collect($userAgents)->implode(' '); |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
private function getDefaultUrl() |
|
171
|
|
|
{ |
|
172
|
|
|
$url = $this->config->get('services.gammu.url'); |
|
173
|
|
|
|
|
174
|
|
|
if (empty($url)) { |
|
175
|
|
|
throw CouldNotSendNotification::apiUrlNotProvided(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$this->url = $url; |
|
179
|
|
|
|
|
180
|
|
|
return $this->url; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|