1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tzsk\Sms; |
4
|
|
|
|
5
|
|
|
class SmsManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Sms Configuration. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sms Driver Settings. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $settings; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sms Driver Name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $driver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SmsBuilder |
30
|
|
|
*/ |
31
|
|
|
protected $builder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SmsManager constructor. |
35
|
|
|
* |
36
|
|
|
* @param array $config |
37
|
|
|
*/ |
38
|
|
|
public function __construct($config) |
39
|
|
|
{ |
40
|
|
|
$this->config = $config; |
41
|
|
|
$this->setBuilder(new SmsBuilder()); |
42
|
|
|
$this->via($this->config['default']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string|array $recipients |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function to($recipients) |
50
|
|
|
{ |
51
|
|
|
$this->builder->to($recipients); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Change the driver on the fly. |
58
|
|
|
* |
59
|
|
|
* @param $driver |
60
|
|
|
* @return $this |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function via($driver) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->driver = $driver; |
66
|
|
|
$this->validateDriver(); |
67
|
|
|
$this->builder->via($driver); |
68
|
|
|
$this->settings = $this->config['drivers'][$driver]; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Send message. |
75
|
|
|
* |
76
|
|
|
* @param $message |
77
|
|
|
* @param $callback |
78
|
|
|
* @return mixed |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function send($message, $callback = null) |
82
|
|
|
{ |
83
|
|
|
if ($message instanceof SmsBuilder) { |
84
|
|
|
return $this->setBuilder($message)->dispatch(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->builder->send($message); |
88
|
|
|
if (! $callback) { |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$driver = $this->getDriverInstance(); |
93
|
|
|
$driver->message($message); |
94
|
|
|
call_user_func($callback, $driver); |
95
|
|
|
|
96
|
|
|
return $driver->send(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function dispatch() |
103
|
|
|
{ |
104
|
|
|
$this->driver = $this->builder->getDriver() ?: $this->driver; |
105
|
|
|
if (empty($this->driver)) { |
106
|
|
|
$this->via($this->config['default']); |
107
|
|
|
} |
108
|
|
|
$driver = $this->getDriverInstance(); |
109
|
|
|
$driver->message($this->builder->getBody()); |
110
|
|
|
$driver->to($this->builder->getRecipients()); |
111
|
|
|
|
112
|
|
|
return $driver->send(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param SmsBuilder $builder |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
protected function setBuilder(SmsBuilder $builder) |
120
|
|
|
{ |
121
|
|
|
$this->builder = $builder; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate driver instance. |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
protected function getDriverInstance() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$this->validateDriver(); |
134
|
|
|
$class = $this->config['map'][$this->driver]; |
135
|
|
|
|
136
|
|
|
return new $class($this->settings); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Validate Parameters before sending. |
141
|
|
|
* |
142
|
|
|
* @throws \Exception |
143
|
|
|
*/ |
144
|
|
|
protected function validateDriver() |
145
|
|
|
{ |
146
|
|
|
if (empty($this->driver)) { |
147
|
|
|
throw new \Exception('Driver not selected or default driver does not exist.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (empty($this->config['drivers'][$this->driver]) || empty($this->config['map'][$this->driver])) { |
151
|
|
|
throw new \Exception('Driver not found in config file. Try updating the package.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (! class_exists($this->config['map'][$this->driver])) { |
155
|
|
|
throw new \Exception('Driver source not found. Please update the package.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$reflect = new \ReflectionClass($this->config['map'][$this->driver]); |
159
|
|
|
|
160
|
|
|
if (! $reflect->implementsInterface(Contracts\DriverInterface::class)) { |
161
|
|
|
throw new \Exception("Driver must be an instance of Contracts\DriverInterface."); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.