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\NotificationService; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
13
|
|
|
use Zbox\UnifiedPush\NotificationService\APNS\ResponseFeedback; |
14
|
|
|
use Zbox\UnifiedPush\Exception\InvalidRecipientException, |
15
|
|
|
Zbox\UnifiedPush\Exception\DispatchMessageException, |
16
|
|
|
Zbox\UnifiedPush\Exception\MalformedNotificationException; |
17
|
|
|
use Psr\Log\LoggerAwareInterface, |
18
|
|
|
Psr\Log\LoggerInterface, |
19
|
|
|
Psr\Log\NullLogger; |
20
|
|
|
|
21
|
|
|
class ResponseHandler implements LoggerAwareInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \SplObjectStorage |
25
|
|
|
*/ |
26
|
|
|
private $responseCollection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \ArrayIterator |
30
|
|
|
*/ |
31
|
|
|
private $invalidRecipients; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \ArrayIterator |
35
|
|
|
*/ |
36
|
|
|
private $messageErrors; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
private $logger; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->responseCollection = new \SplObjectStorage(); |
46
|
|
|
$this->invalidRecipients = new \ArrayIterator(); |
47
|
|
|
$this->messageErrors = new \ArrayIterator(); |
48
|
|
|
|
49
|
|
|
$this->setLogger(new NullLogger()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $messageIdentifier |
54
|
|
|
* @param ResponseInterface $response |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function addIdentifiedResponse($messageIdentifier, ResponseInterface $response) |
58
|
|
|
{ |
59
|
|
|
if (empty($messageIdentifier)) { |
60
|
|
|
throw new InvalidArgumentException('Identifier is required'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->responseCollection->attach($response, $messageIdentifier); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ResponseInterface $response |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function addResponse(ResponseInterface $response) |
72
|
|
|
{ |
73
|
|
|
$this->responseCollection->attach($response); |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function handleResponseCollection() |
78
|
|
|
{ |
79
|
|
|
$responses = $this->responseCollection; |
80
|
|
|
|
81
|
|
|
$responses->rewind(); |
82
|
|
|
|
83
|
|
|
while ($responses->valid()) { |
84
|
|
|
$response = $responses->current(); |
85
|
|
|
$messageId = $responses->getInfo(); |
86
|
|
|
$this->handleResponse($response, $messageId); |
87
|
|
|
$responses->next(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ResponseInterface $response |
93
|
|
|
* @param string $messageIdentifier |
94
|
|
|
*/ |
95
|
|
|
public function handleResponse(ResponseInterface $response, $messageIdentifier) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
$response->processResponse(); |
99
|
|
|
|
100
|
|
|
if ($response instanceof ResponseFeedback) { |
101
|
|
|
throw new InvalidRecipientException(null, $response->getRecipients()); |
102
|
|
|
} |
103
|
|
|
} catch (InvalidRecipientException $e) { |
104
|
|
|
foreach ($e->getRecipientCollection() as $recipient) { |
105
|
|
|
$this->invalidRecipients->append($recipient); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} catch (DispatchMessageException $e) { |
109
|
|
|
$this->messageErrors->offsetSet($messageIdentifier, $e->getCode()); |
110
|
|
|
|
111
|
|
|
$this->logger->warning( |
112
|
|
|
sprintf("Dispatch message warning with code %d '%s'", $e->getCode(), $e->getMessage()) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
} catch (MalformedNotificationException $e) { |
116
|
|
|
$this->messageErrors->offsetSet($messageIdentifier, $e->getCode()); |
117
|
|
|
|
118
|
|
|
$this->logger->error( |
119
|
|
|
sprintf("Malformed Notification error: %s", $e->getMessage()) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return \ArrayIterator |
126
|
|
|
*/ |
127
|
|
|
public function getMessageErrors() |
128
|
|
|
{ |
129
|
|
|
return $this->messageErrors; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return \ArrayIterator |
134
|
|
|
*/ |
135
|
|
|
public function getInvalidRecipients() |
136
|
|
|
{ |
137
|
|
|
return $this->invalidRecipients; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param LoggerInterface $logger |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setLogger(LoggerInterface $logger) |
145
|
|
|
{ |
146
|
|
|
$this->logger = $logger; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|