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,54 @@
<?php
/*
* This file is part of NotificationPusher.
*
* (c) 2013 Cédric Dugat <cedric@dugat.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\PushInterface;
/**
* AdapterInterface.
*
* @author Cédric Dugat <cedric@dugat.me>
*/
interface AdapterInterface
{
/**
* Push.
*
* @param \Sly\NotificationPusher\Model\PushInterface $push Push
*
* @return \Sly\NotificationPusher\Collection\DeviceCollection
*/
public function push(PushInterface $push);
/**
* Supports.
*
* @param string $token Token
*
* @return boolean
*/
public function supports($token);
/**
* Get default parameters.
*
* @return array
*/
public function getDefaultParameters();
/**
* Get required parameters.
*
* @return array
*/
public function getRequiredParameters();
}

View File

@@ -0,0 +1,196 @@
<?php
/*
* This file is part of NotificationPusher.
*
* (c) 2013 Cédric Dugat <cedric@dugat.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\MessageInterface;
use Sly\NotificationPusher\Model\DeviceInterface;
use Sly\NotificationPusher\Exception\AdapterException;
use Sly\NotificationPusher\Exception\PushException;
use Sly\NotificationPusher\Collection\DeviceCollection;
use ZendService\Apple\Apns\Client\AbstractClient as ServiceAbstractClient;
use ZendService\Apple\Apns\Client\Message as ServiceClient;
use ZendService\Apple\Apns\Message as ServiceMessage;
use ZendService\Apple\Apns\Message\Alert as ServiceAlert;
use ZendService\Apple\Apns\Response\Message as ServiceResponse;
use ZendService\Apple\Apns\Exception\RuntimeException as ServiceRuntimeException;
use ZendService\Apple\Apns\Client\Feedback as ServiceFeedbackClient;
/**
* APNS adapter.
*
* @uses \Sly\NotificationPusher\Adapter\BaseAdapter
* @uses \Sly\NotificationPusher\Adapter\AdapterInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class Apns extends BaseAdapter implements AdapterInterface
{
/**
* {@inheritdoc}
*
* @throws \Sly\NotificationPusher\Exception\AdapterException
*/
public function __construct(array $parameters = array())
{
parent::__construct($parameters);
$cert = $this->getParameter('certificate');
if (false === file_exists($cert)) {
throw new AdapterException(sprintf('Certificate %s does not exist', $cert));
}
}
/**
* {@inheritdoc}
*
* @throws \Sly\NotificationPusher\Exception\PushException
*/
public function push(PushInterface $push)
{
$client = $this->getOpenedClient(new ServiceClient());
$pushedDevices = new DeviceCollection();
foreach ($push->getDevices() as $device) {
$message = $this->getServiceMessageFromOrigin($device, $push->getMessage());
try {
$this->response = $client->send($message);
} catch (ServiceRuntimeException $e) {
throw new PushException($e->getMessage());
}
if (ServiceResponse::RESULT_OK === $this->response->getCode()) {
$pushedDevices->add($device);
}
}
$client->close();
return $pushedDevices;
}
/**
* Feedback.
*
* @return array
*/
public function getFeedback()
{
$client = $this->getOpenedClient(new ServiceFeedbackClient());
$responses = array();
$serviceResponses = $client->feedback();
$client->close();
foreach ($serviceResponses as $response) {
$responses[$response->getToken()] = new \DateTime(date("c", $response->getTime()));
}
return $responses;
}
/**
* Get opened client.
*
* @param \ZendService\Apple\Apns\Client\AbstractClient $client Client
*
* @return \ZendService\Apple\Apns\Client\AbstractClient
*/
public function getOpenedClient(ServiceAbstractClient $client)
{
$client->open(
$this->isProductionEnvironment() ? ServiceClient::PRODUCTION_URI : ServiceClient::SANDBOX_URI,
$this->getParameter('certificate'),
$this->getParameter('passPhrase')
);
return $client;
}
/**
* Get service message from origin.
*
* @param \Sly\NotificationPusher\Model\DeviceInterface $device Device
* @param \Sly\NotificationPusher\Model\MessageInterface $message Message
*
* @return \ZendService\Apple\Apns\Message
*/
public function getServiceMessageFromOrigin(DeviceInterface $device, MessageInterface $message)
{
$badge = ($message->hasOption('badge'))
? (int) ($message->getOption('badge') + $device->getParameter('badge', 0))
: 0
;
$sound = $message->getOption('sound', 'bingbong.aiff');
$alert = new ServiceAlert(
$message->getText(),
$message->getOption('actionLocKey'),
$message->getOption('locKey'),
$message->getOption('locArgs'),
$message->getOption('launchImage')
);
if ($actionLocKey = $message->getOption('actionLocKey')) {
$alert->setActionLocKey($actionLocKey);
}
if ($locKey = $message->getOption('locKey')) {
$alert->setLocKey($locKey);
}
if ($locArgs = $message->getOption('locArgs')) {
$alert->setLocArgs($locArgs);
}
if ($launchImage = $message->getOption('launchImage')) {
$alert->setLaunchImage($launchImage);
}
$serviceMessage = new ServiceMessage();
$serviceMessage->setId(sha1($device->getToken().$message->getText()));
$serviceMessage->setAlert($alert);
$serviceMessage->setToken($device->getToken());
$serviceMessage->setBadge($badge);
$serviceMessage->setCustom($message->getOption('custom', array()));
if (null !== $sound) {
$serviceMessage->setSound($sound);
}
return $serviceMessage;
}
/**
* {@inheritdoc}
*/
public function supports($token)
{
return (ctype_xdigit($token) && 64 == strlen($token));
}
/**
* {@inheritdoc}
*/
public function getDefaultParameters()
{
return array('passPhrase' => null);
}
/**
* {@inheritdoc}
*/
public function getRequiredParameters()
{
return array('certificate');
}
}

View File

@@ -0,0 +1,130 @@
<?php
/*
* This file is part of NotificationPusher.
*
* (c) 2013 Cédric Dugat <cedric@dugat.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sly\NotificationPusher\Adapter;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Sly\NotificationPusher\Model\BaseParameteredModel;
use Sly\NotificationPusher\PushManager;
/**
* BaseAdapter.
*
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class BaseAdapter extends BaseParameteredModel
{
/**
* @var string
*/
protected $adapterKey;
/**
* @var string
*/
protected $environment;
/**
* @var mixed
*/
protected $response;
/**
* Constructor.
*
* @param array $parameters Adapter specific parameters
*/
public function __construct(array $parameters = array())
{
$resolver = new OptionsResolver();
$resolver->setDefaults($this->getDefaultParameters());
$resolver->setRequired($this->getRequiredParameters());
$reflectedClass = new \ReflectionClass($this);
$this->adapterKey = lcfirst($reflectedClass->getShortName());
$this->parameters = $resolver->resolve($parameters);
}
/**
* __toString.
*
* @return string
*/
public function __toString()
{
return ucfirst($this->getAdapterKey());
}
/**
* Return the original response.
*
* @return mixed
*/
public function getResponse()
{
return $this->response;
}
/**
* Get AdapterKey.
*
* @return string
*/
public function getAdapterKey()
{
return $this->adapterKey;
}
/**
* Get Environment.
*
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Set Environment.
*
* @param string $environment Environment value to set
*
* @return \Sly\NotificationPusher\Adapter\AdapterInterface
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}
/**
* isDevelopmentEnvironment.
*
* @return boolean
*/
public function isDevelopmentEnvironment()
{
return (PushManager::ENVIRONMENT_DEV === $this->getEnvironment());
}
/**
* isProductionEnvironment.
*
* @return boolean
*/
public function isProductionEnvironment()
{
return (PushManager::ENVIRONMENT_PROD === $this->getEnvironment());
}
}

View File

@@ -0,0 +1,181 @@
<?php
/*
* This file is part of NotificationPusher.
*
* (c) 2013 Cédric Dugat <cedric@dugat.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\MessageInterface;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Exception\PushException;
use Zend\Http\Client as HttpClient;
use Zend\Http\Client\Adapter\Socket as HttpSocketAdapter;
use ZendService\Google\Gcm\Client as ServiceClient;
use ZendService\Google\Gcm\Message as ServiceMessage;
use ZendService\Google\Exception\RuntimeException as ServiceRuntimeException;
use InvalidArgumentException;
/**
* GCM adapter.
*
* @uses \Sly\NotificationPusher\Adapter\BaseAdapter
* @uses \Sly\NotificationPusher\Adapter\AdapterInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class Gcm extends BaseAdapter implements AdapterInterface
{
/**
* @var \Zend\Http\Client
*/
private $httpClient;
/**
* {@inheritdoc}
*/
public function supports($token)
{
return (bool) preg_match('/[0-9a-zA-Z\-\_]/i', $token);
}
/**
* {@inheritdoc}
*
* @throws \Sly\NotificationPusher\Exception\PushException
*/
public function push(PushInterface $push)
{
$client = $this->getOpenedClient(new ServiceClient());
$pushedDevices = new DeviceCollection();
$tokens = array_chunk($push->getDevices()->getTokens(), 100);
foreach ($tokens as $tokensRange) {
$message = $this->getServiceMessageFromOrigin($tokensRange, $push->getMessage());
try {
$this->response = $client->send($message);
} catch (ServiceRuntimeException $e) {
throw new PushException($e->getMessage());
}
if ((bool) $this->response->getSuccessCount()) {
foreach ($tokensRange as $token) {
$pushedDevices->add($push->getDevices()->get($token));
}
}
}
return $pushedDevices;
}
/**
* Get opened client.
*
* @param \ZendService\Google\Gcm\Client $client Client
*
* @return \ZendService\Google\Gcm\Client
*/
public function getOpenedClient(ServiceClient $client)
{
$client->setApiKey($this->getParameter('apiKey'));
if ($this->httpClient !== null) {
$client->setHttpClient($this->httpClient);
}
return $client;
}
/**
* Get service message from origin.
*
* @param array $tokens Tokens
* @param \Sly\NotificationPusher\Model\MessageInterface $message Message
*
* @return \ZendService\Google\Gcm\Message
*/
public function getServiceMessageFromOrigin(array $tokens, MessageInterface $message)
{
$data = $message->getOptions();
$data['message'] = $message->getText();
$serviceMessage = new ServiceMessage();
$serviceMessage->setRegistrationIds($tokens);
$serviceMessage->setData($data);
$serviceMessage->setCollapseKey($this->getParameter('collapseKey'));
$serviceMessage->setRestrictedPackageName($this->getParameter('restrictedPackageName'));
$serviceMessage->setDelayWhileIdle($this->getParameter('delayWhileIdle', false));
$serviceMessage->setTimeToLive($this->getParameter('ttl', 600));
$serviceMessage->setDryRun($this->getParameter('dryRun', false));
return $serviceMessage;
}
/**
* {@inheritdoc}
*/
public function getDefaultParameters()
{
return array();
}
/**
* {@inheritdoc}
*/
public function getRequiredParameters()
{
return array('apiKey');
}
/**
* Get the current Zend Http Client instance.
*
* @return HttpClient
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* Overrides the default Http Client.
*
* @param HttpClient $client
*/
public function setHttpClient(HttpClient $client)
{
$this->httpClient = $client;
}
/**
* Send custom parameters to the Http Adapter without overriding the Http Client.
*
* @param array $config
*
* @throws \InvalidArgumentException
*/
public function setAdapterParameters(array $config = array())
{
if (!is_array($config) || empty($config)) {
throw new InvalidArgumentException('$config must be an associative array with at least 1 item.');
}
if ($this->httpClient === null) {
$this->httpClient = new HttpClient();
$this->httpClient->setAdapter(new HttpSocketAdapter());
}
$this->httpClient->getAdapter()->setOptions($config);
}
}