-
Notifications
You must be signed in to change notification settings - Fork 21
Handles more errors, expands Response class #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 32 commits
d98b7f3
8a5f0f5
f0b18bf
da554d7
60abcd7
8f74f3e
530f345
bf1d7e5
266823c
4e5bc71
bc6b5a5
705fc9b
b758c7c
042ebb0
cc44eb9
4b27d4e
712ea7c
235a667
8c4f381
48d6ab3
7e85a44
ca976f3
93a27a2
7e68f27
e428154
1761917
3bbc5ed
2868047
418efd3
ec55ec9
ea3a222
1635a89
2e763b3
90c63ab
af89559
79cf163
a5cdd5e
5470a0c
c8fb000
3190ea4
268de47
68eea46
bcb07c9
8fecb99
4b33453
d05d88f
ddfa5cf
019602e
b978100
03db6b7
13a0bc2
c0e9cc9
033a9cc
45eb6ed
1d942c8
9e9dc81
2fa1df9
4c2b90a
daa3bd4
c1dce41
6caf321
bc3723c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| <?php | ||
| namespace CodeMonkeysRu\GCM; | ||
|
|
||
| /** | ||
| * @author Vladimir Savenkov <[email protected]> | ||
| */ | ||
| class Response | ||
| { | ||
|
|
||
|
|
@@ -34,6 +31,42 @@ class Response | |
| * @var integer | ||
| */ | ||
| private $canonicalIds = null; | ||
|
|
||
| /** | ||
| * Response headers. | ||
| * | ||
| * @var string[] | ||
| */ | ||
| private $responseHeaders = []; | ||
|
|
||
| /** | ||
| * Did Google demand that we try again. | ||
| * | ||
| * @var boolean | ||
| */ | ||
| private $mustRetry = false; | ||
|
|
||
| /** | ||
| * Number of seconds to wait. | ||
| * | ||
| * @var integer | ||
| */ | ||
| private $waitSeconds = null; | ||
|
|
||
| /** | ||
| * Did you use a reserved data key? | ||
| * | ||
| * @var boolean | ||
| */ | ||
| private $existsInvalidDataKey = false; | ||
|
|
||
| /** | ||
| * Did one of your clients register with the wrong senderId? | ||
| * If one of them did, then presumably they all did. | ||
| * | ||
| * @var boolean | ||
| */ | ||
| private $existsMismatchSenderId = false; | ||
|
|
||
| /** | ||
| * Array of objects representing the status of the messages processed. | ||
|
|
@@ -53,8 +86,20 @@ class Response | |
| */ | ||
| private $results = array(); | ||
|
|
||
| public function __construct(Message $message, $responseBody) | ||
| public function __construct(Message $message, $responseBody, $responseHeaders) | ||
| { | ||
| $this->responseHeaders = $responseHeaders; | ||
|
|
||
|
||
| $this->mustRetry = false; | ||
|
|
||
| foreach($responseHeaders as $header) { | ||
| if (strpos($header, 'Retry-After') !== false) { | ||
| $this->mustRetry = true; | ||
| $this->waitSeconds = (int) explode(" ", $header)[1]; | ||
| break; | ||
| } | ||
| } | ||
|
||
|
|
||
| $data = \json_decode($responseBody, true); | ||
| if ($data === null) { | ||
| throw new Exception("Malformed reponse body. ".$responseBody, Exception::MALFORMED_RESPONSE); | ||
|
|
@@ -63,26 +108,72 @@ public function __construct(Message $message, $responseBody) | |
| $this->failure = $data['failure']; | ||
| $this->success = $data['success']; | ||
| $this->canonicalIds = $data['canonical_ids']; | ||
| $this->existsInvalidDataKey = false; | ||
| $this->existsMismatchSenderId = false; | ||
| $this->results = array(); | ||
|
|
||
| foreach ($message->getRegistrationIds() as $key => $registrationId) { | ||
| $this->results[$registrationId] = $data['results'][$key]; | ||
| $result = $data['results'][$key]; | ||
| if (isset($result['error'])) { | ||
| switch ($result['error']) { | ||
| case "InvalidDataKey": | ||
| $this->existsInvalidDataKey = true; | ||
| break; | ||
| case "MismatchSenderId": | ||
| $this->existsMismatchSenderId = true; | ||
| break; | ||
| default: | ||
|
||
| break; | ||
| } | ||
| } | ||
| $this->results[$registrationId] = $result; | ||
| } | ||
| $result = null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do we need this line for?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use that variable in the |
||
| } | ||
|
|
||
|
|
||
| public function getResponseHeaders() { | ||
|
||
| return $this->responseHeaders; | ||
| } | ||
|
|
||
| public function getMulticastId() | ||
| { | ||
| return $this->multicastId; | ||
| } | ||
|
|
||
| public function getMustRetry() | ||
| { | ||
| return $this->mustRetry; | ||
| } | ||
|
|
||
| public function getWaitSeconds() | ||
| { | ||
| return $this->waitSeconds; | ||
| } | ||
|
|
||
| public function getSuccessCount() | ||
| { | ||
| return $this->success; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Both implementation errors and server errors are included here. | ||
| * | ||
| * @return integer | ||
| */ | ||
| public function getFailureCount() | ||
| { | ||
| return $this->failure; | ||
| } | ||
|
|
||
| public function getExistsInvalidDataKey() | ||
| { | ||
| return $this->existsInvalidDataKey; | ||
| } | ||
|
|
||
| public function getExistsMismatchSenderId() | ||
| { | ||
| return $this->existsMismatchSenderId; | ||
| } | ||
|
|
||
| public function getNewRegistrationIdsCount() | ||
| { | ||
|
|
@@ -144,6 +235,7 @@ function($result) { | |
| return array_keys($filteredResults); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Returns an array of registration ids for which you must resend a message (?), | ||
| * cause devices aren't available now. | ||
|
|
@@ -162,11 +254,38 @@ function($result) { | |
| return ( | ||
| isset($result['error']) | ||
| && | ||
| ( | ||
| ($result['error'] == "Unavailable") | ||
| || | ||
| ($result['error'] == "InternalServerError") | ||
| || | ||
| ($result['error'] == "DeviceMessageRateExceeded") | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| return array_keys($filteredResults); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an array of registration ids who registered | ||
| * for pushes using the wrong senderId. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getMismatchSenderIdIds() { | ||
| if ($this->getFailureCount() == 0) { | ||
| return array(); | ||
| } | ||
| $filteredResults = array_filter($this->results, | ||
| function($result) { | ||
| return ( | ||
| isset($result['error']) | ||
| && | ||
| ($result['error'] == "MismatchSenderId") | ||
| ); | ||
| }); | ||
|
|
||
| } | ||
| return array_keys($filteredResults); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should not expose our transport layer.
There should be no mentions of CURL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, folded into UNKNOWN_ERROR exception.