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
|
3 |
|
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
|
3 |
|
$this->client = $jetSmsClient; |
83
|
3 |
|
$this->factory = $shortMessageFactory; |
84
|
3 |
|
$this->collectionFactory = $shortMessageCollectionFactory; |
85
|
3 |
|
$this->beforeSingleShortMessageCallback = $beforeSingleShortMessageCallback; |
86
|
3 |
|
$this->afterSingleShortMessageCallback = $afterSingleShortMessageCallback; |
87
|
3 |
|
$this->beforeMultipleShortMessageCallback = $beforeMultipleShortMessageCallback; |
88
|
3 |
|
$this->afterMultipleShortMessageCallback = $afterMultipleShortMessageCallback; |
89
|
3 |
|
} |
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
|
2 |
|
public function sendShortMessage($receivers, $body = null) |
100
|
|
|
{ |
101
|
2 |
|
if (! $receivers instanceof ShortMessage) { |
102
|
2 |
|
$receivers = $this->factory->create($receivers, $body); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
if (is_callable($this->beforeSingleShortMessageCallback)) { |
106
|
|
|
call_user_func_array($this->beforeSingleShortMessageCallback, [$receivers]); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
$response = $this->client->sendShortMessage($receivers); |
110
|
|
|
|
111
|
2 |
|
if (is_callable($this->afterSingleShortMessageCallback)) { |
112
|
|
|
call_user_func_array($this->afterSingleShortMessageCallback, [$response, $receivers]); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
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
|
1 |
|
public function sendShortMessages($messages) |
126
|
|
|
{ |
127
|
1 |
|
if (! $messages instanceof ShortMessageCollection) { |
128
|
1 |
|
$collection = $this->collectionFactory->create(); |
129
|
|
|
|
130
|
1 |
|
foreach ($messages as $message) { |
131
|
1 |
|
$collection->push($this->factory->create( |
132
|
1 |
|
$message['recipient'], |
133
|
1 |
|
$message['message'] |
134
|
|
|
)); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$messages = $collection; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
if (is_callable($this->beforeMultipleShortMessageCallback)) { |
141
|
|
|
call_user_func_array($this->beforeMultipleShortMessageCallback, [$messages]); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
$response = $this->client->sendShortMessages($messages); |
145
|
|
|
|
146
|
1 |
|
if (is_callable($this->afterMultipleShortMessageCallback)) { |
147
|
|
|
call_user_func_array($this->afterMultipleShortMessageCallback, [$response, $messages]); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return $response; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|