|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/easy-sms. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overtrue\EasySms; |
|
13
|
|
|
|
|
14
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
15
|
|
|
use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
|
16
|
|
|
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException; |
|
17
|
|
|
use Overtrue\EasySms\Support\Config; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Messenger. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Messenger |
|
23
|
|
|
{ |
|
24
|
|
|
const STATUS_SUCCESS = 'success'; |
|
25
|
|
|
|
|
26
|
|
|
const STATUS_FAILURE = 'failure'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Overtrue\EasySms\EasySms |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $easySms; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Messenger constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Overtrue\EasySms\EasySms $easySms |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function __construct(EasySms $easySms) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->easySms = $easySms; |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Send a message. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to |
|
47
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
48
|
|
|
* @param array $gateways |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException |
|
53
|
|
|
* @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function send(PhoneNumberInterface $to, MessageInterface $message, array $gateways = []) |
|
56
|
|
|
{ |
|
57
|
|
|
if (empty($gateways)) { |
|
58
|
|
|
$gateways = $message->getGateways(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (empty($gateways)) { |
|
62
|
|
|
$gateways = $this->easySms->getConfig()->get('default.gateways', []); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$gateways = $this->formatGateways($gateways); |
|
66
|
|
|
$strategyAppliedGateways = $this->easySms->strategy()->apply($gateways); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$results = []; |
|
69
|
|
|
$isSuccessful = false; |
|
70
|
|
|
foreach ($strategyAppliedGateways as $gateway) { |
|
71
|
|
|
try { |
|
72
|
|
|
$results[$gateway] = [ |
|
73
|
|
|
'status' => self::STATUS_SUCCESS, |
|
74
|
|
|
'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])), |
|
75
|
|
|
]; |
|
76
|
|
|
$isSuccessful = true; |
|
77
|
|
|
|
|
78
|
|
|
break; |
|
79
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
80
|
|
|
$results[$gateway] = [ |
|
81
|
|
|
'status' => self::STATUS_FAILURE, |
|
82
|
|
|
'exception' => $e, |
|
83
|
|
|
]; |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
$results[$gateway] = [ |
|
86
|
|
|
'status' => self::STATUS_FAILURE, |
|
87
|
|
|
'exception' => $e, |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!$isSuccessful) { |
|
93
|
|
|
throw new NoGatewayAvailableException($results); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $results; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array $gateways |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function formatGateways(array $gateways) |
|
105
|
|
|
{ |
|
106
|
|
|
$formatted = []; |
|
107
|
|
|
$config = $this->easySms->getConfig(); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($gateways as $gateway => $setting) { |
|
110
|
|
|
if (is_int($gateway) && is_string($setting)) { |
|
111
|
|
|
$gateway = $setting; |
|
112
|
|
|
$setting = []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$formatted[$gateway] = $setting; |
|
116
|
|
|
$globalSetting = $config->get("gateways.{$gateway}", []); |
|
117
|
|
|
|
|
118
|
|
|
if (is_string($gateway) && !empty($globalSetting) && is_array($setting)) { |
|
119
|
|
|
$formatted[$gateway] = array_merge($globalSetting, $setting); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $formatted; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.