|
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 \ArrayIterator(); |
|
|
|
|
|
|
46
|
|
|
$this->setLogger(new NullLogger()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $messageIdentifier |
|
51
|
|
|
* @param ResponseInterface $response |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addIdentifiedResponse($messageIdentifier, ResponseInterface $response) |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty($messageIdentifier)) { |
|
57
|
|
|
throw new InvalidArgumentException('Identifier is required'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->responseCollection->attach($response, $messageIdentifier); |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param ResponseInterface $response |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function addResponse(ResponseInterface $response) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->responseCollection->attach($response); |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function handleResponseCollection() |
|
75
|
|
|
{ |
|
76
|
|
|
$responses = $this->responseCollection; |
|
77
|
|
|
|
|
78
|
|
|
$responses->rewind(); |
|
79
|
|
|
|
|
80
|
|
|
while ($responses->valid()) { |
|
81
|
|
|
$response = $responses->getcurrent(); |
|
|
|
|
|
|
82
|
|
|
$messageId = $responses->getInfo(); |
|
83
|
|
|
$this->handleResponse($response, $messageId); |
|
84
|
|
|
$responses->next(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param ResponseInterface $response |
|
90
|
|
|
* @param string $messageIdentifier |
|
91
|
|
|
*/ |
|
92
|
|
|
public function handleResponse(ResponseInterface $response, $messageIdentifier) |
|
93
|
|
|
{ |
|
94
|
|
|
try { |
|
95
|
|
|
$response->processResponse(); |
|
96
|
|
|
|
|
97
|
|
|
if ($response instanceof ResponseFeedback) { |
|
98
|
|
|
throw new InvalidRecipientException(null, $response->getRecipients()); |
|
99
|
|
|
} |
|
100
|
|
|
} catch (InvalidRecipientException $e) { |
|
101
|
|
|
foreach ($e->getRecipientCollection() as $recipient) { |
|
102
|
|
|
$this->invalidRecipients->append($recipient); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} catch (DispatchMessageException $e) { |
|
106
|
|
|
$this->messageErrors->offsetSet($messageIdentifier, $e->getCode()); |
|
107
|
|
|
|
|
108
|
|
|
$this->logger->warning( |
|
109
|
|
|
sprintf("Dispatch message warning with code %d '%s'", $e->getCode(), $e->getMessage()) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
} catch (MalformedNotificationException $e) { |
|
113
|
|
|
$this->messageErrors->offsetSet($messageIdentifier, $e->getCode()); |
|
114
|
|
|
|
|
115
|
|
|
$this->logger->error( |
|
116
|
|
|
sprintf("Malformed Notification error: %s", $e->getMessage()) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return \ArrayIterator |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getMessageErrors() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->messageErrors; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return \ArrayIterator |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getInvalidRecipients() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->invalidRecipients; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param LoggerInterface $logger |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setLogger(LoggerInterface $logger) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->logger = $logger; |
|
144
|
|
|
return $this; |
|
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..