|
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\MPNS; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ResponseInterface; |
|
13
|
|
|
use Zbox\UnifiedPush\Message\RecipientDevice; |
|
14
|
|
|
use Zbox\UnifiedPush\Exception\InvalidRecipientException; |
|
15
|
|
|
use Zbox\UnifiedPush\Exception\DispatchMessageException; |
|
16
|
|
|
use Zbox\UnifiedPush\Exception\MalformedNotificationException; |
|
17
|
|
|
use Zbox\UnifiedPush\Exception\RuntimeException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Response |
|
21
|
|
|
* @package Zbox\UnifiedPush\NotificationService\MPNS |
|
22
|
|
|
*/ |
|
23
|
|
|
class Response implements ResponseInterface |
|
24
|
|
|
{ |
|
25
|
|
|
const REQUEST_HAS_SUCCEED_CODE = 200; |
|
26
|
|
|
const MALFORMED_NOTIFICATION_CODE = 400; |
|
27
|
|
|
const AUTHENTICATION_ERROR_CODE = 401; |
|
28
|
|
|
const INVALID_RECIPIENT_ERROR_CODE = 404; |
|
29
|
|
|
const INVALID_METHOD_ERROR_CODE = 405; |
|
30
|
|
|
const QUOTA_EXCEEDED_ERROR_CODE = 406; |
|
31
|
|
|
const DEVICE_INACTIVE_ERROR_CODE = 412; |
|
32
|
|
|
const SERVER_UNAVAILABLE_ERROR_CODE = 503; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Buzz\Message\Response |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $response; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \ArrayIterator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $recipients; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Buzz\Message\Response $response |
|
46
|
|
|
* @param \ArrayIterator $recipients |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(\Buzz\Message\Response $response, \ArrayIterator $recipients) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->response = $response; |
|
51
|
|
|
$this->recipients = $recipients; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function processResponse() |
|
58
|
|
|
{ |
|
59
|
|
|
$statusCode = $this->response->getStatusCode(); |
|
60
|
|
|
$this->checkResponseCode($statusCode, $this->recipients); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Checks if response has succeed code or request was rejected |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $responseCode |
|
67
|
|
|
* @param \ArrayIterator $recipients |
|
68
|
|
|
* @throws \Zbox\UnifiedPush\Exception\MalformedNotificationException |
|
69
|
|
|
* @throws \Zbox\UnifiedPush\Exception\DispatchMessageException |
|
70
|
|
|
* @throws \Zbox\UnifiedPush\Exception\RuntimeException |
|
71
|
|
|
*/ |
|
72
|
|
|
private function checkResponseCode($responseCode, \ArrayIterator $recipients) |
|
73
|
|
|
{ |
|
74
|
|
|
switch ($responseCode) { |
|
75
|
|
|
case self::REQUEST_HAS_SUCCEED_CODE: |
|
76
|
|
|
break; |
|
77
|
|
|
|
|
78
|
|
|
case self::MALFORMED_NOTIFICATION_CODE: |
|
79
|
|
|
throw new MalformedNotificationException( |
|
80
|
|
|
"Notification request with a bad XML document or malformed notification URI" |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
case self::AUTHENTICATION_ERROR_CODE: |
|
84
|
|
|
throw new DispatchMessageException( |
|
85
|
|
|
"Sending this notification is unauthorized", self::AUTHENTICATION_ERROR_CODE |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
case self::INVALID_RECIPIENT_ERROR_CODE: |
|
89
|
|
|
$recipients->current()->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED); |
|
90
|
|
|
|
|
91
|
|
|
throw new InvalidRecipientException( |
|
92
|
|
|
"The subscription is invalid and is not present on the Push Notification Service", |
|
93
|
|
|
$recipients |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
case self::INVALID_METHOD_ERROR_CODE: |
|
97
|
|
|
throw new DispatchMessageException( |
|
98
|
|
|
"Invalid method. Only POST is allowed when sending a notification request", |
|
99
|
|
|
self::INVALID_METHOD_ERROR_CODE |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
case self::QUOTA_EXCEEDED_ERROR_CODE: |
|
103
|
|
|
throw new DispatchMessageException( |
|
104
|
|
|
"Unauthenticated service has reached the per-day throttling limit or there are many notifications per second", |
|
105
|
|
|
self::QUOTA_EXCEEDED_ERROR_CODE |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
case self::DEVICE_INACTIVE_ERROR_CODE: |
|
109
|
|
|
throw new DispatchMessageException( |
|
110
|
|
|
"The device is in a disconnected state", |
|
111
|
|
|
self::DEVICE_INACTIVE_ERROR_CODE |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
case self::SERVER_UNAVAILABLE_ERROR_CODE: |
|
115
|
|
|
throw new DispatchMessageException( |
|
116
|
|
|
"The Push Notification Service is unable to process the request", |
|
117
|
|
|
self::SERVER_UNAVAILABLE_ERROR_CODE |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
default: |
|
121
|
|
|
throw new RuntimeException( |
|
122
|
|
|
"Unknown error occurred while sending notification." |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|