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