|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zing\LaravelSms\Connectors; |
|
6
|
|
|
|
|
7
|
|
|
use GrahamCampbell\Manager\ConnectorInterface; |
|
8
|
|
|
use Illuminate\Support\Facades\Event; |
|
9
|
|
|
use Illuminate\Support\Facades\Log; |
|
10
|
|
|
use Overtrue\EasySms\Contracts\GatewayInterface; |
|
11
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
12
|
|
|
use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
|
13
|
|
|
use Overtrue\EasySms\Message; |
|
14
|
|
|
use Overtrue\EasySms\PhoneNumber; |
|
15
|
|
|
use Overtrue\EasySms\Support\Config; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
use Zing\LaravelSms\Events\SmsSending; |
|
18
|
|
|
use Zing\LaravelSms\Events\SmsSent; |
|
19
|
|
|
use Zing\LaravelSms\Exceptions\CouldNotSendNotification; |
|
20
|
|
|
use Zing\LaravelSms\Exceptions\InvalidArgumentException; |
|
21
|
|
|
use function is_array; |
|
22
|
|
|
use function trim; |
|
23
|
|
|
|
|
24
|
|
|
class Connector implements ConnectorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Overtrue\EasySms\Contracts\GatewayInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $driver; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Overtrue\EasySms\Support\Config |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Connector constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
*/ |
|
41
|
32 |
|
public function __construct($config) |
|
42
|
|
|
{ |
|
43
|
32 |
|
$this->config = new Config($config); |
|
44
|
32 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $config |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Zing\LaravelSms\Exceptions\InvalidArgumentException |
|
50
|
|
|
* |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
30 |
|
public function connect(array $config) |
|
54
|
|
|
{ |
|
55
|
30 |
|
if (! isset($config['driver'])) { |
|
56
|
1 |
|
throw new InvalidArgumentException('A driver must be specified.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
29 |
|
$driverClass = $config['driver']; |
|
60
|
29 |
|
$this->driver = $this->resolveDriver($driverClass, $config); |
|
61
|
|
|
|
|
62
|
28 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get gateway by class name. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $driverClass |
|
69
|
|
|
* @param array $config |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \Zing\LaravelSms\Exceptions\InvalidArgumentException |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Overtrue\EasySms\Contracts\GatewayInterface |
|
74
|
|
|
*/ |
|
75
|
29 |
|
protected function resolveDriver(string $driverClass, array $config): GatewayInterface |
|
76
|
|
|
{ |
|
77
|
29 |
|
if (! class_exists($driverClass) || ! in_array(GatewayInterface::class, class_implements($driverClass), true)) { |
|
78
|
1 |
|
throw new InvalidArgumentException("Unsupported driver [{$driverClass}]."); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
28 |
|
return new $driverClass($config); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Overtrue\EasySms\Contracts\PhoneNumberInterface |
|
88
|
|
|
*/ |
|
89
|
26 |
|
protected function formatPhoneNumber($number) |
|
90
|
|
|
{ |
|
91
|
26 |
|
if ($number instanceof PhoneNumberInterface) { |
|
92
|
13 |
|
return $number; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
13 |
|
return new PhoneNumber(trim($number)); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Overtrue\EasySms\Contracts\MessageInterface |
|
102
|
|
|
*/ |
|
103
|
26 |
|
protected function formatMessage($message) |
|
104
|
|
|
{ |
|
105
|
26 |
|
if (! ($message instanceof MessageInterface)) { |
|
106
|
8 |
|
if (! is_array($message)) { |
|
107
|
|
|
$message = [ |
|
108
|
7 |
|
'content' => $message, |
|
109
|
7 |
|
'template' => $message, |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
8 |
|
$message = new Message($message); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
26 |
|
return $message; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param mixed $number |
|
121
|
|
|
* @param mixed $message |
|
122
|
|
|
* |
|
123
|
|
|
* @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification |
|
124
|
|
|
* @throws \Throwable |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool|mixed |
|
127
|
|
|
*/ |
|
128
|
26 |
|
public function send($number, $message) |
|
129
|
|
|
{ |
|
130
|
26 |
|
$number = $this->formatPhoneNumber($number); |
|
131
|
26 |
|
$message = $this->formatMessage($message); |
|
132
|
|
|
|
|
133
|
|
|
try { |
|
134
|
26 |
|
Event::dispatch(new SmsSending($number, $message)); |
|
135
|
24 |
|
Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType())); |
|
136
|
24 |
|
$result = $this->driver->send($number, $message, $this->config); |
|
137
|
24 |
|
Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType()), (array) $result); |
|
138
|
24 |
|
Event::dispatch(new SmsSent($number, $message, $result)); |
|
139
|
|
|
|
|
140
|
24 |
|
return $result; |
|
141
|
2 |
|
} catch (Throwable $exception) { |
|
142
|
2 |
|
throw CouldNotSendNotification::captureExceptionInDriver($exception); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|