Gcm.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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-2015 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-2015 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. * @throws Zend_Mobile_Push_Exception
  127. * @return Zend_Mobile_Push_Response_Gcm
  128. */
  129. public function setResponse(array $response)
  130. {
  131. if (!isset($response['results']) ||
  132. !isset($response['success']) ||
  133. !isset($response['failure']) ||
  134. !isset($response['canonical_ids']) ||
  135. !isset($response['multicast_id'])) {
  136. throw new Zend_Mobile_Push_Exception('Response did not contain the proper fields');
  137. }
  138. $this->_response = $response;
  139. $this->_results = $response['results'];
  140. $this->_successCnt = (int) $response['success'];
  141. $this->_failureCnt = (int) $response['failure'];
  142. $this->_canonicalCnt = (int) $response['canonical_ids'];
  143. $this->_id = (int) $response['multicast_id'];
  144. return $this;
  145. }
  146. /**
  147. * Get Success Count
  148. *
  149. * @return int
  150. */
  151. public function getSuccessCount()
  152. {
  153. return $this->_successCnt;
  154. }
  155. /**
  156. * Get Failure Count
  157. *
  158. * @return int
  159. */
  160. public function getFailureCount()
  161. {
  162. return $this->_failureCnt;
  163. }
  164. /**
  165. * Get Canonical Count
  166. *
  167. * @return int
  168. */
  169. public function getCanonicalCount()
  170. {
  171. return $this->_canonicalCnt;
  172. }
  173. /**
  174. * Get Results
  175. *
  176. * @return array multi dimensional array of:
  177. * NOTE: key is registration_id if the message is passed.
  178. * 'registration_id' => array(
  179. * 'message_id' => 'id',
  180. * 'error' => 'error',
  181. * 'registration_id' => 'id'
  182. * )
  183. */
  184. public function getResults()
  185. {
  186. return $this->_correlate();
  187. }
  188. /**
  189. * Get Singular Result
  190. *
  191. * @param int $flag one of the RESULT_* flags
  192. * @return array singular array with keys being registration id
  193. * value is the type of result
  194. */
  195. public function getResult($flag)
  196. {
  197. $ret = array();
  198. foreach ($this->_correlate() as $k => $v) {
  199. if (isset($v[$flag])) {
  200. $ret[$k] = $v[$flag];
  201. }
  202. }
  203. return $ret;
  204. }
  205. /**
  206. * Correlate Message and Result
  207. *
  208. * @return array
  209. */
  210. protected function _correlate()
  211. {
  212. $results = $this->_results;
  213. if ($this->_message && $results) {
  214. $tokens = $this->_message->getToken();
  215. while($token = array_shift($tokens)) {
  216. $results[$token] = array_shift($results);
  217. }
  218. }
  219. return $results;
  220. }
  221. }