Gcm.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mobile
  17. * @subpackage Zend_Mobile_Push
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Gcm Response
  24. *
  25. * @category Zend
  26. * @package Zend_Mobile
  27. * @subpackage Zend_Mobile_Push
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Mobile_Push_Response_Gcm
  33. {
  34. const RESULT_MESSAGE_ID = 'message_id';
  35. const RESULT_ERROR = 'error';
  36. const RESULT_CANONICAL = 'registration_id';
  37. /**
  38. * Multicast ID
  39. * @var int
  40. */
  41. protected $_id;
  42. /**
  43. * Success Count
  44. * @var int
  45. */
  46. protected $_successCnt;
  47. /**
  48. * Failure Count
  49. * @var int
  50. */
  51. protected $_failureCnt;
  52. /**
  53. * Canonical registration id count
  54. * @var int
  55. */
  56. protected $_canonicalCnt;
  57. /**
  58. * Message
  59. * @var Zend_Mobile_Push_Message_Gcm
  60. */
  61. protected $_message;
  62. /**
  63. * Results
  64. * @var array
  65. */
  66. protected $_results;
  67. /**
  68. * Raw Response
  69. * @var array
  70. */
  71. protected $_response;
  72. /**
  73. * Constructor
  74. *
  75. * @param string $responseString JSON encoded response
  76. * @param Zend_Mobile_Push_Message_Gcm $message
  77. * @return Zend_Mobile_Push_Response_Gcm
  78. * @throws Zend_Mobile_Push_Exception_ServerUnavailable
  79. */
  80. public function __construct($responseString = null, Zend_Mobile_Push_Message_Gcm $message = null)
  81. {
  82. if ($responseString) {
  83. if (!$response = json_decode($responseString, true)) {
  84. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  85. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server gave us an invalid response, try again later');
  86. }
  87. $this->setResponse($response);
  88. }
  89. if ($message) {
  90. $this->setMessage($message);
  91. }
  92. }
  93. /**
  94. * Get Message
  95. *
  96. * @return Zend_Mobile_Push_Message_Gcm
  97. */
  98. public function getMessage()
  99. {
  100. return $this->_message;
  101. }
  102. /**
  103. * Set Message
  104. *
  105. * @param Zend_Mobile_Push_Message_Gcm $message
  106. * @return Zend_Mobile_Push_Response_Gcm
  107. */
  108. public function setMessage(Zend_Mobile_Push_Message_Gcm $message)
  109. {
  110. $this->_message = $message;
  111. return $this;
  112. }
  113. /**
  114. * Get Response
  115. *
  116. * @return array
  117. */
  118. public function getResponse()
  119. {
  120. return $this->_response;
  121. }
  122. /**
  123. * Set Response
  124. *
  125. * @param array $response
  126. * @return Zend_Mobile_Push_Response_Gcm
  127. */
  128. public function setResponse(array $response)
  129. {
  130. if (!isset($response['results']) ||
  131. !isset($response['success']) ||
  132. !isset($response['failure']) ||
  133. !isset($response['canonical_ids']) ||
  134. !isset($response['multicast_id'])) {
  135. throw new Zend_Mobile_Push_Exception('Response did not contain the proper fields');
  136. }
  137. $this->_response = $response;
  138. $this->_results = $response['results'];
  139. $this->_successCnt = (int) $response['success'];
  140. $this->_failureCnt = (int) $response['failure'];
  141. $this->_canonicalCnt = (int) $response['canonical_ids'];
  142. $this->_id = (int) $response['multicast_id'];
  143. return $this;
  144. }
  145. /**
  146. * Get Success Count
  147. *
  148. * @return int
  149. */
  150. public function getSuccessCount()
  151. {
  152. return $this->_successCnt;
  153. }
  154. /**
  155. * Get Failure Count
  156. *
  157. * @return int
  158. */
  159. public function getFailureCount()
  160. {
  161. return $this->_failureCnt;
  162. }
  163. /**
  164. * Get Canonical Count
  165. *
  166. * @return int
  167. */
  168. public function getCanonicalCount()
  169. {
  170. return $this->_canonicalCnt;
  171. }
  172. /**
  173. * Get Results
  174. *
  175. * @return array multi dimensional array of:
  176. * NOTE: key is registration_id if the message is passed.
  177. * 'registration_id' => array(
  178. * 'message_id' => 'id',
  179. * 'error' => 'error',
  180. * 'registration_id' => 'id'
  181. * )
  182. */
  183. public function getResults()
  184. {
  185. return $this->_correlate();
  186. }
  187. /**
  188. * Get Singular Result
  189. *
  190. * @param int $flag one of the RESULT_* flags
  191. * @return array singular array with keys being registration id
  192. * value is the type of result
  193. */
  194. public function getResult($flag)
  195. {
  196. $ret = array();
  197. foreach ($this->_correlate() as $k => $v) {
  198. if (isset($v[$flag])) {
  199. $ret[$k] = $v[$flag];
  200. }
  201. }
  202. return $ret;
  203. }
  204. /**
  205. * Correlate Message and Result
  206. *
  207. * @return array
  208. */
  209. protected function _correlate()
  210. {
  211. $results = $this->_results;
  212. if ($this->_message && $results) {
  213. $tokens = $this->_message->getToken();
  214. while($token = array_shift($tokens)) {
  215. $results[$token] = array_shift($results);
  216. }
  217. }
  218. return $results;
  219. }
  220. }