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\ConfigurationException; |
11
|
|
|
use RM\SMSender\GatewayException; |
12
|
|
|
use RM\SMSender\MissingParameterException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sender for service EuroSms.sk |
16
|
|
|
* @method onBeforeSend(IMessage $message) |
17
|
|
|
* @method onSuccess(IMessage $message, $response) |
18
|
|
|
* @method onError(IMessage $message, $response) |
19
|
|
|
*/ |
20
|
1 |
|
class Sender extends BaseSender implements ISender |
21
|
|
|
{ |
22
|
|
|
CONST URL = 'https://as.eurosms.com/sms/Sender'; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $id; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $config |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
|
|
public function config($config) |
35
|
|
|
{ |
36
|
1 |
|
$this->checkConfig($config['id'], $config['key']); |
37
|
|
|
|
38
|
1 |
|
$this->id = $config['id']; |
39
|
1 |
|
$this->key = $config['key']; |
40
|
1 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param IMessage $message |
45
|
|
|
* @throws RM\SMSender\Exception |
46
|
|
|
* @return bool|string ID of Message |
47
|
|
|
*/ |
48
|
|
|
public function send(IMessage $message) |
49
|
|
|
{ |
50
|
1 |
|
$this->check($message); |
51
|
1 |
|
$this->onBeforeSend($message); |
52
|
1 |
|
$res = $this->getHttpClient()->request('GET', self::URL . '?' . str_replace(urlencode($message->getTo()), $message->getTo(), http_build_query([ |
53
|
1 |
|
'action' => ($this->debug ? 'validate' : 'send') . '1SMSHTTP', |
54
|
1 |
|
'i' => $this->id, |
55
|
1 |
|
's' => $this->getSignature($message), |
56
|
1 |
|
'd' => 1, |
57
|
1 |
|
'sender' => $message->getFrom(), |
58
|
1 |
|
'number' => $message->getTo(), |
59
|
1 |
|
'msg' => urlencode($message->getText()), |
60
|
|
|
]))); |
61
|
1 |
|
$response = $res->getBody(); |
62
|
1 |
|
if ($this->isSuccess($response)) { |
63
|
1 |
|
$this->onSuccess($message, $response); |
64
|
|
|
} else { |
65
|
1 |
|
$e = new GatewayException($response); |
66
|
1 |
|
$this->onError($message, $response, $e); |
|
|
|
|
67
|
1 |
|
throw $e; |
68
|
|
|
} |
69
|
1 |
|
return ($this->debug) |
70
|
1 |
|
? TRUE |
71
|
1 |
|
: substr(Strings::trim((string)$response), 3); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Message $message |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getSignature(IMessage $message) |
79
|
|
|
{ |
80
|
1 |
|
return substr(md5($this->key . str_replace('+', '', $message->getTo())), 10, 11); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $response |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
|
|
public function isSuccess($response) |
88
|
|
|
{ |
89
|
1 |
|
$response = Strings::trim((string)$response); |
90
|
1 |
|
return ($this->debug && $response === 'SMSValid') || (!$this->debug && substr($response, 0, 2) === 'ok'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $id |
95
|
|
|
* @param string $key |
96
|
|
|
* @throws ConfigurationException |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
private function checkConfig($id, $key) |
100
|
|
|
{ |
101
|
1 |
|
if (!Strings::match($id, '~^1-[0-9a-zA-Z]{6}$~')) |
102
|
1 |
|
throw new ConfigurationException('Parameter "id" must be in format "1-[0-9a-zA-Z]{6}".'); |
103
|
1 |
|
if (strlen($key) !== 8) |
104
|
1 |
|
throw new ConfigurationException('Parameter "key" must have 8 charactest. It has ' . strlen($key) . ' characters.'); |
105
|
1 |
|
return TRUE; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Message $message |
110
|
|
|
* @throws MissingParameterException |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
private function check(IMessage $message) |
114
|
|
|
{ |
115
|
1 |
|
if (empty($message->getFrom())) |
116
|
1 |
|
throw new MissingParameterException('Message has empty sender. Use method setFrom().'); |
117
|
1 |
|
if (empty($message->getTo())) |
118
|
1 |
|
throw new MissingParameterException('Message has empty recipent number. Use method setTo().'); |
119
|
1 |
|
if (empty($message->getText())) |
120
|
1 |
|
throw new MissingParameterException('Message has empty text. Use method setText().'); |
121
|
1 |
|
return TRUE; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.