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
|
|
|
* @param string $authToken Plivo API Auth Token |
45
|
|
|
* @param string $authId Plivo API Auth ID |
46
|
|
|
* @param string $fromNumber The phone number that will be shown as the sender ID |
47
|
|
|
* @param string $toNumber The phone number to which the message will be sent |
48
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered |
49
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
50
|
|
|
* @param bool $useSSL Whether to connect via SSL. |
51
|
|
|
* @param string $host The Plivo server hostname. |
52
|
|
|
* @param string $version The Plivo API version (default PlivoHandler::API_V1) |
53
|
|
|
* @param string $limit The character limit |
54
|
|
|
*/ |
55
|
45 |
|
public function __construct($authToken, $authId, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.plivo.com', $version = null, $limit = 160) |
56
|
|
|
{ |
57
|
45 |
|
if (empty($version)) { |
58
|
|
|
throw new Exception('API Version is empty'); |
59
|
|
|
} |
60
|
|
|
|
61
|
45 |
|
$connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80'; |
62
|
45 |
|
parent::__construct($connectionString, $level, $bubble); |
63
|
|
|
|
64
|
45 |
|
$this->authToken = $authToken; |
65
|
45 |
|
$this->authId = $authId; |
66
|
45 |
|
$this->fromNumber = $fromNumber; |
67
|
45 |
|
$this->toNumber = $toNumber; |
68
|
45 |
|
$this->host = $host; |
69
|
45 |
|
$this->version = $version; |
70
|
45 |
|
$this->limit = $limit; |
|
|
|
|
71
|
45 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
* |
76
|
|
|
* @param array $record |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
36 |
|
protected function generateDataStream($record) |
80
|
|
|
{ |
81
|
36 |
|
$content = $this->buildContent($record); |
82
|
36 |
|
return $this->buildHeader($content) . $content; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Builds the body of API call |
87
|
|
|
* |
88
|
|
|
* @param array $record |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
abstract protected function buildContent($record); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Builds the URL for the API call |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
abstract protected function buildRequestUrl(); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Builds the header of the API call |
102
|
|
|
* |
103
|
|
|
* @param string $content |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
36 |
|
private function buildHeader($content) |
107
|
|
|
{ |
108
|
36 |
|
$auth = $this->authToken; |
109
|
|
|
|
110
|
36 |
|
if ($this->authId) { |
111
|
24 |
|
$auth = "Basic " . base64_encode($this->authId.":".$this->authToken); |
112
|
24 |
|
} |
113
|
|
|
|
114
|
36 |
|
$header = $this->buildRequestUrl(); |
115
|
|
|
|
116
|
36 |
|
$header .= "Host: {$this->host}\r\n"; |
117
|
36 |
|
$header .= "Authorization: ".$auth."\r\n"; |
118
|
36 |
|
$header .= "Content-Type: application/json\r\n"; |
119
|
36 |
|
$header .= "Content-Length: " . strlen($content) . "\r\n"; |
120
|
36 |
|
$header .= "\r\n"; |
121
|
36 |
|
return $header; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
* |
129
|
|
|
* @param array $record |
130
|
|
|
*/ |
131
|
36 |
|
protected function write(array $record) |
132
|
|
|
{ |
133
|
36 |
|
parent::write($record); |
134
|
36 |
|
$this->closeSocket(); |
135
|
36 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
9 |
|
protected function getDefaultFormatter() |
141
|
|
|
{ |
142
|
9 |
|
return new SMSFormatter(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: