|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/easy-sms. |
|
5
|
|
|
* (c) overtrue <[email protected]> |
|
6
|
|
|
* This source file is subject to the MIT license that is bundled |
|
7
|
|
|
* with this source code in the file LICENSE. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Overtrue\EasySms; |
|
11
|
|
|
|
|
12
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Messenger. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Messenger |
|
18
|
|
|
{ |
|
19
|
|
|
const STATUS_SUCCESS = 'success'; |
|
20
|
|
|
const STATUS_ERRED = 'erred'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Overtrue\EasySms\EasySms |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $easySms; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Messenger constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Overtrue\EasySms\EasySms $easySms |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(EasySms $easySms) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->easySms = $easySms; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Send a message. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $to |
|
41
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
42
|
|
|
* @param array $gateways |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function send($to, MessageInterface $message, array $gateways = []) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!($message instanceof MessageInterface)) { |
|
49
|
|
|
$message = new Message([ |
|
50
|
|
|
'content' => $message, |
|
51
|
|
|
'template' => $message, |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$results = []; |
|
56
|
|
|
|
|
57
|
|
|
if (empty($gateways)) { |
|
58
|
|
|
$gateways = $message->getGateways(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$gateways = $this->formatGateways($gateways); |
|
62
|
|
|
$strategyAppliedGateways = $this->easySms->strategy()->apply($gateways); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($strategyAppliedGateways as $gateway) { |
|
65
|
|
|
try { |
|
66
|
|
|
$result[$gateway] = [ |
|
|
|
|
|
|
67
|
|
|
'status' => self::STATUS_SUCCESS, |
|
68
|
|
|
'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])), |
|
69
|
|
|
]; |
|
70
|
|
|
} catch (GatewayErrorException $e) { |
|
|
|
|
|
|
71
|
|
|
$result[$gateway] = [ |
|
|
|
|
|
|
72
|
|
|
'status' => self::STATUS_ERRED, |
|
73
|
|
|
'exception' => $e, |
|
74
|
|
|
]; |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $results; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param array $gateways |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function formatGateways(array $gateways) |
|
88
|
|
|
{ |
|
89
|
|
|
$formatted = []; |
|
90
|
|
|
$config = $this->easySms->getConfig(); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($gateways as $gateway => $setting) { |
|
93
|
|
|
if (is_integer($gateway) && is_string($setting)) { |
|
94
|
|
|
$gateway = $setting; |
|
95
|
|
|
$setting = []; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$globalSetting = $config->get("gateways.{$gateway}", []); |
|
99
|
|
|
|
|
100
|
|
|
if (is_string($gateway) && !empty($globalSetting) && is_array($setting)) { |
|
101
|
|
|
$formatted[$gateway] = array_merge($globalSetting, $setting); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $formatted; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.