File indexing completed on 2025-05-04 05:32:44

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mobile
0017  * @subpackage Zend_Mobile_Push
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Gcm Response
0025  *
0026  * @category   Zend
0027  * @package    Zend_Mobile
0028  * @subpackage Zend_Mobile_Push
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  * @version    $Id$
0032  */
0033 class Zend_Mobile_Push_Response_Gcm
0034 {
0035 
0036     const RESULT_MESSAGE_ID = 'message_id';
0037     const RESULT_ERROR = 'error';
0038     const RESULT_CANONICAL = 'registration_id';
0039 
0040     /**
0041      * Multicast ID
0042      * @var int
0043      */
0044     protected $_id;
0045 
0046     /**
0047      * Success Count
0048      * @var int
0049      */
0050     protected $_successCnt;
0051 
0052     /**
0053      * Failure Count
0054      * @var int
0055      */
0056     protected $_failureCnt;
0057 
0058     /**
0059      * Canonical registration id count
0060      * @var int
0061      */
0062     protected $_canonicalCnt;
0063 
0064     /**
0065      * Message
0066      * @var Zend_Mobile_Push_Message_Gcm
0067      */
0068     protected $_message;
0069 
0070     /**
0071      * Results
0072      * @var array
0073      */
0074     protected $_results;
0075 
0076     /**
0077      * Raw Response
0078      * @var array
0079      */
0080     protected $_response;
0081 
0082     /**
0083      * Constructor
0084      *
0085      * @param string $responseString JSON encoded response
0086      * @param Zend_Mobile_Push_Message_Gcm $message
0087      * @return Zend_Mobile_Push_Response_Gcm
0088      * @throws Zend_Mobile_Push_Exception_ServerUnavailable
0089      */
0090     public function __construct($responseString = null, Zend_Mobile_Push_Message_Gcm $message = null)
0091     {
0092         if ($responseString) {
0093             if (!$response = json_decode($responseString, true)) {
0094                 // require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
0095                 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server gave us an invalid response, try again later');
0096             }
0097             $this->setResponse($response);
0098         }
0099 
0100         if ($message) {
0101             $this->setMessage($message);
0102         }
0103 
0104     }
0105 
0106     /**
0107      * Get Message
0108      *
0109      * @return Zend_Mobile_Push_Message_Gcm
0110      */
0111     public function getMessage()
0112     {
0113         return $this->_message;
0114     }
0115 
0116     /**
0117      * Set Message
0118      *
0119      * @param Zend_Mobile_Push_Message_Gcm $message
0120      * @return Zend_Mobile_Push_Response_Gcm
0121      */
0122     public function setMessage(Zend_Mobile_Push_Message_Gcm $message)
0123     {
0124         $this->_message = $message;
0125         return $this;
0126     }
0127 
0128     /**
0129      * Get Response
0130      *
0131      * @return array
0132      */
0133     public function getResponse()
0134     {
0135         return $this->_response;
0136     }
0137 
0138     /**
0139      * Set Response
0140      *
0141      * @param  array $response
0142      * @throws Zend_Mobile_Push_Exception
0143      * @return Zend_Mobile_Push_Response_Gcm
0144      */
0145     public function setResponse(array $response)
0146     {
0147         if (!isset($response['results']) ||
0148             !isset($response['success']) ||
0149             !isset($response['failure']) ||
0150             !isset($response['canonical_ids']) ||
0151             !isset($response['multicast_id'])) {
0152             throw new Zend_Mobile_Push_Exception('Response did not contain the proper fields');
0153         }
0154         $this->_response = $response;
0155         $this->_results = $response['results'];
0156         $this->_successCnt = (int) $response['success'];
0157         $this->_failureCnt = (int) $response['failure'];
0158         $this->_canonicalCnt = (int) $response['canonical_ids'];
0159         $this->_id = (int) $response['multicast_id'];
0160         return $this;
0161     }
0162 
0163     /**
0164      * Get Success Count
0165      *
0166      * @return int
0167      */
0168     public function getSuccessCount()
0169     {
0170         return $this->_successCnt;
0171     }
0172 
0173     /**
0174      * Get Failure Count
0175      *
0176      * @return int
0177      */
0178     public function getFailureCount()
0179     {
0180         return $this->_failureCnt;
0181     }
0182 
0183     /**
0184      * Get Canonical Count
0185      *
0186      * @return int
0187      */
0188     public function getCanonicalCount()
0189     {
0190         return $this->_canonicalCnt;
0191     }
0192 
0193     /**
0194      * Get Results
0195      *
0196      * @return array multi dimensional array of:
0197      *         NOTE: key is registration_id if the message is passed.
0198      *         'registration_id' => array( 
0199      *             'message_id' => 'id',
0200      *             'error' => 'error',
0201      *             'registration_id' => 'id'
0202      *          )
0203      */
0204     public function getResults()
0205     {
0206         return $this->_correlate();
0207     }
0208 
0209     /**
0210      * Get Singular Result
0211      *
0212      * @param  int   $flag one of the RESULT_* flags
0213      * @return array singular array with keys being registration id
0214      *               value is the type of result
0215      */
0216     public function getResult($flag)
0217     {
0218         $ret = array();
0219         foreach ($this->_correlate() as $k => $v) {
0220             if (isset($v[$flag])) {
0221                 $ret[$k] = $v[$flag];
0222             }
0223         }
0224         return $ret;
0225     }
0226 
0227     /**
0228      * Correlate Message and Result
0229      *
0230      * @return array
0231      */
0232     protected function _correlate()
0233     {
0234         $results = $this->_results;
0235         if ($this->_message && $results) {
0236             $tokens = $this->_message->getToken();
0237             while($token = array_shift($tokens)) {
0238                 $results[$token] = array_shift($results);
0239             }
0240         }
0241         return $results;
0242     }
0243 }