1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\SmscRu; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
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 $client; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $endpoint; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $login; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $secret; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $sender; |
27
|
|
|
|
28
|
2 |
|
public function __construct(array $config) |
29
|
|
|
{ |
30
|
2 |
|
$this->login = Arr::get($config, 'login'); |
31
|
2 |
|
$this->secret = Arr::get($config, 'secret'); |
32
|
2 |
|
$this->sender = Arr::get($config, 'sender'); |
33
|
2 |
|
$this->endpoint = Arr::get($config, 'host', 'https://smsc.ru/').'sys/send.php'; |
34
|
|
|
|
35
|
2 |
|
$this->client = new HttpClient([ |
36
|
2 |
|
'timeout' => 5, |
37
|
|
|
'connect_timeout' => 5, |
38
|
|
|
]); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
public function send($params) |
42
|
|
|
{ |
43
|
|
|
$base = [ |
44
|
|
|
'charset' => 'utf-8', |
45
|
|
|
'login' => $this->login, |
46
|
|
|
'psw' => $this->secret, |
47
|
|
|
'sender' => $this->sender, |
48
|
|
|
'fmt' => self::FORMAT_JSON, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$params = \array_merge($base, \array_filter($params)); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$response = $this->client->request('POST', $this->endpoint, ['form_params' => $params]); |
55
|
|
|
|
56
|
|
|
$response = \json_decode((string) $response->getBody(), true); |
57
|
|
|
|
58
|
|
|
if (isset($response['error'])) { |
59
|
|
|
throw new \DomainException($response['error'], $response['error_code']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $response; |
63
|
|
|
} catch (\DomainException $exception) { |
64
|
|
|
throw CouldNotSendNotification::smscRespondedWithAnError($exception); |
65
|
|
|
} catch (\Exception $exception) { |
66
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithSmsc($exception); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|