|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Erdemkeren\JetSms; |
|
4
|
|
|
|
|
5
|
|
|
use Erdemkeren\JetSms\Http\Clients\JetSmsClientInterface; |
|
6
|
|
|
use Erdemkeren\JetSms\Http\Responses\JetSmsResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class JetSmsService. |
|
10
|
|
|
*/ |
|
11
|
|
|
final class JetSmsService |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The jet sms client implementation. |
|
15
|
|
|
* |
|
16
|
|
|
* @var JetSmsClientInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The short message factory implementation. |
|
22
|
|
|
* |
|
23
|
|
|
* @var ShortMessageFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $factory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The short message collection factory implementation. |
|
29
|
|
|
* |
|
30
|
|
|
* @var ShortMessageCollectionFactoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $collectionFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The before callback which will be called before sending single messages. |
|
36
|
|
|
* |
|
37
|
|
|
* @var callable|null |
|
38
|
|
|
*/ |
|
39
|
|
|
private $beforeSingleShortMessageCallback; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The after callback which will be called before sending single messages. |
|
43
|
|
|
* |
|
44
|
|
|
* @var callable|null |
|
45
|
|
|
*/ |
|
46
|
|
|
private $afterSingleShortMessageCallback; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The before callback which will be called before sending multiple messages. |
|
50
|
|
|
* |
|
51
|
|
|
* @var callable|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private $beforeMultipleShortMessageCallback; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The after callback which will be called after sending multiple messages. |
|
57
|
|
|
* |
|
58
|
|
|
* @var callable|null |
|
59
|
|
|
*/ |
|
60
|
|
|
private $afterMultipleShortMessageCallback; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* JetSmsService constructor. |
|
64
|
|
|
* |
|
65
|
|
|
* @param JetSmsClientInterface $jetSmsClient |
|
66
|
|
|
* @param ShortMessageFactoryInterface $shortMessageFactory |
|
67
|
|
|
* @param ShortMessageCollectionFactoryInterface $shortMessageCollectionFactory |
|
68
|
|
|
* @param callable|null $beforeSingleShortMessageCallback |
|
69
|
|
|
* @param callable|null $afterSingleShortMessageCallback |
|
70
|
|
|
* @param callable|null $beforeMultipleShortMessageCallback |
|
71
|
|
|
* @param callable|null $afterMultipleShortMessageCallback |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function __construct( |
|
74
|
|
|
JetSmsClientInterface $jetSmsClient, |
|
75
|
|
|
ShortMessageFactoryInterface $shortMessageFactory, |
|
76
|
|
|
ShortMessageCollectionFactoryInterface $shortMessageCollectionFactory, |
|
77
|
|
|
$beforeSingleShortMessageCallback = null, |
|
78
|
|
|
$afterSingleShortMessageCallback = null, |
|
79
|
|
|
$beforeMultipleShortMessageCallback = null, |
|
80
|
|
|
$afterMultipleShortMessageCallback = null |
|
81
|
|
|
) { |
|
82
|
4 |
|
$this->client = $jetSmsClient; |
|
83
|
4 |
|
$this->factory = $shortMessageFactory; |
|
84
|
4 |
|
$this->collectionFactory = $shortMessageCollectionFactory; |
|
85
|
4 |
|
$this->beforeSingleShortMessageCallback = $beforeSingleShortMessageCallback; |
|
86
|
4 |
|
$this->afterSingleShortMessageCallback = $afterSingleShortMessageCallback; |
|
87
|
4 |
|
$this->beforeMultipleShortMessageCallback = $beforeMultipleShortMessageCallback; |
|
88
|
4 |
|
$this->afterMultipleShortMessageCallback = $afterMultipleShortMessageCallback; |
|
89
|
4 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Send the given body to the given receivers. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array|string|ShortMessage $receivers The receiver(s) of the message or the message object. |
|
95
|
|
|
* @param string|null $body The body of the message or null when using short message object. |
|
96
|
|
|
* |
|
97
|
|
|
* @return JetSmsResponseInterface The parsed JetSms response object. |
|
98
|
|
|
*/ |
|
99
|
3 |
|
public function sendShortMessage($receivers, $body = null) |
|
100
|
|
|
{ |
|
101
|
3 |
|
if (! $receivers instanceof ShortMessage) { |
|
102
|
3 |
|
$receivers = $this->factory->create($receivers, $body); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
if (is_callable($this->beforeSingleShortMessageCallback)) { |
|
106
|
1 |
|
call_user_func_array($this->beforeSingleShortMessageCallback, [$receivers]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
$response = $this->client->sendShortMessage($receivers); |
|
110
|
|
|
|
|
111
|
3 |
|
if (is_callable($this->afterSingleShortMessageCallback)) { |
|
112
|
1 |
|
call_user_func_array($this->afterSingleShortMessageCallback, [$response, $receivers]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
return $response; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Send the given short messages. |
|
120
|
|
|
* |
|
121
|
|
|
* @param array|ShortMessageCollection $messages An array containing short message arrays or collection. |
|
122
|
|
|
* |
|
123
|
|
|
* @return JetSmsResponseInterface The parsed JetSms response object. |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function sendShortMessages($messages) |
|
126
|
|
|
{ |
|
127
|
2 |
|
if (! $messages instanceof ShortMessageCollection) { |
|
128
|
2 |
|
$collection = $this->collectionFactory->create(); |
|
129
|
|
|
|
|
130
|
2 |
|
foreach ($messages as $message) { |
|
131
|
2 |
|
$collection->push($this->factory->create( |
|
132
|
2 |
|
$message['recipient'], |
|
133
|
2 |
|
$message['message'] |
|
134
|
|
|
)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
$messages = $collection; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
if (is_callable($this->beforeMultipleShortMessageCallback)) { |
|
141
|
1 |
|
call_user_func_array($this->beforeMultipleShortMessageCallback, [$messages]); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
$response = $this->client->sendShortMessages($messages); |
|
145
|
|
|
|
|
146
|
2 |
|
if (is_callable($this->afterMultipleShortMessageCallback)) { |
|
147
|
1 |
|
call_user_func_array($this->afterMultipleShortMessageCallback, [$response, $messages]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
return $response; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|