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\Notification\NotificationBuilder; |
14
|
|
|
use Zbox\UnifiedPush\NotificationService\NotificationServices, |
15
|
|
|
Zbox\UnifiedPush\NotificationService\ServiceClientInterface, |
16
|
|
|
Zbox\UnifiedPush\NotificationService\ServiceClientFactory, |
17
|
|
|
Zbox\UnifiedPush\NotificationService\ResponseHandler; |
18
|
|
|
use Zbox\UnifiedPush\Exception\ClientException, |
19
|
|
|
Zbox\UnifiedPush\Exception\RuntimeException; |
20
|
|
|
use Psr\Log\LoggerAwareInterface, |
21
|
|
|
Psr\Log\LoggerInterface, |
22
|
|
|
Psr\Log\NullLogger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Dispatcher |
26
|
|
|
* @package Zbox\UnifiedPush |
27
|
|
|
*/ |
28
|
|
|
class Dispatcher implements LoggerAwareInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $connectionPool; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ServiceClientFactory |
37
|
|
|
*/ |
38
|
|
|
private $clientFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var NotificationBuilder |
42
|
|
|
*/ |
43
|
|
|
private $notificationBuilder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ResponseHandler |
47
|
|
|
*/ |
48
|
|
|
private $responseHandler; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var LoggerInterface |
52
|
|
|
*/ |
53
|
|
|
private $logger; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ServiceClientFactory $clientFactory |
57
|
|
|
* @param NotificationBuilder $notificationBuilder |
58
|
|
|
* @param ResponseHandler $responseHandler |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
ServiceClientFactory $clientFactory, |
62
|
|
|
NotificationBuilder $notificationBuilder, |
63
|
|
|
ResponseHandler $responseHandler |
64
|
|
|
){ |
65
|
|
|
$this->clientFactory = $clientFactory; |
66
|
|
|
$this->notificationBuilder = $notificationBuilder; |
67
|
|
|
$this->responseHandler = $responseHandler; |
68
|
|
|
|
69
|
|
|
$this->setLogger(new NullLogger()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param LoggerInterface $logger |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setLogger(LoggerInterface $logger) |
77
|
|
|
{ |
78
|
|
|
$this->logger = $logger; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param bool $isDevelopment |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setDevelopmentMode($isDevelopment) |
87
|
|
|
{ |
88
|
|
|
$this->clientFactory->setDevelopmentMode($isDevelopment); |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets a service client connection by service name |
94
|
|
|
* |
95
|
|
|
* @param string $serviceName |
96
|
|
|
* @return ServiceClientInterface |
97
|
|
|
*/ |
98
|
|
|
public function getConnection($serviceName) |
99
|
|
|
{ |
100
|
|
|
if (empty($this->connectionPool[$serviceName])) { |
101
|
|
|
$this->initConnection($serviceName); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->connectionPool[$serviceName]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Initialize service client connection by service name |
109
|
|
|
* |
110
|
|
|
* @param string $serviceName |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function initConnection($serviceName) |
114
|
|
|
{ |
115
|
|
|
$connection = $this->clientFactory->createServiceClient($serviceName); |
116
|
|
|
$this->connectionPool[$serviceName] = $connection; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Creates a feedback service connection |
123
|
|
|
* |
124
|
|
|
* @param string $serviceName |
125
|
|
|
* @return ServiceClientInterface |
126
|
|
|
*/ |
127
|
|
|
public function initFeedbackConnection($serviceName) |
128
|
|
|
{ |
129
|
|
|
return $this->clientFactory->createServiceClient($serviceName, true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return ResponseHandler |
134
|
|
|
*/ |
135
|
|
|
public function getResponseHandler() |
136
|
|
|
{ |
137
|
|
|
return $this->responseHandler; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Tries to dispatch all messages to notification service |
142
|
|
|
* |
143
|
|
|
* @param MessageInterface $message |
144
|
|
|
*/ |
145
|
|
|
public function dispatch(MessageInterface $message) |
146
|
|
|
{ |
147
|
|
|
try { |
148
|
|
|
$this->logger->info( |
149
|
|
|
sprintf( |
150
|
|
|
"Dispatching message id: '%s'", |
151
|
|
|
$message->getMessageIdentifier() |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$this->notificationBuilder->buildNotifications($message); |
156
|
|
|
$this->sendNotifications($message->getMessageIdentifier()); |
157
|
|
|
|
158
|
|
|
} catch (ClientException $e) { |
159
|
|
|
$this->logger->error( |
160
|
|
|
sprintf("Client connection error: %s", $e->getMessage()) |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
} catch (RuntimeException $e) { |
164
|
|
|
$this->logger->error( |
165
|
|
|
sprintf("Runtime error: %s", $e->getMessage()) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
} catch (\Exception $e) { |
169
|
|
|
$this->logger->error( |
170
|
|
|
sprintf("Error occurs: %s", $e->getMessage()) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Tries to connect and load feedback data |
177
|
|
|
* |
178
|
|
|
* @return $this |
179
|
|
|
* @throws RuntimeException |
180
|
|
|
* @throws \Exception |
181
|
|
|
*/ |
182
|
|
|
public function loadFeedback() |
183
|
|
|
{ |
184
|
|
|
$serviceName = NotificationServices::APPLE_PUSH_NOTIFICATIONS_SERVICE; |
185
|
|
|
|
186
|
|
|
try { |
187
|
|
|
$this->logger->info(sprintf("Querying the feedback service '%s'", $serviceName)); |
188
|
|
|
|
189
|
|
|
$connection = $this->initFeedbackConnection($serviceName); |
190
|
|
|
|
191
|
|
|
$this |
192
|
|
|
->responseHandler |
193
|
|
|
->addResponse( |
194
|
|
|
$connection->sendRequest() |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
} catch (RuntimeException $e) { |
198
|
|
|
$this->logger->error( |
199
|
|
|
sprintf("Runtime error while acquiring feedback: %s", $e->getMessage()) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
} catch (\Exception $e) { |
203
|
|
|
$this->logger->error( |
204
|
|
|
sprintf("Error occurs while acquiring feedback: %s", $e->getMessage()) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Tries to connect and send a message to notification service |
213
|
|
|
* |
214
|
|
|
* @param string $messageIdentifier |
215
|
|
|
*/ |
216
|
|
|
private function sendNotifications($messageIdentifier) |
217
|
|
|
{ |
218
|
|
|
while ($notification = $this->notificationBuilder->getNotification()) { |
219
|
|
|
$connection = $this->getConnection($notification->getType()); |
220
|
|
|
$connection->setNotification($notification); |
221
|
|
|
|
222
|
|
|
$this |
223
|
|
|
->responseHandler |
224
|
|
|
->addMessageResponse( |
225
|
|
|
$messageIdentifier, |
226
|
|
|
$connection->sendRequest() |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|