Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 10.08.17
* Time: 11:06
*/
namespace Sly\NotificationPusher;
use Sly\NotificationPusher\Model\ResponseInterface;
abstract class AbstractPushService
{
/**
* @var string
*/
protected $environment;
/**
* @var ResponseInterface
*/
protected $response;
/**
* AbstractPushService constructor.
* @param string $environment
*/
public function __construct($environment = PushManager::ENVIRONMENT_DEV)
{
$this->environment = $environment;
}
/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}

View File

@@ -12,6 +12,8 @@
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\Response;
use Sly\NotificationPusher\Model\ResponseInterface;
/**
* AdapterInterface.
@@ -38,6 +40,16 @@ interface AdapterInterface
*/
public function supports($token);
/**
* @return ResponseInterface
*/
public function getResponse();
/**
* @param ResponseInterface $response
*/
public function setResponse(ResponseInterface $response);
/**
* Get defined parameters.
*
@@ -58,4 +70,20 @@ interface AdapterInterface
* @return array
*/
public function getRequiredParameters();
/**
* Get Environment.
*
* @return string
*/
public function getEnvironment();
/**
* Set Environment.
*
* @param string $environment Environment value to set
*
* @return \Sly\NotificationPusher\Adapter\AdapterInterface
*/
public function setEnvironment($environment);
}

View File

@@ -11,20 +11,18 @@
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\BaseOptionedModel;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\DeviceInterface;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Exception\AdapterException;
use Sly\NotificationPusher\Exception\PushException;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Model\BaseOptionedModel;
use Sly\NotificationPusher\Model\DeviceInterface;
use Sly\NotificationPusher\Model\PushInterface;
use ZendService\Apple\Apns\Client\AbstractClient as ServiceAbstractClient;
use ZendService\Apple\Apns\Client\Feedback as ServiceFeedbackClient;
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.
@@ -33,13 +31,17 @@ use ZendService\Apple\Apns\Client\Feedback as ServiceFeedbackClient;
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class Apns extends BaseAdapter
class Apns extends BaseAdapter implements FeedbackAdapterInterface
{
/** @var ServiceClient */
/**
* @var ServiceClient
*/
private $openedClient;
/** @var ServiceFeedbackClient */
/**
* @var ServiceFeedbackClient
*/
private $feedbackClient;
/**
@@ -70,16 +72,32 @@ class Apns extends BaseAdapter
$pushedDevices = new DeviceCollection();
foreach ($push->getDevices() as $device) {
/** @var \ZendService\Apple\Apns\Message $message */
$message = $this->getServiceMessageFromOrigin($device, $push->getMessage());
try {
$this->response = $client->send($message);
} catch (ServiceRuntimeException $e) {
throw new PushException($e->getMessage());
}
/** @var \ZendService\Apple\Apns\Response\Message $response */
$response = $client->send($message);
if (ServiceResponse::RESULT_OK === $this->response->getCode()) {
$pushedDevices->add($device);
$responseArr = [
'id' => $response->getId(),
'token' => $response->getCode(),
];
$push->addResponse($device, $responseArr);
if (ServiceResponse::RESULT_OK === $response->getCode()) {
$pushedDevices->add($device);
} else {
$client->close();
unset($this->openedClient, $client);
// Assign returned new client to the in-scope/in-use $client variable
$client = $this->getOpenedServiceClient();
}
$this->response->addOriginalResponse($device, $response);
$this->response->addParsedResponse($device, $responseArr);
} catch (\RuntimeException $e) {
throw new PushException($e->getMessage());
}
}
@@ -97,6 +115,7 @@ class Apns extends BaseAdapter
$responses = [];
$serviceResponses = $client->feedback();
/** @var \ZendService\Apple\Apns\Response\Feedback $response */
foreach ($serviceResponses as $response) {
$responses[$response->getToken()] = new \DateTime(date('c', $response->getTime()));
}
@@ -107,12 +126,16 @@ class Apns extends BaseAdapter
/**
* Get opened client.
*
* @param \ZendService\Apple\Apns\Client\AbstractClient $client Client
* @param \ZendService\Apple\Apns\Client\AbstractClient|null $client Client
*
* @return \ZendService\Apple\Apns\Client\AbstractClient
*/
public function getOpenedClient(ServiceAbstractClient $client)
public function getOpenedClient(ServiceAbstractClient $client = null)
{
if (!$client) {
$client = new ServiceClient();
}
$client->open(
$this->isProductionEnvironment() ? ServiceClient::PRODUCTION_URI : ServiceClient::SANDBOX_URI,
$this->getParameter('certificate'),
@@ -125,9 +148,9 @@ class Apns extends BaseAdapter
/**
* Get opened ServiceClient
*
* @return ServiceAbstractClient
* @return ServiceClient
*/
private function getOpenedServiceClient()
protected function getOpenedServiceClient()
{
if (!isset($this->openedClient)) {
$this->openedClient = $this->getOpenedClient(new ServiceClient());
@@ -139,7 +162,7 @@ class Apns extends BaseAdapter
/**
* Get opened ServiceFeedbackClient
*
* @return ServiceAbstractClient
* @return ServiceFeedbackClient
*/
private function getOpenedFeedbackClient()
{
@@ -161,13 +184,14 @@ class Apns extends BaseAdapter
public function getServiceMessageFromOrigin(DeviceInterface $device, BaseOptionedModel $message)
{
$badge = ($message->hasOption('badge'))
? (int) ($message->getOption('badge') + $device->getParameter('badge', 0))
: false
;
? (int)($message->getOption('badge') + $device->getParameter('badge', 0))
: false;
$sound = $message->getOption('sound', 'bingbong.aiff');
$sound = $message->getOption('sound');
$contentAvailable = $message->getOption('content-available');
$category = $message->getOption('category');
$category = $message->getOption('category');
$urlArgs = $message->getOption('urlArgs');
$expire = $message->getOption('expire');
$alert = new ServiceAlert(
$message->getText(),
@@ -202,7 +226,7 @@ class Apns extends BaseAdapter
}
$serviceMessage = new ServiceMessage();
$serviceMessage->setId(sha1($device->getToken().$message->getText()));
$serviceMessage->setId(sha1($device->getToken() . $message->getText()));
$serviceMessage->setAlert($alert);
$serviceMessage->setToken($device->getToken());
if (false !== $badge) {
@@ -222,6 +246,14 @@ class Apns extends BaseAdapter
$serviceMessage->setCategory($category);
}
if (null !== $urlArgs) {
$serviceMessage->setUrlArgs($urlArgs);
}
if (null !== $expire) {
$serviceMessage->setExpire($expire);
}
return $serviceMessage;
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 09.08.17
* Time: 17:03
*/
namespace Sly\Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Adapter\BaseAdapter;
use Sly\NotificationPusher\Model\PushInterface;
/**
* Class ApnsAPI
* @package Sly\Sly\NotificationPusher\Adapter
*
* todo: implement with edamov/pushok
*/
class ApnsAPI extends BaseAdapter
{
/**
* Push.
*
* @param \Sly\NotificationPusher\Model\PushInterface $push Push
*
* @return \Sly\NotificationPusher\Collection\DeviceCollection
*/
public function push(PushInterface $push)
{
// TODO: Implement push() method.
}
/**
* Supports.
*
* @param string $token Token
*
* @return boolean
*/
public function supports($token)
{
// TODO: Implement supports() method.
}
/**
* Get defined parameters.
*
* @return array
*/
public function getDefinedParameters()
{
// TODO: Implement getDefinedParameters() method.
}
/**
* Get default parameters.
*
* @return array
*/
public function getDefaultParameters()
{
// TODO: Implement getDefaultParameters() method.
}
/**
* Get required parameters.
*
* @return array
*/
public function getRequiredParameters()
{
// TODO: Implement getRequiredParameters() method.
}
}

View File

@@ -11,10 +11,11 @@
namespace Sly\NotificationPusher\Adapter;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Sly\NotificationPusher\Model\BaseParameteredModel;
use Sly\NotificationPusher\Model\Response;
use Sly\NotificationPusher\Model\ResponseInterface;
use Sly\NotificationPusher\PushManager;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* BaseAdapter.
@@ -34,7 +35,7 @@ abstract class BaseAdapter extends BaseParameteredModel implements AdapterInterf
protected $environment;
/**
* @var mixed
* @var ResponseInterface
*/
protected $response;
@@ -53,6 +54,23 @@ abstract class BaseAdapter extends BaseParameteredModel implements AdapterInterf
$reflectedClass = new \ReflectionClass($this);
$this->adapterKey = lcfirst($reflectedClass->getShortName());
$this->parameters = $resolver->resolve($parameters);
$this->response = new Response();
}
/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
/**
* @param ResponseInterface $response
*/
public function setResponse(ResponseInterface $response)
{
$this->response = $response;
}
/**
@@ -65,16 +83,6 @@ abstract class BaseAdapter extends BaseParameteredModel implements AdapterInterf
return ucfirst($this->getAdapterKey());
}
/**
* Return the original response.
*
* @return mixed
*/
public function getResponse()
{
return $this->response;
}
/**
* Get AdapterKey.
*
@@ -128,4 +136,5 @@ abstract class BaseAdapter extends BaseParameteredModel implements AdapterInterf
{
return (PushManager::ENVIRONMENT_PROD === $this->getEnvironment());
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 09.08.17
* Time: 16:06
*/
namespace Sly\NotificationPusher\Adapter;
interface FeedbackAdapterInterface
{
public function getFeedback();
}

View File

@@ -11,19 +11,18 @@
namespace Sly\NotificationPusher\Adapter;
use Sly\NotificationPusher\Model\BaseOptionedModel;
use Sly\NotificationPusher\Model\PushInterface;
use InvalidArgumentException;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Exception\PushException;
use Sly\NotificationPusher\Model\BaseOptionedModel;
use Sly\NotificationPusher\Model\DeviceInterface;
use Sly\NotificationPusher\Model\GcmMessage;
use Sly\NotificationPusher\Model\PushInterface;
use Zend\Http\Client as HttpClient;
use Zend\Http\Client\Adapter\Socket as HttpSocketAdapter;
use ZendService\Google\Exception\RuntimeException as ServiceRuntimeException;
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.
@@ -49,7 +48,7 @@ class Gcm extends BaseAdapter
*/
public function supports($token)
{
return is_string($token) && $token != '';
return is_string($token) && $token !== '';
}
/**
@@ -67,16 +66,40 @@ class Gcm extends BaseAdapter
$message = $this->getServiceMessageFromOrigin($tokensRange, $push->getMessage());
try {
$this->response = $client->send($message);
/** @var \ZendService\Google\Gcm\Response $response */
$response = $client->send($message);
$responseResults = $response->getResults();
foreach ($tokensRange as $token) {
/** @var DeviceInterface $device */
$device = $push->getDevices()->get($token);
// map the overall response object
// into a per device response
$tokenResponse = [];
if (isset($responseResults[$token]) && is_array($responseResults[$token])) {
$tokenResponse = $responseResults[$token];
}
$responseData = $response->getResponse();
if ($responseData && is_array($responseData)) {
$tokenResponse = array_merge(
$tokenResponse,
array_diff_key($responseData, ['results' => true])
);
}
$push->addResponse($device, $tokenResponse);
$pushedDevices->add($device);
$this->response->addOriginalResponse($device, $response);
$this->response->addParsedResponse($device, $tokenResponse);
}
} 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;
@@ -96,8 +119,8 @@ class Gcm extends BaseAdapter
$newClient = new \Zend\Http\Client(
null,
[
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'sslverifypeer' => false
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'sslverifypeer' => false,
]
);
@@ -114,6 +137,7 @@ class Gcm extends BaseAdapter
* @param BaseOptionedModel|\Sly\NotificationPusher\Model\MessageInterface $message Message
*
* @return \ZendService\Google\Gcm\Message
* @throws \ZendService\Google\Exception\InvalidArgumentException
*/
public function getServiceMessageFromOrigin(array $tokens, BaseOptionedModel $message)
{
@@ -122,7 +146,18 @@ class Gcm extends BaseAdapter
$serviceMessage = new ServiceMessage();
$serviceMessage->setRegistrationIds($tokens);
if (isset($data['notificationData']) && !empty($data['notificationData'])) {
$serviceMessage->setNotification($data['notificationData']);
unset($data['notificationData']);
}
if ($message instanceof GcmMessage) {
$serviceMessage->setNotification($message->getNotificationData());
}
$serviceMessage->setData($data);
$serviceMessage->setCollapseKey($this->getParameter('collapseKey'));
$serviceMessage->setRestrictedPackageName($this->getParameter('restrictedPackageName'));
$serviceMessage->setDelayWhileIdle($this->getParameter('delayWhileIdle', false));
@@ -138,11 +173,11 @@ class Gcm extends BaseAdapter
public function getDefinedParameters()
{
return [
'collapse_key',
'delay_while_idle',
'time_to_live',
'restricted_package_name',
'dry_run'
'collapseKey',
'delayWhileIdle',
'ttl',
'restrictedPackageName',
'dryRun',
];
}

View File

@@ -0,0 +1,215 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 10.08.17
* Time: 10:43
*/
namespace Sly\NotificationPusher;
use Sly\NotificationPusher\Adapter\Apns as ApnsAdapter;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Model\Device;
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\Model\ResponseInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Facade for simple use cases
*
* Class ApnsPushService
* @package Sly\NotificationPusher
*/
class ApnsPushService extends AbstractPushService
{
/**
* @var string
*/
private $certificatePath = '';
/**
* @var string|null
*/
private $passPhrase = '';
/**
* @var array
*/
private $feedback = [];
/**
* IOSPushNotificationService constructor.
* @param string $environment
* @param string $certificatePath
* @param string $passPhrase
*/
public function __construct($certificatePath, $passPhrase = null, $environment = PushManager::ENVIRONMENT_DEV)
{
parent::__construct($environment);
$this->certificatePath = $certificatePath;
$this->passPhrase = $passPhrase;
}
/**
* @param array $tokens
* @param array $notifications
* @param array $params
* @return ResponseInterface
*/
public function push(array $tokens = [], array $notifications = [], array $params = [])
{
if (!$tokens || !$notifications) {
return null;
}
if (!$this->certificatePath) {
throw new \RuntimeException('IOS certificate path must be set');
}
$fs = new Filesystem();
if (!$fs->exists($this->certificatePath) || !is_readable($this->certificatePath)) {
throw new \InvalidArgumentException('Wrong or not readable certificate path');
}
$adapterParams = [];
$deviceParams = [];
$messageParams = [];
if (isset($params) && !empty($params)) {
if (isset($params['adapter'])) {
$adapterParams = $params['adapter'];
}
if (isset($params['device'])) {
$deviceParams = $params['device'];
}
if (isset($params['message'])) {
$messageParams = $params['message'];
}
}
$adapterParams['certificate'] = $this->certificatePath;
$adapterParams['passPhrase'] = $this->passPhrase;
// Development one by default (without argument).
/** @var PushManager $pushManager */
$pushManager = new PushManager($this->environment);
// Then declare an adapter.
$apnsAdapter = new ApnsAdapter($adapterParams);
// Set the device(s) to push the notification to.
$devices = new DeviceCollection([]);
//devices
foreach ($tokens as $token) {
$devices->add(new Device($token, $deviceParams));
}
foreach ($notifications as $notificationText) {
// Then, create the push skel.
$message = new Message($notificationText, $messageParams);
// Finally, create and add the push to the manager, and push it!
$push = new Push($apnsAdapter, $devices, $message);
$pushManager->add($push);
}
// Returns a collection of notified devices
$pushes = $pushManager->push();
$this->response = $apnsAdapter->getResponse();
$this->feedback = [];
return $this->response;
}
/**
* Use feedback to get not registered tokens from last send
* and remove them from your DB
*
* @return array
*/
public function feedback()
{
$adapterParams = [];
$adapterParams['certificate'] = $this->certificatePath;
$adapterParams['passPhrase'] = $this->passPhrase;
// Development one by default (without argument).
/** @var PushManager $pushManager */
$pushManager = new PushManager($this->environment);
// Then declare an adapter.
$apnsAdapter = new ApnsAdapter($adapterParams);
$this->feedback = $pushManager->getFeedback($apnsAdapter);
return $this->feedback;
}
/**
* The Apple Push Notification service includes a feedback service to give you information
* about failed remote notifications. When a remote notification cannot be delivered
* because the intended app does not exist on the device,
* the feedback service adds that devices token to its list.
*
* @return array
*/
public function getFeedback()
{
return $this->feedback;
}
/**
* @return array
*/
public function getInvalidTokens()
{
if (!$this->response) {
return [];
}
if (!$this->feedback) {
$this->feedback = $this->feedback();
}
$feedbackTokens = array_keys($this->feedback);
//all bad
if ($feedbackTokens) {
return $feedbackTokens;
}
return [];
}
/**
* @return array
*/
public function getSuccessfulTokens()
{
if (!$this->response) {
return [];
}
if (!$this->feedback) {
$this->feedback = $this->feedback();
}
$feedbackTokens = array_keys($this->feedback);
$sentTokens = array_keys($this->response->getParsedResponses());
//all bad
if (!$feedbackTokens) {
return $sentTokens;
}
$tokens = array_diff($sentTokens, $feedbackTokens);
return $tokens;
}
}

View File

@@ -17,13 +17,19 @@ namespace Sly\NotificationPusher\Collection;
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class AbstractCollection
abstract class AbstractCollection implements \IteratorAggregate, \Countable
{
/**
* @var \ArrayIterator
*/
protected $coll;
/**
* @inheritdoc
* @return \ArrayIterator|\SeekableIterator
*/
abstract public function getIterator();
/**
* Get.
*
@@ -43,7 +49,7 @@ abstract class AbstractCollection
*/
public function count()
{
return count($this->getIterator());
return $this->getIterator()->count();
}
/**
@@ -63,4 +69,38 @@ abstract class AbstractCollection
{
$this->coll = new \ArrayIterator();
}
/**
* @return mixed|null
*/
public function first()
{
$tmp = clone $this->coll;
//go to the beginning
$tmp->rewind();
if (!$tmp->valid()) {
return null;
}
return $tmp->current();
}
/**
* @return mixed|null
*/
public function last()
{
$tmp = clone $this->coll;
//go to the end
$tmp->seek($tmp->count() - 1);
if (!$tmp->valid()) {
return null;
}
return $tmp->current();
}
}

View File

@@ -20,7 +20,7 @@ use Sly\NotificationPusher\Model\PushInterface;
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushCollection extends AbstractCollection implements \IteratorAggregate
class PushCollection extends AbstractCollection
{
/**
* Constructor.

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of NotificationPusher.
*
* (c) 2016 Lukas Klinzing <theluk@gmail.com>
* (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\Collection;
/**
* Response Collection.
* is just a container for a response from a push service
*
* @uses \Sly\NotificationPusher\Collection\AbstractCollection
* @uses \IteratorAggregate
* @author Lukas Klinzing <theluk@gmail.com>
*/
class ResponseCollection extends AbstractCollection
{
/**
* Constructor.
*/
public function __construct()
{
$this->coll = new \ArrayIterator();
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->coll;
}
/**
* @param string $token
* @param mixed $response
*/
public function add($token, $response)
{
$this->coll[$token] = $response;
}
}

View File

@@ -11,9 +11,9 @@
namespace Sly\NotificationPusher\Console;
use Symfony\Component\Console\Application as BaseApplication;
use Sly\NotificationPusher\NotificationPusher;
use Sly\NotificationPusher\Console\Command\PushCommand;
use Sly\NotificationPusher\NotificationPusher;
use Symfony\Component\Console\Application as BaseApplication;
/**
* Application.

View File

@@ -11,20 +11,18 @@
namespace Sly\NotificationPusher\Console\Command;
use Doctrine\Common\Inflector\Inflector;
use Sly\NotificationPusher\Exception\AdapterException;
use Sly\NotificationPusher\Model\Device;
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\PushManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Sly\NotificationPusher\PushManager;
use Sly\NotificationPusher\Model\Device;
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\Exception\AdapterException;
use Doctrine\Common\Inflector\Inflector;
/**
* PushCommand.
*
@@ -108,7 +106,7 @@ class PushCommand extends Command
private function getAdapterClassFromArgument($argument)
{
if (!class_exists($adapterClass = $argument) &&
!class_exists($adapterClass = '\\Sly\\NotificationPusher\\Adapter\\'.ucfirst($argument))) {
!class_exists($adapterClass = '\\Sly\\NotificationPusher\\Adapter\\' . ucfirst($argument))) {
throw new AdapterException(
sprintf(
'Adapter class %s does not exist',

View File

@@ -0,0 +1,159 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 10.08.17
* Time: 10:43
*/
namespace Sly\NotificationPusher;
use Sly\NotificationPusher\Adapter\Gcm as GcmAdapter;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Model\Device;
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\Model\ResponseInterface;
/**
* Facade for simple use cases
*
* Class GcmPushService
* @package Sly\NotificationPusher
*/
class GcmPushService extends AbstractPushService
{
/**
* @var string
*/
private $apiKey = '';
/**
* GcmPushService constructor.
* @param string $environment
* @param string $apiKey
*/
public function __construct($apiKey, $environment = PushManager::ENVIRONMENT_DEV)
{
parent::__construct($environment);
$this->apiKey = $apiKey;
}
/**
* params keys
* adapter
* message
* device
*
* @param array $tokens
* @param array $notifications
* @param array $params
* @return ResponseInterface
*/
public function push(array $tokens = [], array $notifications = [], array $params = [])
{
if (!$tokens || !$notifications) {
return null;
}
$adapterParams = [];
$deviceParams = [];
$messageParams = [];
if (isset($params) && !empty($params)) {
if (isset($params['adapter'])) {
$adapterParams = $params['adapter'];
}
if (isset($params['device'])) {
$deviceParams = $params['device'];
}
if (isset($params['message'])) {
$messageParams = $params['message'];
}
//because we have now notification and data separated
if (isset($params['notificationData'])) {
$messageParams['notificationData'] = $params['notificationData'];
}
}
$adapterParams['apiKey'] = $this->apiKey;
if (!$this->apiKey) {
throw new \RuntimeException('Android api key must be set');
}
// Development one by default (without argument).
/** @var PushManager $pushManager */
$pushManager = new PushManager($this->environment);
// Then declare an adapter.
$gcmAdapter = new GcmAdapter($adapterParams);
// Set the device(s) to push the notification to.
$devices = new DeviceCollection([]);
//devices
foreach ($tokens as $token) {
$devices->add(new Device($token, $deviceParams));
}
foreach ($notifications as $notificationText) {
// Then, create the push skel.
$message = new Message($notificationText, $messageParams);
// Finally, create and add the push to the manager, and push it!
$push = new Push($gcmAdapter, $devices, $message);
$pushManager->add($push);
}
// Returns a collection of notified devices
$pushes = $pushManager->push();
$this->response = $gcmAdapter->getResponse();
return $this->response;
}
/**
* @return array
*/
public function getInvalidTokens()
{
if (!$this->response) {
return [];
}
$tokens = [];
foreach ($this->response->getParsedResponses() as $token => $response) {
if (array_key_exists('error', $response) && !array_key_exists('message_id', $response)) {
array_push($tokens, $token);
}
}
return $tokens;
}
/**
* @return array
*/
public function getSuccessfulTokens()
{
if (!$this->response) {
return [];
}
$tokens = [];
foreach ($this->response->getParsedResponses() as $token => $response) {
if (!array_key_exists('error', $response) && array_key_exists('message_id', $response)) {
array_push($tokens, $token);
}
}
return $tokens;
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Sly\NotificationPusher\Model;
class ApnsMessage extends Message
{
}

View File

@@ -48,8 +48,8 @@ abstract class BaseOptionedModel
/**
* Get option.
*
* @param string $key Key
* @param mixed $default Default
* @param string $key Key
* @param mixed $default Default
*
* @return mixed
*/
@@ -75,8 +75,8 @@ abstract class BaseOptionedModel
/**
* Set option.
*
* @param string $key Key
* @param mixed $value Value
* @param string $key Key
* @param mixed $value Value
*
* @return mixed
*/

View File

@@ -48,8 +48,8 @@ abstract class BaseParameteredModel
/**
* Get parameter.
*
* @param string $key Key
* @param mixed $default Default
* @param string $key Key
* @param mixed $default Default
*
* @return mixed
*/
@@ -75,8 +75,8 @@ abstract class BaseParameteredModel
/**
* Set parameter.
*
* @param string $key Key
* @param mixed $value Value
* @param string $key Key
* @param mixed $value Value
*
* @return mixed
*/

View File

@@ -26,8 +26,8 @@ class Device extends BaseParameteredModel implements DeviceInterface
/**
* Constructor.
*
* @param string $token Token
* @param array $parameters Parameters
* @param string $token Token
* @param array $parameters Parameters
*/
public function __construct($token, array $parameters = [])
{

View File

@@ -0,0 +1,28 @@
<?php
namespace Sly\NotificationPusher\Model;
class GcmMessage extends Message
{
/**
* @var array
*/
private $notificationData = [];
/**
* @return array
*/
public function getNotificationData()
{
return $this->notificationData;
}
/**
* @param array $notificationData
*/
public function setNotificationData($notificationData)
{
$this->notificationData = $notificationData;
}
}

View File

@@ -21,13 +21,13 @@ class Message extends BaseOptionedModel implements MessageInterface
/**
* @var string
*/
private $text;
protected $text;
/**
* Constructor.
*
* @param string $text Text
* @param array $options Options
* @param string $text Text
* @param array $options Options
*/
public function __construct($text, array $options = [])
{

View File

@@ -11,8 +11,9 @@
namespace Sly\NotificationPusher\Model;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Adapter\AdapterInterface;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Collection\ResponseCollection;
use Sly\NotificationPusher\Exception\AdapterException;
/**
@@ -48,13 +49,18 @@ class Push extends BaseOptionedModel implements PushInterface
*/
private $pushedAt;
/**
* @var \Sly\NotificationPusher\Collection\ResponseCollection
*/
private $responses;
/**
* Constructor.
*
* @param \Sly\NotificationPusher\Adapter\AdapterInterface $adapter Adapter
* @param DeviceInterface|DeviceCollection $devices Device(s)
* @param \Sly\NotificationPusher\Model\MessageInterface $message Message
* @param array $options Options
* @param DeviceInterface|DeviceCollection $devices Device(s)
* @param \Sly\NotificationPusher\Model\MessageInterface $message Message
* @param array $options Options
*
* Options are adapters specific ones, like Apns "badge" or "sound" option for example.
* Of course, they can be more general.
@@ -78,6 +84,7 @@ class Push extends BaseOptionedModel implements PushInterface
/**
* Check devices tokens.
* @throws \Sly\NotificationPusher\Exception\AdapterException
*/
private function checkDevicesTokens()
{
@@ -89,7 +96,7 @@ class Push extends BaseOptionedModel implements PushInterface
throw new AdapterException(
sprintf(
'Adapter %s does not support %s token\'s device',
(string) $adapter,
(string)$adapter,
$device->getToken()
)
);
@@ -128,7 +135,7 @@ class Push extends BaseOptionedModel implements PushInterface
*/
public function isPushed()
{
return (bool) (self::STATUS_PUSHED === $this->status);
return (bool)(self::STATUS_PUSHED === $this->status);
}
/**
@@ -218,6 +225,28 @@ class Push extends BaseOptionedModel implements PushInterface
return $this;
}
/**
* Get Responses
* @return \Sly\NotificationPusher\Collection\ResponseCollection
*/
public function getResponses()
{
if (!$this->responses)
$this->responses = new ResponseCollection();
return $this->responses;
}
/**
* adds a response
* @param \Sly\NotificationPusher\Model\DeviceInterface $device
* @param mixed $response
*/
public function addResponse(DeviceInterface $device, $response)
{
$this->getResponses()->add($device->getToken(), $response);
}
/**
* Get PushedAt.
*

View File

@@ -104,6 +104,19 @@ interface PushInterface
*/
public function setDevices(DeviceCollection $devices);
/**
* Get Responses
* @return \Sly\NotificationPusher\Collection\ResponseCollection
*/
public function getResponses();
/**
* adds a response
* @param \Sly\NotificationPusher\Model\DeviceInterface $device
* @param mixed $response
*/
public function addResponse(DeviceInterface $device, $response);
/**
* Get PushedAt.
*

View File

@@ -0,0 +1,92 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 09.08.17
* Time: 17:57
*/
namespace Sly\NotificationPusher\Model;
use Sly\NotificationPusher\Collection\PushCollection;
class Response implements ResponseInterface
{
/**
* @var array
*/
private $parsedResponses = [];
/**
* @var array
*/
private $originalResponses = [];
/**
* @var PushCollection
*/
private $pushCollection;
/**
* Response constructor.
*/
public function __construct()
{
$this->pushCollection = new PushCollection();
}
/**
* @param DeviceInterface $device
* @param array $response
*/
public function addParsedResponse(DeviceInterface $device, $response)
{
if (!is_array($response)) {
throw new \InvalidArgumentException('Response must be array type');
}
$this->parsedResponses[$device->getToken()] = $response;
}
/**
* @param DeviceInterface $device
* @param mixed $originalResponse
*/
public function addOriginalResponse(DeviceInterface $device, $originalResponse)
{
$this->originalResponses[$device->getToken()] = $originalResponse;
}
/**
* @param \Sly\NotificationPusher\Model\PushInterface $push Push
*/
public function addPush(PushInterface $push)
{
$this->pushCollection->add($push);
}
/**
* @return array
*/
public function getParsedResponses()
{
return $this->parsedResponses;
}
/**
* @return mixed
*/
public function getOriginalResponses()
{
return $this->originalResponses;
}
/**
* @return PushCollection
*/
public function getPushCollection()
{
return $this->pushCollection;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 10.08.17
* Time: 10:30
*/
namespace Sly\NotificationPusher\Model;
use Sly\NotificationPusher\Collection\PushCollection;
interface ResponseInterface
{
/**
* @param DeviceInterface $device
* @param array $response
*/
public function addParsedResponse(DeviceInterface $device, $response);
/**
* @param DeviceInterface $device
* @param mixed $originalResponse
*/
public function addOriginalResponse(DeviceInterface $device, $originalResponse);
/**
* @param \Sly\NotificationPusher\Model\PushInterface $push Push
*/
public function addPush(PushInterface $push);
/**
* @return array
*/
public function getParsedResponses();
/**
* @return mixed
*/
public function getOriginalResponses();
/**
* @return PushCollection
*/
public function getPushCollection();
}

View File

@@ -11,9 +11,13 @@
namespace Sly\NotificationPusher;
use Sly\NotificationPusher\Collection\PushCollection;
use Sly\NotificationPusher\Adapter\AdapterInterface;
use Sly\NotificationPusher\Adapter\FeedbackAdapterInterface;
use Sly\NotificationPusher\Collection\PushCollection;
use Sly\NotificationPusher\Exception\AdapterException;
use Sly\NotificationPusher\Model\Push;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\ResponseInterface;
/**
* PushManager.
@@ -21,7 +25,7 @@ use Sly\NotificationPusher\Exception\AdapterException;
* @uses \Sly\NotificationPusher\Collection\PushCollection
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushManager extends PushCollection
class PushManager
{
const ENVIRONMENT_DEV = 'dev';
const ENVIRONMENT_PROD = 'prod';
@@ -31,6 +35,16 @@ class PushManager extends PushCollection
*/
private $environment;
/**
* @var PushCollection
*/
private $pushCollection;
/**
* @var ResponseInterface
*/
private $response;
/**
* Constructor.
*
@@ -38,9 +52,16 @@ class PushManager extends PushCollection
*/
public function __construct($environment = self::ENVIRONMENT_DEV)
{
parent::__construct();
$this->environment = $environment;
$this->pushCollection = new PushCollection();
}
$this->environment = $environment;
/**
* @param \Sly\NotificationPusher\Model\PushInterface $push Push
*/
public function add(PushInterface $push)
{
$this->pushCollection->add($push);
}
/**
@@ -56,11 +77,12 @@ class PushManager extends PushCollection
/**
* Push.
*
* @return \Sly\NotificationPusher\Collection\PushCollection
* @return PushCollection
*/
public function push()
{
foreach ($this as $push) {
/** @var Push $push */
foreach ($this->pushCollection as $push) {
$adapter = $push->getAdapter();
$adapter->setEnvironment($this->getEnvironment());
@@ -69,7 +91,13 @@ class PushManager extends PushCollection
}
}
return $this;
if ($this->pushCollection && !$this->pushCollection->isEmpty()) {
/** @var Push $push */
$push = $this->pushCollection->first();
$this->response = $push->getAdapter()->getResponse();
}
return $this->pushCollection;
}
/**
@@ -83,11 +111,11 @@ class PushManager extends PushCollection
*/
public function getFeedback(AdapterInterface $adapter)
{
if (false === method_exists($adapter, 'getFeedback')) {
if (!$adapter instanceof FeedbackAdapterInterface) {
throw new AdapterException(
sprintf(
'%s adapter has no dedicated "getFeedback" method',
(string) $adapter
(string)$adapter
)
);
}
@@ -95,4 +123,20 @@ class PushManager extends PushCollection
return $adapter->getFeedback();
}
/**
* @return PushCollection
*/
public function getPushCollection()
{
return $this->pushCollection;
}
/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}