update v1.0.7.9 R.C.

This is a Release Candidate. We are still testing.
This commit is contained in:
Sujit Prasad
2016-08-03 20:04:36 +05:30
parent 8b6b924d09
commit ffa56a43cb
3830 changed files with 181529 additions and 495353 deletions

View File

@@ -0,0 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @category ZendService
* @package ZendService_Google
*/
namespace ZendService\Google\Exception;
/**
* Invalid Argument Exception
*
* @category ZendService
* @package ZendService_Google
*/
class InvalidArgumentException extends \InvalidArgumentException
{
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @category ZendService
* @package ZendService_Google
*/
namespace ZendService\Google\Exception;
/**
* Runtime Exception
*
* @category ZendService
* @package ZendService_Google
*/
class RuntimeException extends \RuntimeException
{
}

View File

@@ -0,0 +1,141 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @category ZendService
* @package ZendService_Google\Gcm
*/
namespace ZendService\Google\Gcm;
use ZendService\Google\Exception;
use Zend\Http\Client as HttpClient;
use Zend\Json\Json;
/**
* Google Cloud Messaging Client
* This class allows the ability to send out messages
* through the Google Cloud Messaging API.
*
* @category ZendService
* @package ZendService_Google
* @subpackage Gcm
*/
class Client
{
/**
* @const string Server URI
*/
const SERVER_URI = 'https://gcm-http.googleapis.com/gcm/send';
/**
* @var Zend\Http\Client
*/
protected $httpClient;
/**
* @var string
*/
protected $apiKey;
/**
* Get API Key
*
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* Set API Key
*
* @param string $apiKey
* @return Client
* @throws InvalidArgumentException
*/
public function setApiKey($apiKey)
{
if (!is_string($apiKey) || empty($apiKey)) {
throw new Exception\InvalidArgumentException('The api key must be a string and not empty');
}
$this->apiKey = $apiKey;
return $this;
}
/**
* Get HTTP Client
*
* @return Zend\Http\Client
*/
public function getHttpClient()
{
if (!$this->httpClient) {
$this->httpClient = new HttpClient();
$this->httpClient->setOptions(array('strictredirects' => true));
}
return $this->httpClient;
}
/**
* Set HTTP Client
*
* @param Zend\Http\Client
* @return Client
*/
public function setHttpClient(HttpClient $http)
{
$this->httpClient = $http;
return $this;
}
/**
* Send Message
*
* @param Mesage $message
* @return Response
* @throws Exception\RuntimeException
*/
public function send(Message $message)
{
$client = $this->getHttpClient();
$client->setUri(self::SERVER_URI);
$headers = $client->getRequest()->getHeaders();
$headers->addHeaderLine('Authorization', 'key=' . $this->getApiKey());
$response = $client->setHeaders($headers)
->setMethod('POST')
->setRawBody($message->toJson())
->setEncType('application/json')
->send();
switch ($response->getStatusCode()) {
case 500:
throw new Exception\RuntimeException('500 Internal Server Error');
break;
case 503:
$exceptionMessage = '503 Server Unavailable';
if ($retry = $response->getHeaders()->get('Retry-After')) {
$exceptionMessage .= '; Retry After: ' . $retry;
}
throw new Exception\RuntimeException($exceptionMessage);
break;
case 401:
throw new Exception\RuntimeException('401 Forbidden; Authentication Error');
break;
case 400:
throw new Exception\RuntimeException('400 Bad Request; invalid message');
break;
}
if (!$response = Json::decode($response->getBody(), Json::TYPE_ARRAY)) {
throw new Exception\RuntimeException('Response body did not contain a valid JSON response');
}
return new Response($response, $message);
}
}

View File

@@ -0,0 +1,325 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @category ZendService
* @package ZendService_Google\Gcm
*/
namespace ZendService\Google\Gcm;
use ZendService\Google\Exception;
use Zend\Json\Json;
/**
* Google Cloud Messaging Message
* This class defines a message to be sent
* through the Google Cloud Messaging API.
*
* @category ZendService
* @package ZendService_Google
* @subpackage Gcm
*/
class Message
{
/**
* @var array
*/
protected $registrationIds = array();
/**
* @var string
*/
protected $collapseKey;
/**
* @var array
*/
protected $data = array();
/**
* @var bool
*/
protected $delayWhileIdle = false;
/**
* @var int
*/
protected $timeToLive = 2419200;
/**
* @var string
*/
protected $restrictedPackageName;
/**
* @var bool
*/
protected $dryRun = false;
/**
* Set Registration Ids
*
* @param array $ids
* @return Message
*/
public function setRegistrationIds(array $ids)
{
$this->clearRegistrationIds();
foreach ($ids as $id) {
$this->addRegistrationId($id);
}
return $this;
}
/**
* Get Registration Ids
*
* @return array
*/
public function getRegistrationIds()
{
return $this->registrationIds;
}
/**
* Add Registration Ids
*
* @param string $id
* @return Message
* @throws Exception\InvalidArgumentException
*/
public function addRegistrationId($id)
{
if (!is_string($id) || empty($id)) {
throw new Exception\InvalidArgumentException('$id must be a non-empty string');
}
if (!in_array($id, $this->registrationIds)) {
$this->registrationIds[] = $id;
}
return $this;
}
/**
* Clear Registration Ids
*
* @return Message
*/
public function clearRegistrationIds()
{
$this->registrationIds = array();
return $this;
}
/**
* Get Collapse Key
*
* @return string
*/
public function getCollapseKey()
{
return $this->collapseKey;
}
/**
* Set Collapse Key
*
* @param string $key
* @return Message
* @throws Exception\InvalidArgumentException
*/
public function setCollapseKey($key)
{
if (!is_null($key) && !(is_string($key) && strlen($key) > 0)) {
throw new Exception\InvalidArgumentException('$key must be null or a non-empty string');
}
$this->collapseKey = $key;
return $this;
}
/**
* Set Data
*
* @param array $data
* @return Message
*/
public function setData(array $data)
{
$this->clearData();
foreach ($data as $k => $v) {
$this->addData($k, $v);
}
return $this;
}
/**
* Get Data
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Add Data
*
* @param string $key
* @param mixed $value
* @return Message
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function addData($key, $value)
{
if (!is_string($key) || empty($key)) {
throw new Exception\InvalidArgumentException('$key must be a non-empty string');
}
if (array_key_exists($key, $this->data)) {
throw new Exception\RuntimeException('$key conflicts with current set data');
}
$this->data[$key] = $value;
return $this;
}
/**
* Clear Data
*
* @return Message
*/
public function clearData()
{
$this->data = array();
return $this;
}
/**
* Set Delay While Idle
*
* @param bool $delay
* @return Message
*/
public function setDelayWhileIdle($delay)
{
$this->delayWhileIdle = (bool) $delay;
return $this;
}
/**
* Get Delay While Idle
*
* @return bool
*/
public function getDelayWhileIdle()
{
return $this->delayWhileIdle;
}
/**
* Set Time to Live
*
* @param int $ttl
* @return Message
*/
public function setTimeToLive($ttl)
{
$this->timeToLive = (int) $ttl;
return $this;
}
/**
* Get Time to Live
*
* @return int
*/
public function getTimeToLive()
{
return $this->timeToLive;
}
/**
* Set Restricted Package Name
*
* @param string $name
* @return Message
* @throws Exception\InvalidArgumentException
*/
public function setRestrictedPackageName($name)
{
if (!is_null($name) && !(is_string($name) && strlen($name) > 0)) {
throw new Exception\InvalidArgumentException('$name must be null OR a non-empty string');
}
$this->restrictedPackageName = $name;
return $this;
}
/**
* Get Restricted Package Name
*
* @return string
*/
public function getRestrictedPackageName()
{
return $this->restrictedPackageName;
}
/**
* Set Dry Run
*
* @param bool $dryRun
* @return Message
*/
public function setDryRun($dryRun)
{
$this->dryRun = (bool) $dryRun;
return $this;
}
/**
* Get Dry Run
*
* @return bool
*/
public function getDryRun()
{
return $this->dryRun;
}
/**
* To JSON
* Utility method to put the JSON into the
* GCM proper format for sending the message.
*
* @return string
*/
public function toJson()
{
$json = array();
if ($this->registrationIds) {
$json['registration_ids'] = $this->registrationIds;
}
if ($this->collapseKey) {
$json['collapse_key'] = $this->collapseKey;
}
if ($this->data) {
$json['data'] = $this->data;
}
if ($this->delayWhileIdle) {
$json['delay_while_idle'] = $this->delayWhileIdle;
}
if ($this->timeToLive != 2419200) {
$json['time_to_live'] = $this->timeToLive;
}
if ($this->restrictedPackageName) {
$json['restricted_package_name'] = $this->restrictedPackageName;
}
if ($this->dryRun) {
$json['dry_run'] = $this->dryRun;
}
return Json::encode($json);
}
}

View File

@@ -0,0 +1,234 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @category ZendService
* @package ZendService_Google
* @subpackage Gcm
*/
namespace ZendService\Google\Gcm;
use ZendService\Google\Exception;
/**
* Google Cloud Messaging Response
* This class parses out the response from
* the Google Cloud Messaging API
*
* @category ZendService
* @package ZendService_Google
* @subpackage Gcm
*/
class Response
{
/**
* @const Message ID field
*/
const RESULT_MESSAGE_ID = 'message_id';
/**
* @const Error field
*/
const RESULT_ERROR = 'error';
/**
* @const Canonical field
*/
const RESULT_CANONICAL = 'registration_id';
/**
* @var int
*/
protected $id;
/**
* @var int
*/
protected $cntSuccess;
/**
* @var int
*/
protected $cntFailure;
/**
* @var int
*/
protected $cntCanonical;
/**
* @var Message
*/
protected $message;
/**
* @var array
*/
protected $results;
/**
* @var array
*/
protected $response;
/**
* Constructor
*
* @param string $response
* @param Message $message
* @return Response
* @throws Exception\ServerUnavailable
*/
public function __construct($response = null, Message $message = null)
{
if ($response) {
$this->setResponse($response);
}
if ($message) {
$this->setMessage($message);
}
}
/**
* Get Message
*
* @return Message
*/
public function getMessage()
{
return $this->message;
}
/**
* Set Message
*
* @param Message $message
* @return Response
*/
public function setMessage(Message $message)
{
$this->message = $message;
return $this;
}
/**
* Get Response
*
* @return array
*/
public function getResponse()
{
return $this->response;
}
/**
* Set Response
*
* @param array $response
* @return Response
* @throws Exception\InvalidArgumentException
*/
public function setResponse(array $response)
{
if (!isset($response['results']) ||
!isset($response['success']) ||
!isset($response['failure']) ||
!isset($response['canonical_ids']) ||
!isset($response['multicast_id'])) {
throw new Exception\InvalidArgumentException('Response did not contain the proper fields');
}
$this->response = $response;
$this->results = $response['results'];
$this->cntSuccess = (int) $response['success'];
$this->cntFailure = (int) $response['failure'];
$this->cntCanonical = (int) $response['canonical_ids'];
$this->id = (int) $response['multicast_id'];
return $this;
}
/**
* Get Success Count
*
* @return int
*/
public function getSuccessCount()
{
return $this->cntSuccess;
}
/**
* Get Failure Count
*
* @return int
*/
public function getFailureCount()
{
return $this->cntFailure;
}
/**
* Get Canonical Count
*
* @return int
*/
public function getCanonicalCount()
{
return $this->cntCanonical;
}
/**
* Get Results
*
* @return array multi dimensional array of:
* NOTE: key is registration_id if the message is passed.
* 'registration_id' => array(
* 'message_id' => 'id',
* 'error' => 'error',
* 'registration_id' => 'id'
* )
*/
public function getResults()
{
return $this->correlate();
}
/**
* Get Singular Result
*
* @param int $flag one of the RESULT_* flags
* @return array singular array with keys being registration id
* value is the type of result
*/
public function getResult($flag)
{
$ret = array();
foreach ($this->correlate() as $k => $v) {
if (isset($v[$flag])) {
$ret[$k] = $v[$flag];
}
}
return $ret;
}
/**
* Correlate Message and Result
*
* @return array
*/
protected function correlate()
{
$results = $this->results;
if ($this->message && $results) {
$ids = $this->message->getRegistrationIds();
while ($id = array_shift($ids)) {
$results[$id] = array_shift($results);
}
}
return $results;
}
}