web/lib/Zend/Mobile/Push/Response/Gcm.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     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 /**
       
    24  * Gcm Response
       
    25  *
       
    26  * @category   Zend
       
    27  * @package    Zend_Mobile
       
    28  * @subpackage Zend_Mobile_Push
       
    29  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  * @version    $Id$
       
    32  */
       
    33 class Zend_Mobile_Push_Response_Gcm
       
    34 {
       
    35 
       
    36     const RESULT_MESSAGE_ID = 'message_id';
       
    37     const RESULT_ERROR = 'error';
       
    38     const RESULT_CANONICAL = 'registration_id';
       
    39 
       
    40     /**
       
    41      * Multicast ID
       
    42      * @var int
       
    43      */
       
    44     protected $_id;
       
    45 
       
    46     /**
       
    47      * Success Count
       
    48      * @var int
       
    49      */
       
    50     protected $_successCnt;
       
    51 
       
    52     /**
       
    53      * Failure Count
       
    54      * @var int
       
    55      */
       
    56     protected $_failureCnt;
       
    57 
       
    58     /**
       
    59      * Canonical registration id count
       
    60      * @var int
       
    61      */
       
    62     protected $_canonicalCnt;
       
    63 
       
    64     /**
       
    65      * Message
       
    66      * @var Zend_Mobile_Push_Message_Gcm
       
    67      */
       
    68     protected $_message;
       
    69 
       
    70     /**
       
    71      * Results
       
    72      * @var array
       
    73      */
       
    74     protected $_results;
       
    75 
       
    76     /**
       
    77      * Raw Response
       
    78      * @var array
       
    79      */
       
    80     protected $_response;
       
    81 
       
    82     /**
       
    83      * Constructor
       
    84      *
       
    85      * @param string $responseString JSON encoded response
       
    86      * @param Zend_Mobile_Push_Message_Gcm $message
       
    87      * @return Zend_Mobile_Push_Response_Gcm
       
    88      * @throws Zend_Mobile_Push_Exception_ServerUnavailable
       
    89      */
       
    90     public function __construct($responseString = null, Zend_Mobile_Push_Message_Gcm $message = null)
       
    91     {
       
    92         if ($responseString) {
       
    93             if (!$response = json_decode($responseString, true)) {
       
    94                 require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
       
    95                 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server gave us an invalid response, try again later');
       
    96             }
       
    97             $this->setResponse($response);
       
    98         }
       
    99 
       
   100         if ($message) {
       
   101             $this->setMessage($message);
       
   102         }
       
   103 
       
   104     }
       
   105 
       
   106     /**
       
   107      * Get Message
       
   108      *
       
   109      * @return Zend_Mobile_Push_Message_Gcm
       
   110      */
       
   111     public function getMessage()
       
   112     {
       
   113         return $this->_message;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Set Message
       
   118      *
       
   119      * @param Zend_Mobile_Push_Message_Gcm $message
       
   120      * @return Zend_Mobile_Push_Response_Gcm
       
   121      */
       
   122     public function setMessage(Zend_Mobile_Push_Message_Gcm $message)
       
   123     {
       
   124         $this->_message = $message;
       
   125         return $this;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Get Response
       
   130      *
       
   131      * @return array
       
   132      */
       
   133     public function getResponse()
       
   134     {
       
   135         return $this->_response;
       
   136     }
       
   137 
       
   138     /**
       
   139      * Set Response
       
   140      *
       
   141      * @param array $response
       
   142      * @return Zend_Mobile_Push_Response_Gcm
       
   143      */
       
   144     public function setResponse(array $response)
       
   145     {
       
   146         if (!isset($response['results']) ||
       
   147             !isset($response['success']) ||
       
   148             !isset($response['failure']) ||
       
   149             !isset($response['canonical_ids']) ||
       
   150             !isset($response['multicast_id'])) {
       
   151             throw new Zend_Mobile_Push_Exception('Response did not contain the proper fields');
       
   152         }
       
   153         $this->_response = $response;
       
   154         $this->_results = $response['results'];
       
   155         $this->_successCnt = (int) $response['success'];
       
   156         $this->_failureCnt = (int) $response['failure'];
       
   157         $this->_canonicalCnt = (int) $response['canonical_ids'];
       
   158         $this->_id = (int) $response['multicast_id'];
       
   159         return $this;
       
   160     }
       
   161 
       
   162     /**
       
   163      * Get Success Count
       
   164      *
       
   165      * @return int
       
   166      */
       
   167     public function getSuccessCount()
       
   168     {
       
   169         return $this->_successCnt;
       
   170     }
       
   171 
       
   172     /**
       
   173      * Get Failure Count
       
   174      *
       
   175      * @return int
       
   176      */
       
   177     public function getFailureCount()
       
   178     {
       
   179         return $this->_failureCnt;
       
   180     }
       
   181 
       
   182     /**
       
   183      * Get Canonical Count
       
   184      *
       
   185      * @return int
       
   186      */
       
   187     public function getCanonicalCount()
       
   188     {
       
   189         return $this->_canonicalCnt;
       
   190     }
       
   191 
       
   192     /**
       
   193      * Get Results
       
   194      *
       
   195      * @return array multi dimensional array of:
       
   196      *         NOTE: key is registration_id if the message is passed.
       
   197      *         'registration_id' => array( 
       
   198      *             'message_id' => 'id',
       
   199      *             'error' => 'error',
       
   200      *             'registration_id' => 'id'
       
   201      *          )
       
   202      */
       
   203     public function getResults()
       
   204     {
       
   205         return $this->_correlate();
       
   206     }
       
   207 
       
   208     /**
       
   209      * Get Singular Result
       
   210      *
       
   211      * @param  int   $flag one of the RESULT_* flags
       
   212      * @return array singular array with keys being registration id
       
   213      *               value is the type of result
       
   214      */
       
   215     public function getResult($flag)
       
   216     {
       
   217         $ret = array();
       
   218         foreach ($this->_correlate() as $k => $v) {
       
   219             if (isset($v[$flag])) {
       
   220                 $ret[$k] = $v[$flag];
       
   221             }
       
   222         }
       
   223         return $ret;
       
   224     }
       
   225 
       
   226     /**
       
   227      * Correlate Message and Result
       
   228      *
       
   229      * @return array
       
   230      */
       
   231     protected function _correlate()
       
   232     {
       
   233         $results = $this->_results;
       
   234         if ($this->_message && $results) {
       
   235             $tokens = $this->_message->getToken();
       
   236             while($token = array_shift($tokens)) {
       
   237                 $results[$token] = array_shift($results);
       
   238             }
       
   239         }
       
   240         return $results;
       
   241     }
       
   242 }