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
|
|
|
* @param array $config |
33
|
|
|
* @return self |
34
|
|
|
*/ |
35
|
|
|
public function config($config) |
36
|
|
|
{ |
37
|
1 |
|
$this->checkConfig($config['id'], $config['key']); |
38
|
|
|
|
39
|
1 |
|
$this->id = $config['id']; |
40
|
1 |
|
$this->key = $config['key']; |
41
|
1 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param IMessage $message |
46
|
|
|
* @throws RM\SMSender\Exception |
47
|
|
|
* @return bool|string ID of Message |
48
|
|
|
*/ |
49
|
|
|
public function send(IMessage $message) |
50
|
|
|
{ |
51
|
1 |
|
$this->check($message); |
52
|
1 |
|
$this->onBeforeSend($message); |
53
|
1 |
|
$res = $this->getHttpClient()->request('GET', self::URL . '?' . str_replace(urlencode($message->getTo()), $message->getTo(), http_build_query([ |
54
|
1 |
|
'action' => ($this->debug ? 'validate' : 'send') . '1SMSHTTP', |
55
|
1 |
|
'i' => $this->id, |
56
|
1 |
|
's' => $this->getSignature($message), |
57
|
1 |
|
'd' => 1, |
58
|
1 |
|
'sender' => $message->getFrom(), |
59
|
1 |
|
'number' => $message->getTo(), |
60
|
1 |
|
'msg' => $message->getText(), |
61
|
|
|
]))); |
62
|
1 |
|
$response = $res->getBody(); |
63
|
1 |
|
if ($this->isSuccess($response)) { |
64
|
1 |
|
$this->onSuccess($message, $response); |
65
|
|
|
} else { |
66
|
1 |
|
$e = new GatewayException($response); |
67
|
1 |
|
$this->onError($message, $response, $e); |
68
|
1 |
|
throw $e; |
69
|
|
|
} |
70
|
1 |
|
return ($this->debug) |
71
|
1 |
|
? TRUE |
72
|
1 |
|
: substr(Strings::trim((string)$response), 3); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Message $message |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getSignature(IMessage $message) |
80
|
|
|
{ |
81
|
1 |
|
return substr(md5($this->key . str_replace('+', '', $message->getTo())), 10, 11); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $response |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
|
|
public function isSuccess($response) |
89
|
|
|
{ |
90
|
1 |
|
$response = Strings::trim((string)$response); |
91
|
1 |
|
return ($this->debug && $response === 'SMSValid') || (!$this->debug && substr($response, 0, 2) === 'ok'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $id |
96
|
|
|
* @param string $key |
97
|
|
|
* @throws ConfigurationException |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
private function checkConfig($id, $key) |
101
|
|
|
{ |
102
|
1 |
|
if (!Strings::match($id, '~^1-[0-9a-zA-Z]{6}$~')) |
103
|
1 |
|
throw new ConfigurationException('Parameter "id" must be in format "1-[0-9a-zA-Z]{6}".'); |
104
|
1 |
|
if (strlen($key) !== 8) |
105
|
1 |
|
throw new ConfigurationException('Parameter "key" must have 8 charactest. It has ' . strlen($key) . ' characters.'); |
106
|
1 |
|
return TRUE; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Message $message |
111
|
|
|
* @throws MissingParameterException |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
private function check(IMessage $message) |
115
|
|
|
{ |
116
|
1 |
|
if (empty($message->getFrom())) |
117
|
1 |
|
throw new MissingParameterException('Message has empty sender. Use method setFrom().'); |
118
|
1 |
|
if (empty($message->getTo())) |
119
|
1 |
|
throw new MissingParameterException('Message has empty recipent number. Use method setTo().'); |
120
|
1 |
|
if (empty($message->getText())) |
121
|
1 |
|
throw new MissingParameterException('Message has empty text. Use method setText().'); |
122
|
1 |
|
return TRUE; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|