Skip to content

Commit ab6cffe

Browse files
author
Benjamin Wilson Friedman
authored
Merge pull request #283 from montymxb/push-status
Added ParsePushStatus class and updated tests.
2 parents 4ce2feb + 5c3761f commit ab6cffe

File tree

8 files changed

+375
-4
lines changed

8 files changed

+375
-4
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,33 @@ ParsePush::send(array(
231231
"data" => $data
232232
), true);
233233

234-
// Get Push Status Id
235-
$reponse = ParsePush::send($data, true);
236-
if(isset($response['_headers']['X-Push-Status-Id'])) {
237-
// Retrieve info on _PushStatus using the id
234+
// Get Push Status
235+
$response = ParsePush::send($data, true);
236+
if(ParsePush::hasStatus($response)) {
237+
238+
// Retrieve PushStatus object
239+
$pushStatus = ParsePush::getStatus($response);
240+
241+
// get push status string
242+
$status = $pushStatus->getPushStatus();
243+
244+
if($status == "succeeded") {
245+
// handle a successful push request
246+
247+
} else if($status == "running") {
248+
// handle a running push request
249+
250+
} else if($status == "failed") {
251+
// push request did not succeed
252+
253+
}
254+
255+
// get # pushes sent
256+
$sent = $pushStatus->getPushesSent();
257+
258+
// get # pushes failed
259+
$failed = $pushStatus->getPushesFailed();
260+
238261
}
239262
```
240263

src/Parse/ParseClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public static function initialize($app_id, $rest_key, $master_key, $enableCurlEx
122122
ParseInstallation::registerSubclass();
123123
}
124124

125+
if(!ParseObject::hasRegisteredSubclass('_PushStatus')) {
126+
ParsePushStatus::registerSubclass();
127+
}
128+
125129
ParseSession::registerSubclass();
126130
self::$applicationId = $app_id;
127131
self::$restKey = $rest_key;

src/Parse/ParsePush.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,50 @@ public static function send($data, $useMasterKey = false)
7777
true
7878
);
7979
}
80+
81+
/**
82+
* Returns whether or not the given response has a push status
83+
* Checks to see if X-Push-Status-Id is present in $response
84+
*
85+
* @param array $response Response from ParsePush::send
86+
* @return bool
87+
*/
88+
public static function hasStatus($response)
89+
{
90+
return(
91+
isset($response['_headers']) &&
92+
isset($response['_headers']['X-Parse-Push-Status-Id'])
93+
);
94+
95+
}
96+
97+
/**
98+
* Returns the PushStatus for a response from ParsePush::send
99+
*
100+
* @param array $response Response from ParsePush::send
101+
* @return null|ParsePushStatus
102+
*/
103+
public static function getStatus($response)
104+
{
105+
if(!isset($response['_headers'])) {
106+
// missing headers
107+
return null;
108+
109+
}
110+
111+
$headers = $response['_headers'];
112+
113+
if(!isset($headers['X-Parse-Push-Status-Id'])) {
114+
// missing push status id
115+
return null;
116+
117+
}
118+
119+
// get our push status id
120+
$pushStatusId = $response['_headers']['X-Parse-Push-Status-Id'];
121+
122+
// return our push status if it exists
123+
return ParsePushStatus::getFromId($pushStatusId);
124+
125+
}
80126
}

src/Parse/ParsePushStatus.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace Parse;
4+
5+
/**
6+
* ParsePushStatus - Representation of PushStatus for push notifications
7+
*
8+
* @author Ben Friedman <[email protected]>
9+
*/
10+
class ParsePushStatus extends ParseObject
11+
{
12+
public static $parseClassName = '_PushStatus';
13+
14+
/**
15+
* Returns a push status object or null from an id
16+
*
17+
* @param string $id Id to get this push status by
18+
* @return ParsePushStatus|null
19+
*/
20+
public static function getFromId($id)
21+
{
22+
try {
23+
// return the associated PushStatus object
24+
$query = new ParseQuery(self::$parseClassName);
25+
return $query->get($id, true);
26+
27+
} catch (ParseException $pe) {
28+
// no push found
29+
return null;
30+
31+
}
32+
33+
}
34+
35+
/**
36+
* Gets the time this push was sent at
37+
*
38+
* @return \DateTime
39+
*/
40+
public function getPushTime()
41+
{
42+
return new \DateTime($this->get("pushTime"));
43+
44+
}
45+
46+
/**
47+
* Gets the query used to send this push
48+
*
49+
* @return ParseQuery
50+
*/
51+
public function getPushQuery()
52+
{
53+
$query = $this->get("query");
54+
55+
// get the conditions
56+
$queryConditions = json_decode($query, true);
57+
58+
// setup a query
59+
$query = new ParseQuery(self::$parseClassName);
60+
61+
// set the conditions
62+
$query->_setConditions($queryConditions);
63+
64+
return $query;
65+
66+
}
67+
68+
/**
69+
* Gets the payload
70+
*
71+
* @return array
72+
*/
73+
public function getPushPayload()
74+
{
75+
return json_decode($this->get("payload"), true);
76+
77+
}
78+
79+
/**
80+
* Gets the source of this push
81+
*
82+
* @return string
83+
*/
84+
public function getPushSource()
85+
{
86+
return $this->get("source");
87+
88+
}
89+
90+
/**
91+
* Gets the status of this push
92+
*
93+
* @return string
94+
*/
95+
public function getPushStatus()
96+
{
97+
return $this->get("status");
98+
99+
}
100+
101+
/**
102+
* Gets the number of pushes sent
103+
*
104+
* @return int
105+
*/
106+
public function getPushesSent()
107+
{
108+
return $this->get("numSent");
109+
110+
}
111+
112+
/**
113+
* Gets the hash for this push
114+
*
115+
* @return string
116+
*/
117+
public function getPushHash()
118+
{
119+
return $this->get("pushHash");
120+
121+
}
122+
123+
/**
124+
* Gets the number of pushes failed
125+
*
126+
* @return int
127+
*/
128+
public function getPushesFailed()
129+
{
130+
return $this->get("numFailed");
131+
132+
}
133+
}

src/Parse/ParseQuery.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ private function addCondition($key, $condition, $value)
134134
$this->where[$key][$condition] = ParseClient::_encode($value, true);
135135
}
136136

137+
/**
138+
* Sets the conditions of this parse query from an array
139+
*
140+
* @param array $conditions Array of Conditions to set
141+
* @throws ParseException
142+
*/
143+
public function _setConditions($conditions)
144+
{
145+
if(!is_array($conditions)) {
146+
throw new ParseException("Conditions must be in an array");
147+
148+
}
149+
150+
// iterate over and add each condition
151+
foreach($conditions as $key => $entry) {
152+
foreach($entry as $condition => $value) {
153+
$this->addCondition($key, $condition, $value);
154+
155+
}
156+
157+
}
158+
159+
}
160+
137161
/**
138162
* Add a constraint to the query that requires a particular key's value to
139163
* be not equal to the provided value.

tests/Parse/ParseObjectTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Parse\ParseACL;
77
use Parse\ParseInstallation;
88
use Parse\ParseObject;
9+
use Parse\ParsePushStatus;
910
use Parse\ParseQuery;
1011
use Parse\ParseRole;
1112
use Parse\ParseSession;
@@ -997,6 +998,7 @@ public function testNoRegisteredSubclasses()
997998
ParseRole::_unregisterSubclass();
998999
ParseInstallation::_unregisterSubclass();
9991000
ParseSession::_unregisterSubclass();
1001+
ParsePushStatus::_unregisterSubclass();
10001002

10011003
new ParseObject('TestClass');
10021004

0 commit comments

Comments
 (0)