1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\Monolog\Handler; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Monolog\Handler\SocketHandler; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Tylercd100\Monolog\Formatter\SMSFormatter; |
9
|
|
|
|
10
|
|
|
abstract class SMSHandler extends SocketHandler |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $authToken; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $authId; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $fromNumber; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $toNumber; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $host; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $version; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
protected $limit; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $authToken Plivo API Auth Token |
50
|
|
|
* @param string $authId Plivo API Auth ID |
51
|
|
|
* @param string $fromNumber The phone number that will be shown as the sender ID |
52
|
|
|
* @param string $toNumber The phone number to which the message will be sent |
53
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered |
54
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
55
|
|
|
* @param bool $useSSL Whether to connect via SSL. |
56
|
|
|
* @param string $host The Plivo server hostname. |
57
|
|
|
* @param string $version The Plivo API version (default PlivoHandler::API_V1) |
58
|
|
|
* @param int $limit The character limit |
59
|
|
|
*/ |
60
|
45 |
|
public function __construct($authToken, $authId, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.plivo.com', $version = null, $limit = 160) |
61
|
|
|
{ |
62
|
45 |
|
if (empty($version)) { |
63
|
|
|
throw new Exception('API Version is empty'); |
64
|
|
|
} |
65
|
|
|
|
66
|
45 |
|
$connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80'; |
67
|
45 |
|
parent::__construct($connectionString, $level, $bubble); |
68
|
|
|
|
69
|
45 |
|
$this->authToken = $authToken; |
70
|
45 |
|
$this->authId = $authId; |
71
|
45 |
|
$this->fromNumber = $fromNumber; |
72
|
45 |
|
$this->toNumber = $toNumber; |
73
|
45 |
|
$this->host = $host; |
74
|
45 |
|
$this->version = $version; |
75
|
45 |
|
$this->limit = $limit; |
76
|
45 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @param array $record |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
36 |
|
protected function generateDataStream($record) |
85
|
|
|
{ |
86
|
36 |
|
$content = $this->buildContent($record); |
87
|
36 |
|
return $this->buildHeader($content) . $content; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Builds the body of API call |
92
|
|
|
* |
93
|
|
|
* @param array $record |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
abstract protected function buildContent($record); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Builds the URL for the API call |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
abstract protected function buildRequestUrl(); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Builds the header of the API call |
107
|
|
|
* |
108
|
|
|
* @param string $content |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
36 |
|
private function buildHeader($content) |
112
|
|
|
{ |
113
|
36 |
|
$auth = $this->authToken; |
114
|
|
|
|
115
|
36 |
|
if ($this->authId) { |
116
|
24 |
|
$auth = "Basic " . base64_encode($this->authId.":".$this->authToken); |
117
|
24 |
|
} |
118
|
|
|
|
119
|
36 |
|
$header = $this->buildRequestUrl(); |
120
|
|
|
|
121
|
36 |
|
$header .= "Host: {$this->host}\r\n"; |
122
|
36 |
|
$header .= "Authorization: ".$auth."\r\n"; |
123
|
36 |
|
$header .= "Content-Type: application/json\r\n"; |
124
|
36 |
|
$header .= "Content-Length: " . strlen($content) . "\r\n"; |
125
|
36 |
|
$header .= "\r\n"; |
126
|
36 |
|
return $header; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* |
134
|
|
|
* @param array $record |
135
|
|
|
*/ |
136
|
36 |
|
protected function write(array $record) |
137
|
|
|
{ |
138
|
36 |
|
parent::write($record); |
139
|
36 |
|
$this->closeSocket(); |
140
|
36 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
9 |
|
protected function getDefaultFormatter() |
146
|
|
|
{ |
147
|
9 |
|
return new SMSFormatter(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|