1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageInterface; |
13
|
|
|
use Zbox\UnifiedPush\Message\MessageCollection; |
14
|
|
|
use Zbox\UnifiedPush\Notification\Notification; |
15
|
|
|
use Zbox\UnifiedPush\Notification\NotificationBuilder; |
16
|
|
|
use Zbox\UnifiedPush\NotificationService\NotificationServices, |
17
|
|
|
Zbox\UnifiedPush\NotificationService\ServiceClientInterface, |
18
|
|
|
Zbox\UnifiedPush\NotificationService\ServiceClientFactory, |
19
|
|
|
Zbox\UnifiedPush\NotificationService\ResponseHandler; |
20
|
|
|
use Zbox\UnifiedPush\Exception\ClientException, |
21
|
|
|
Zbox\UnifiedPush\Exception\RuntimeException; |
22
|
|
|
use Psr\Log\LoggerAwareInterface, |
23
|
|
|
Psr\Log\LoggerInterface, |
24
|
|
|
Psr\Log\NullLogger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Dispatcher |
28
|
|
|
* @package Zbox\UnifiedPush |
29
|
|
|
*/ |
30
|
|
|
class Dispatcher implements LoggerAwareInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $connectionPool; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ServiceClientFactory |
39
|
|
|
*/ |
40
|
|
|
private $clientFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var NotificationBuilder |
44
|
|
|
*/ |
45
|
|
|
private $notificationBuilder; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ResponseHandler |
49
|
|
|
*/ |
50
|
|
|
private $responseHandler; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var LoggerInterface |
54
|
|
|
*/ |
55
|
|
|
private $logger; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ServiceClientFactory $clientFactory |
59
|
|
|
* @param NotificationBuilder $notificationBuilder |
60
|
|
|
* @param ResponseHandler $responseHandler |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
ServiceClientFactory $clientFactory, |
64
|
|
|
NotificationBuilder $notificationBuilder, |
65
|
|
|
ResponseHandler $responseHandler |
66
|
|
|
){ |
67
|
|
|
$this->clientFactory = $clientFactory; |
68
|
|
|
$this->notificationBuilder = $notificationBuilder; |
69
|
|
|
$this->responseHandler = $responseHandler; |
70
|
|
|
|
71
|
|
|
$this->setLogger(new NullLogger()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param LoggerInterface $logger |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setLogger(LoggerInterface $logger) |
79
|
|
|
{ |
80
|
|
|
$this->logger = $logger; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param bool $isDevelopment |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setDevelopmentMode($isDevelopment) |
89
|
|
|
{ |
90
|
|
|
$this->clientFactory->setDevelopmentMode($isDevelopment); |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets a service client connection by service name |
96
|
|
|
* |
97
|
|
|
* @param string $serviceName |
98
|
|
|
* @return ServiceClientInterface |
99
|
|
|
*/ |
100
|
|
|
public function getConnection($serviceName) |
101
|
|
|
{ |
102
|
|
|
if (empty($this->connectionPool[$serviceName])) { |
103
|
|
|
$this->initConnection($serviceName); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->connectionPool[$serviceName]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Initialize service client connection by service name |
111
|
|
|
* |
112
|
|
|
* @param string $serviceName |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function initConnection($serviceName) |
116
|
|
|
{ |
117
|
|
|
$connection = $this->clientFactory->createServiceClient($serviceName); |
118
|
|
|
$this->connectionPool[$serviceName] = $connection; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Creates a feedback service connection |
125
|
|
|
* |
126
|
|
|
* @param string $serviceName |
127
|
|
|
* @return ServiceClientInterface |
128
|
|
|
*/ |
129
|
|
|
public function initFeedbackConnection($serviceName) |
130
|
|
|
{ |
131
|
|
|
return $this->clientFactory->createServiceClient($serviceName, true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return ResponseHandler |
136
|
|
|
*/ |
137
|
|
|
public function getResponseHandler() |
138
|
|
|
{ |
139
|
|
|
return $this->responseHandler; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Build notification and send it to notification service |
144
|
|
|
* |
145
|
|
|
* @param MessageInterface $message |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function dispatch(MessageInterface $message) |
149
|
|
|
{ |
150
|
|
|
$builder = $this->notificationBuilder; |
151
|
|
|
|
152
|
|
|
$notifications = $builder->buildNotifications($message); |
153
|
|
|
|
154
|
|
|
/** @var Notification $notification */ |
155
|
|
|
foreach ($notifications as $notification) { |
156
|
|
|
$this->sendNotification($notification); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Tries to dispatch all messages to notification service |
164
|
|
|
* |
165
|
|
|
* @param MessageCollection $messages |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function dispatchAll(MessageCollection $messages) |
169
|
|
|
{ |
170
|
|
|
$collection = $messages->getMessageCollection(); |
171
|
|
|
|
172
|
|
|
while ($collection->valid()) { |
173
|
|
|
$message = $collection->current(); |
174
|
|
|
$this->dispatch($message); |
175
|
|
|
$collection->next(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Tries to connect and send a notification |
183
|
|
|
* |
184
|
|
|
* @param Notification $notification |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function sendNotification(Notification $notification) |
188
|
|
|
{ |
189
|
|
|
try { |
190
|
|
|
$connection = $this->getConnection($notification->getType()); |
191
|
|
|
$connection->setNotification($notification); |
192
|
|
|
|
193
|
|
|
$this->logger->info( |
194
|
|
|
sprintf( |
195
|
|
|
"Dispatching notification id: %s", |
196
|
|
|
$notification->getIdentifier() |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$this |
201
|
|
|
->responseHandler |
202
|
|
|
->addIdentifiedResponse( |
203
|
|
|
$notification->getIdentifier(), |
204
|
|
|
$connection->sendRequest() |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
return true; |
208
|
|
|
|
209
|
|
|
} catch (ClientException $e) { |
210
|
|
|
$this->logger->error( |
211
|
|
|
sprintf("Client connection error: %s", $e->getMessage()) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
} catch (RuntimeException $e) { |
215
|
|
|
$this->logger->error( |
216
|
|
|
sprintf("Runtime error: %s", $e->getMessage()) |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
} catch (\Exception $e) { |
220
|
|
|
$this->logger->error( |
221
|
|
|
sprintf("Error occurs: %s", $e->getMessage()) |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Tries to connect and load feedback data |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
* @throws RuntimeException |
233
|
|
|
* @throws \Exception |
234
|
|
|
*/ |
235
|
|
|
public function loadFeedback() |
236
|
|
|
{ |
237
|
|
|
$serviceName = NotificationServices::APPLE_PUSH_NOTIFICATIONS_SERVICE; |
238
|
|
|
|
239
|
|
|
try { |
240
|
|
|
$this->logger->info(sprintf("Querying the feedback service '%s'", $serviceName)); |
241
|
|
|
|
242
|
|
|
$connection = $this->initFeedbackConnection($serviceName); |
243
|
|
|
|
244
|
|
|
$this |
245
|
|
|
->responseHandler |
246
|
|
|
->addResponse( |
247
|
|
|
$connection->sendRequest() |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
} catch (RuntimeException $e) { |
251
|
|
|
$this->logger->error( |
252
|
|
|
sprintf("Runtime error while acquiring feedback: %s", $e->getMessage()) |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
} catch (\Exception $e) { |
256
|
|
|
$this->logger->error( |
257
|
|
|
sprintf("Error occurs while acquiring feedback: %s", $e->getMessage()) |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|