|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\SmscRu; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification; |
|
8
|
|
|
|
|
9
|
|
|
class SmscRuApi |
|
10
|
|
|
{ |
|
11
|
|
|
public 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
|
|
|
/** @var array */ |
|
29
|
|
|
protected $extra; |
|
30
|
|
|
|
|
31
|
2 |
|
public function __construct(array $config) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->login = Arr::get($config, 'login'); |
|
34
|
2 |
|
$this->secret = Arr::get($config, 'secret'); |
|
35
|
2 |
|
$this->sender = Arr::get($config, 'sender'); |
|
36
|
2 |
|
$this->endpoint = Arr::get($config, 'host', 'https://smsc.ru/').'sys/send.php'; |
|
37
|
|
|
|
|
38
|
2 |
|
$this->extra = Arr::get($config, 'extra', []); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
2 |
|
$this->client = new HttpClient([ |
|
41
|
2 |
|
'timeout' => 5, |
|
42
|
|
|
'connect_timeout' => 5, |
|
43
|
|
|
]); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function send($params) |
|
47
|
|
|
{ |
|
48
|
|
|
$base = [ |
|
49
|
|
|
'charset' => 'utf-8', |
|
50
|
|
|
'login' => $this->login, |
|
51
|
|
|
'psw' => $this->secret, |
|
52
|
|
|
'sender' => $this->sender, |
|
53
|
|
|
'fmt' => self::FORMAT_JSON, |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
$params = \array_merge($base, \array_filter($params), $this->extra); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$response = $this->client->request('POST', $this->endpoint, ['form_params' => $params]); |
|
60
|
|
|
|
|
61
|
|
|
$response = \json_decode((string) $response->getBody(), true); |
|
62
|
|
|
|
|
63
|
|
|
if (isset($response['error'])) { |
|
64
|
|
|
throw new \DomainException($response['error'], $response['error_code']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $response; |
|
68
|
|
|
} catch (\DomainException $exception) { |
|
69
|
|
|
throw CouldNotSendNotification::smscRespondedWithAnError($exception); |
|
70
|
|
|
} catch (\Exception $exception) { |
|
71
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithSmsc($exception); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..