|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\SmscRu; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
7
|
|
|
use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification; |
|
8
|
|
|
|
|
9
|
|
|
class SmscRuApi |
|
10
|
|
|
{ |
|
11
|
|
|
const FORMAT_JSON = 3; |
|
12
|
|
|
|
|
13
|
|
|
/** @var HttpClient */ |
|
14
|
|
|
protected $httpClient; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $url; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $login; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $secret; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $sender; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($config) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->url = array_get($config, 'host', 'https://smsc.ru/') . 'sys/send.php'; |
|
31
|
|
|
$this->login = array_get($config, 'login'); |
|
32
|
|
|
$this->secret = array_get($config, 'secret'); |
|
33
|
|
|
$this->sender = array_get($config, 'sender'); |
|
34
|
|
|
|
|
35
|
|
|
$this->httpClient = new HttpClient([ |
|
36
|
|
|
'timeout' => 5, |
|
37
|
|
|
'connect_timeout' => 5, |
|
38
|
|
|
]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $params |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
* |
|
46
|
|
|
* @throws CouldNotSendNotification |
|
47
|
|
|
*/ |
|
48
|
|
|
public function send($params) |
|
49
|
|
|
{ |
|
50
|
|
|
$base = [ |
|
51
|
|
|
'charset' => 'utf-8', |
|
52
|
|
|
'login' => $this->login, |
|
53
|
|
|
'psw' => $this->secret, |
|
54
|
|
|
'sender' => $this->sender, |
|
55
|
|
|
'fmt' => self::FORMAT_JSON, |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
$params = array_merge($params, $base); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$response = $this->httpClient->post($this->url, ['form_params' => $params]); |
|
62
|
|
|
|
|
63
|
|
|
$response = json_decode((string) $response->getBody(), true); |
|
64
|
|
|
|
|
65
|
|
|
if (isset($response['error'])) { |
|
66
|
|
|
throw new DomainException($response['error'], $response['error_code']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $response; |
|
70
|
|
|
} catch (DomainException $exception) { |
|
71
|
|
|
throw CouldNotSendNotification::smscRespondedWithAnError($exception); |
|
72
|
|
|
} catch (\Exception $exception) { |
|
73
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithSmsc($exception); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|