1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RM\SMSender\EuroSms; |
4
|
|
|
|
5
|
|
|
use Nette\Utils\Strings; |
6
|
|
|
use RM; |
7
|
|
|
use RM\SMSender\BaseSender; |
8
|
|
|
use RM\SMSender\ISender; |
9
|
|
|
use RM\SMSender\IMessage; |
10
|
|
|
use RM\SMSender\Exception; |
11
|
|
|
use RM\SMSender\ConfigurationException; |
12
|
|
|
use RM\SMSender\GatewayException; |
13
|
|
|
use RM\SMSender\MissingParameterException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Sender for service EuroSms.sk |
17
|
|
|
* @method onBeforeSend(IMessage $message) |
18
|
|
|
* @method onSuccess(IMessage $message, $response) |
19
|
|
|
* @method onError(IMessage $message, $response, Exception $e) |
20
|
|
|
*/ |
21
|
1 |
|
class Sender extends BaseSender implements ISender |
22
|
|
|
{ |
23
|
|
|
CONST URL = 'https://as.eurosms.com/sms/Sender'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $id; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $key; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct(array $config = NULL) |
33
|
|
|
{ |
34
|
1 |
|
if (is_array($config) && !empty($config)) |
35
|
|
|
$this->config($config); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $config |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
|
public function config($config) |
43
|
|
|
{ |
44
|
1 |
|
$this->checkConfig($config['id'], $config['key']); |
45
|
|
|
|
46
|
1 |
|
$this->id = $config['id']; |
47
|
1 |
|
$this->key = $config['key']; |
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws RM\SMSender\Exception |
53
|
|
|
* @return bool|string ID of Message |
54
|
|
|
*/ |
55
|
|
|
public function send(IMessage $message) |
56
|
|
|
{ |
57
|
1 |
|
$this->check($message); |
58
|
1 |
|
$this->onBeforeSend($message); |
59
|
1 |
|
$res = $this->getHttpClient()->request('GET', self::URL . '?' . str_replace(urlencode($message->getTo()), $message->getTo(), http_build_query([ |
60
|
1 |
|
'action' => ($this->debug ? 'validate' : 'send') . '1SMSHTTP', |
61
|
1 |
|
'i' => $this->id, |
62
|
1 |
|
's' => $this->getSignature($message), |
63
|
1 |
|
'd' => 1, |
64
|
1 |
|
'sender' => $message->getFrom(), |
65
|
1 |
|
'number' => $message->getTo(), |
66
|
1 |
|
'msg' => $message->getText(), |
67
|
|
|
]))); |
68
|
1 |
|
$response = $res->getBody(); |
69
|
1 |
|
if ($this->isSuccess($response)) { |
70
|
1 |
|
$this->onSuccess($message, $response); |
71
|
|
|
} else { |
72
|
1 |
|
$e = new GatewayException($response); |
73
|
1 |
|
$this->onError($message, $response, $e); |
74
|
1 |
|
throw $e; |
75
|
|
|
} |
76
|
1 |
|
return ($this->debug) |
77
|
1 |
|
? TRUE |
78
|
1 |
|
: substr(Strings::trim((string)$response), 3); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getSignature(IMessage $message) : string |
82
|
|
|
{ |
83
|
1 |
|
return substr(md5($this->key . str_replace('+', '', $message->getTo())), 10, 11); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isSuccess(string $response) |
87
|
|
|
{ |
88
|
1 |
|
$response = Strings::trim($response); |
89
|
1 |
|
return ($this->debug && $response === 'SMSValid') || (!$this->debug && substr($response, 0, 2) === 'ok'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $id |
94
|
|
|
* @param string $key |
95
|
|
|
* @throws ConfigurationException |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
private function checkConfig($id, $key) |
99
|
|
|
{ |
100
|
1 |
|
if (!Strings::match($id, '~^1-[0-9a-zA-Z]{6}$~')) |
101
|
1 |
|
throw new ConfigurationException('Parameter "id" must be in format "1-[0-9a-zA-Z]{6}".'); |
102
|
1 |
|
if (strlen($key) !== 8) |
103
|
1 |
|
throw new ConfigurationException('Parameter "key" must have 8 charactest. It has ' . strlen($key) . ' characters.'); |
104
|
1 |
|
return TRUE; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @throws MissingParameterException |
109
|
|
|
*/ |
110
|
|
|
private function check(IMessage $message) : bool |
111
|
|
|
{ |
112
|
1 |
|
if (empty($message->getFrom())) |
113
|
1 |
|
throw new MissingParameterException('Message has empty sender. Use method setFrom().'); |
114
|
1 |
|
if (empty($message->getTo())) |
115
|
1 |
|
throw new MissingParameterException('Message has empty recipent number. Use method setTo().'); |
116
|
1 |
|
if (empty($message->getText())) |
117
|
1 |
|
throw new MissingParameterException('Message has empty text. Use method setText().'); |
118
|
1 |
|
return TRUE; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|