forked from arcturial/clickatell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clickatell.php
213 lines (184 loc) · 5.86 KB
/
Clickatell.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* The Clickatell SMS Library provides a standardised way of talking to and
* receiving replies from the Clickatell API's. It makes it
* easier to write your applications and grants the ability to
* quickly switch the type of API you want to use HTTP/XML without
* changing any code.
*
* PHP Version 5.3
*
* @category Clickatell
* @package Clickatell
* @author Chris Brand <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link https://github.com/arcturial
*/
namespace Clickatell;
use Clickatell\Component\Translate\TranslateJson as TranslateJson;
use Clickatell\Api\Api as Api;
use Clickatell\Exception\ApiException as ApiException;
use \Closure as Closure;
use Clickatell\Component\Event as Event;
use Clickatell\Component\Validate as Validate;
/**
* This is the main messenger class that encapsulates various objects to succesfully
* send Clickatell calls and respond in an appropriate manner. The messenger class
* enables you to set your own Transport and Translate interfaces.
*
* @category Clickatell
* @package Clickatell
* @author Chris Brand <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link https://github.com/arcturial
*/
class Clickatell
{
/**
* The HTTP Transport Interface
* @var string
*/
const HTTP_API = "Clickatell\Api\Http";
/**
* The XML Transport Interface
* @var string
*/
const XML_API = "Clickatell\Api\Xml";
/**
* The SOAP Transport Interface
* @var string
*/
const SOAP_API = "Clickatell\Api\Soap";
/**
* The SMTP Transport Interface
* @var string
*/
const SMTP_API = "Clickatell\Api\Smtp";
/**
* The transport/api to use for the request
* @var Clickatell\Api\Api
*/
private $_transport;
/**
* Clickatell Messenger Instantiation. Creates the Transport/Translate/Request
* interfaces required.
*
* @param string $username API username
* @param string $password API password
* @param int $apiId API ID (Sub-product ID)
* @param string $transport Transport protocol to use (defaults to HTTP api)
*
* @return boolean
*/
public function __construct($username, $password, $apiId, $transport = self::HTTP_API)
{
// Register autoloader
spl_autoload_register(array($this, '_autoLoad'));
$this->_transport = new $transport(new TranslateJson());
$this->_transport->authenticate($username, $password, $apiId);
// Clear all registered events
Event::clear();
// Add validation listener using events
Event::on(
'request',
function ($data) {
$method = $data['call'];
$args = $data['request'];
Validate::processValidation($method, $args);
}
);
}
/**
* Module autoloader. This allows us to integrate our module
* with other frameworks without clashing.
*
* @param string $class Class to load
*
* @return boolean
*/
private function _autoLoad($class)
{
$class = str_replace(
"\\",
"/",
preg_replace("/Clickatell\\\/", "", $class)
);
if (is_file(__DIR__ . "/" . $class . ".php")) {
return (boolean) include_once __DIR__ . "/" . $class . ".php";
} else {
return false;
}
}
/**
* Loop through an array of implemented interface and check
* if the method is defined as an available API.
*
* @param array $interfaces Interfaces to loop through
* @param string $method Method name to check for
*
* @return boolean
*/
private function _methodExists($interfaces, $method)
{
foreach ($interfaces as $interface) {
if (method_exists($interface, $method)) {
return true;
}
}
return false;
}
/**
* Return the transport associated with the request.
*
* @param Clickatell\Api\Api $transport Transport to use
*
* @return boolean
*/
public function setTransport(Api $transport)
{
$this->_transport = $transport;
}
/**
* Return the transport associated with the request.
*
* @return Clickatell\Api\Api
*/
public function getTransport()
{
return $this->_transport;
}
/**
* Utility function to register a new event listener.
*
* @param string $event Event to listen for
* @param \Closure $closure Callback to trigger
*
* @return boolean
*/
public function on($event, Closure $closure)
{
return Event::on($event, $closure);
}
/**
* Magic to forward an API request to the Transport interface.
* The Transport interface then ensures the method exists and
* connects to Clickatell to complete the call. The messenger
* uses an Action object (the handler) to group these tasks.
*
* @param string $name Method name
* @param array $arguments Method arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
// Get the interface the class uses
$interfaces = class_implements($this->_transport);
if ($this->_methodExists($interfaces, $name)) {
$callArgs = array($name, $arguments);
return call_user_func_array(array($this->_transport, "call"), $callArgs);
} else {
throw new ApiException(ApiException::ERR_METHOD_NOT_FOUND);
}
}
}