|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zing\LaravelSms\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GrahamCampbell\Manager\ConnectorInterface; |
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
|
8
|
|
|
use function is_array; |
|
9
|
|
|
use Overtrue\EasySms\Contracts\GatewayInterface; |
|
10
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
11
|
|
|
use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
|
12
|
|
|
use Overtrue\EasySms\Support\Config; |
|
13
|
|
|
use function trim; |
|
14
|
|
|
use Zing\LaravelSms\Exceptions\CouldNotSendNotification; |
|
15
|
|
|
use Zing\LaravelSms\Exceptions\InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
class Connector implements ConnectorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var GatewayInterface */ |
|
20
|
|
|
protected $driver; |
|
21
|
|
|
|
|
22
|
|
|
/** @var \Overtrue\EasySms\Support\Config */ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Connector constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param $config |
|
29
|
|
|
*/ |
|
30
|
22 |
|
public function __construct($config) |
|
31
|
|
|
{ |
|
32
|
22 |
|
$this->config = new Config($config); |
|
33
|
22 |
|
} |
|
34
|
|
|
|
|
35
|
20 |
|
public function connect(array $config) |
|
36
|
|
|
{ |
|
37
|
20 |
|
if (! isset($config['driver'])) { |
|
38
|
1 |
|
throw new InvalidArgumentException('A driver must be specified.'); |
|
39
|
|
|
} |
|
40
|
19 |
|
$driver = $config['driver']; |
|
41
|
19 |
|
if (! class_exists($driver) || ! in_array(GatewayInterface::class, class_implements($driver), true)) { |
|
42
|
1 |
|
throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
18 |
|
$this->driver = new $driver($config); |
|
46
|
|
|
|
|
47
|
18 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Overtrue\EasySms\PhoneNumber |
|
54
|
|
|
*/ |
|
55
|
20 |
|
protected function formatPhoneNumber($number) |
|
56
|
|
|
{ |
|
57
|
20 |
|
if ($number instanceof PhoneNumberInterface) { |
|
58
|
9 |
|
return $number; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
11 |
|
return new \Overtrue\EasySms\PhoneNumber(trim($number)); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Overtrue\EasySms\Contracts\MessageInterface |
|
68
|
|
|
*/ |
|
69
|
20 |
|
protected function formatMessage($message) |
|
70
|
|
|
{ |
|
71
|
20 |
|
if (! ($message instanceof MessageInterface)) { |
|
72
|
6 |
|
if (! is_array($message)) { |
|
73
|
|
|
$message = [ |
|
74
|
5 |
|
'content' => $message, |
|
75
|
5 |
|
'template' => $message, |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
$message = new \Overtrue\EasySms\Message($message); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
20 |
|
return $message; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $number |
|
87
|
|
|
* @param mixed $message |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool|mixed |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification |
|
92
|
|
|
* @throws \Throwable |
|
93
|
|
|
*/ |
|
94
|
20 |
|
public function send($number, $message) |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
20 |
|
$number = $this->formatPhoneNumber($number); |
|
98
|
20 |
|
$message = $this->formatMessage($message); |
|
99
|
|
|
|
|
100
|
20 |
|
$this->sending($number, $message); |
|
101
|
18 |
|
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())); |
|
102
|
18 |
|
$result = $this->driver->send($number, $message, $this->config); |
|
103
|
18 |
|
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); |
|
104
|
18 |
|
$this->sent($number, $message, $result); |
|
105
|
|
|
|
|
106
|
18 |
|
return $result; |
|
107
|
2 |
|
} catch (Exception $exception) { |
|
108
|
2 |
|
throw CouldNotSendNotification::captureExceptionInDriver($exception); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
18 |
|
public function sending($number, $message) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
18 |
|
} |
|
115
|
|
|
|
|
116
|
18 |
|
public function sent($number, $message, $result) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
18 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|