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 ShortMessageFactoryInterface |
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
|
|
|
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
|
|
|
$this->client = $jetSmsClient; |
83
|
|
|
$this->factory = $shortMessageFactory; |
84
|
|
|
$this->collectionFactory = $shortMessageCollectionFactory; |
|
|
|
|
85
|
|
|
$this->beforeSingleShortMessageCallback = $beforeSingleShortMessageCallback; |
86
|
|
|
$this->afterSingleShortMessageCallback = $afterSingleShortMessageCallback; |
87
|
|
|
$this->beforeMultipleShortMessageCallback = $beforeMultipleShortMessageCallback; |
88
|
|
|
$this->afterMultipleShortMessageCallback = $afterMultipleShortMessageCallback; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Send the given body to the given receivers. |
93
|
|
|
* |
94
|
|
|
* @param string $body The body of the short message. |
95
|
|
|
* @param array|string $receivers The receiver(s) of the message. |
96
|
|
|
* |
97
|
|
|
* @return JetSmsResponseInterface The parsed JetSms response object. |
98
|
|
|
*/ |
99
|
|
|
public function sendShortMessage($receivers, $body) |
100
|
|
|
{ |
101
|
|
|
$shortMessage = $this->factory->create($receivers, $body); |
102
|
|
|
|
103
|
|
|
if(is_callable($this->beforeSingleShortMessageCallback)) { |
104
|
|
|
($this->beforeSingleShortMessageCallback)($shortMessage); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$response = $this->client->sendShortMessage($shortMessage); |
108
|
|
|
|
109
|
|
|
if(is_callable($this->afterSingleShortMessageCallback)) { |
110
|
|
|
($this->afterSingleShortMessageCallback)($response, $shortMessage); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Send the given short messages. |
118
|
|
|
* |
119
|
|
|
* @param array $messages An array containing short message arrays. |
120
|
|
|
* |
121
|
|
|
* @return JetSmsResponseInterface The parsed JetSms response object. |
122
|
|
|
*/ |
123
|
|
|
public function sendShortMessages(array $messages) |
124
|
|
|
{ |
125
|
|
|
$collection = $this->collectionFactory->create(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
if(is_callable($this->beforeMultipleShortMessageCallback)) { |
128
|
|
|
($this->beforeMultipleShortMessageCallback)($collection); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
foreach ($messages as $message) { |
132
|
|
|
$collection->push($this->factory->create( |
|
|
|
|
133
|
|
|
$message['recipient'], |
134
|
|
|
$message['message'] |
135
|
|
|
)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$response = $this->client->sendShortMessages($collection); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if(is_callable($this->afterMultipleShortMessageCallback)) { |
141
|
|
|
($this->afterMultipleShortMessageCallback)($response, $collection); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $response; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..