|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\Sms; |
|
3
|
|
|
|
|
4
|
|
|
class SmsManager |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* Sms Configuration. |
|
8
|
|
|
* |
|
9
|
|
|
* @var null|object |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $config = null; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Sms Driver Settings. |
|
15
|
|
|
* |
|
16
|
|
|
* @var null|object |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $settings = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sms Driver Name. |
|
22
|
|
|
* |
|
23
|
|
|
* @var null|string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $driver = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* SmsManager constructor. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->config = config('sms'); |
|
33
|
|
|
$this->withDriver($this->config['default']); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Change the driver on the fly. |
|
38
|
|
|
* |
|
39
|
|
|
* @param $driver |
|
40
|
|
|
* @return $this |
|
41
|
|
|
* @throws \Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public function withDriver($driver) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->validateDriver($driver); |
|
46
|
|
|
|
|
47
|
|
|
$this->driver = $driver; |
|
48
|
|
|
$this->settings = $this->config['drivers'][$this->driver]; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Send message. |
|
55
|
|
|
* |
|
56
|
|
|
* @param $message |
|
57
|
|
|
* @param $callback |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public function send($message, $callback) |
|
62
|
|
|
{ |
|
63
|
|
|
$driverObj = $this->getDriverInstance(); |
|
64
|
|
|
$driverObj->message($message); |
|
65
|
|
|
|
|
66
|
|
|
call_user_func($callback, $driverObj); |
|
67
|
|
|
|
|
68
|
|
|
return $driverObj->send(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Generate driver instance. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getDriverInstance() |
|
77
|
|
|
{ |
|
78
|
|
|
$class = $this->config['map'][$this->driver]; |
|
79
|
|
|
return new $class($this->settings); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Validate Parameters before sending. |
|
84
|
|
|
* |
|
85
|
|
|
* @param $driver |
|
86
|
|
|
* @throws \Exception |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function validateDriver($driver) |
|
89
|
|
|
{ |
|
90
|
|
|
if (empty($driver)) { |
|
91
|
|
|
throw new \Exception("Driver not selected or default driver does not exist."); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (empty($this->config['drivers'][$driver]) || empty($this->config['map'][$driver])) { |
|
95
|
|
|
throw new \Exception("Driver not found in config file. Try updating the package."); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!class_exists($this->config['map'][$driver])) { |
|
99
|
|
|
throw new \Exception("Driver source not found. Please update the package."); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!($this->getDriverInstance() instanceof Contracts\DriverInterface)) { |
|
103
|
|
|
throw new \Exception("Driver must be an instance of Contracts\DriverInterface."); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|