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\APNS; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ResponseInterface; |
13
|
|
|
use Zbox\UnifiedPush\Message\RecipientDevice; |
14
|
|
|
use Zbox\UnifiedPush\Exception\DispatchMessageException; |
15
|
|
|
use Zbox\UnifiedPush\Exception\InvalidRecipientException; |
16
|
|
|
use Zbox\UnifiedPush\Exception\RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Response |
20
|
|
|
* @package Zbox\UnifiedPush\NotificationService\APNS |
21
|
|
|
*/ |
22
|
|
|
class Response implements ResponseInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* APNS error-response packet length |
26
|
|
|
*/ |
27
|
|
|
const ERROR_RESPONSE_LENGTH = 6; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* APNS error-response packet command |
31
|
|
|
*/ |
32
|
|
|
const ERROR_RESPONSE_COMMAND = 8; |
33
|
|
|
|
34
|
|
|
const NO_ERROR = 0; |
35
|
|
|
const ERROR_PROCESSING = 1; |
36
|
|
|
const ERROR_MISSING_DEVICE_TOKEN = 2; |
37
|
|
|
const ERROR_MISSING_TOPIC = 3; |
38
|
|
|
const ERROR_MISSING_PAYLOAD = 4; |
39
|
|
|
const ERROR_INVALID_TOKEN_SIZE = 5; |
40
|
|
|
const ERROR_INVALID_TOPIC_SIZE = 6; |
41
|
|
|
const ERROR_INVALID_PAYLOAD_SIZE = 7; |
42
|
|
|
const ERROR_INVALID_TOKEN = 8; |
43
|
|
|
const ERROR_SHUTDOWN = 10; |
44
|
|
|
const ERROR_UNKNOWN = 255; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private static $responseDescription = array( |
50
|
|
|
self::NO_ERROR => 'No errors encountered', |
51
|
|
|
self::ERROR_PROCESSING => 'Processing error', |
52
|
|
|
self::ERROR_MISSING_DEVICE_TOKEN => 'Missing device token', |
53
|
|
|
self::ERROR_MISSING_TOPIC => 'Missing topic', |
54
|
|
|
self::ERROR_MISSING_PAYLOAD => 'Missing payload', |
55
|
|
|
self::ERROR_INVALID_TOKEN_SIZE => 'Invalid token size', |
56
|
|
|
self::ERROR_INVALID_TOPIC_SIZE => 'Invalid topic size', |
57
|
|
|
self::ERROR_INVALID_PAYLOAD_SIZE => 'Invalid payload size', |
58
|
|
|
self::ERROR_INVALID_TOKEN => 'Invalid token', |
59
|
|
|
self::ERROR_SHUTDOWN => 'Shutdown', |
60
|
|
|
self::ERROR_UNKNOWN => 'None (unknown)', |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $rawResponse; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var \ArrayIterator |
70
|
|
|
*/ |
71
|
|
|
protected $recipients; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $binaryData |
75
|
|
|
* @param \ArrayIterator $recipients |
76
|
|
|
*/ |
77
|
|
|
public function __construct($binaryData, \ArrayIterator $recipients) |
78
|
|
|
{ |
79
|
|
|
$this->rawResponse = $binaryData; |
80
|
|
|
$this->recipients = $recipients; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Unpacks and process response data |
85
|
|
|
* |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function processResponse() |
89
|
|
|
{ |
90
|
|
|
$rawResponse = $this->rawResponse; |
91
|
|
|
|
92
|
|
|
if (empty($rawResponse)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$responseData = unpack("Ccommand/Cstatus/Nidentifier", $rawResponse); |
97
|
|
|
|
98
|
|
|
$this->validateResponse($responseData); |
99
|
|
|
|
100
|
|
|
$statusCode = $responseData['status']; |
101
|
|
|
$errorDescription = self::$responseDescription[$statusCode]; |
102
|
|
|
|
103
|
|
|
if ( |
104
|
|
|
$statusCode == self::ERROR_INVALID_TOKEN_SIZE |
105
|
|
|
|| $statusCode == self::ERROR_INVALID_TOKEN |
106
|
|
|
) { |
107
|
|
|
$recipients = $this->recipients; |
108
|
|
|
$recipients->current()->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED); |
109
|
|
|
|
110
|
|
|
throw new InvalidRecipientException($errorDescription, $recipients); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new DispatchMessageException($errorDescription, $statusCode); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Validates response data |
118
|
|
|
* |
119
|
|
|
* @param array $responseData |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function validateResponse($responseData) |
123
|
|
|
{ |
124
|
|
|
if ($responseData === false) { |
125
|
|
|
throw new RuntimeException('Unable to unpack response data'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (self::ERROR_RESPONSE_COMMAND != $responseData['command']) { |
129
|
|
|
throw new RuntimeException("Invalid APNS response packet command"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|