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