laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

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

View File

@@ -1,73 +0,0 @@
<?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\Collection\DeviceCollection;
use Sly\NotificationPusher\Model\PushInterface;
use Sly\NotificationPusher\Model\ResponseInterface;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
interface AdapterInterface
{
/**
* @param PushInterface $push Push
*
* @return DeviceCollection
*/
public function push(PushInterface $push);
/**
* @param string $token Token
*
* @return boolean
*/
public function supports($token);
/**
* @return ResponseInterface
*/
public function getResponse();
/**
* @param ResponseInterface $response
*/
public function setResponse(ResponseInterface $response);
/**
* @return array
*/
public function getDefinedParameters();
/**
* @return array
*/
public function getDefaultParameters();
/**
* @return array
*/
public function getRequiredParameters();
/**
* @return string
*/
public function getEnvironment();
/**
* @param string $environment Environment value to set
*
* @return AdapterInterface
*/
public function setEnvironment($environment);
}

View File

@@ -1,285 +0,0 @@
<?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\Collection\DeviceCollection;
use Sly\NotificationPusher\Exception\AdapterException;
use Sly\NotificationPusher\Exception\PushException;
use Sly\NotificationPusher\Model\BaseOptionedModel;
use Sly\NotificationPusher\Model\DeviceInterface;
use Sly\NotificationPusher\Model\MessageInterface;
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\Feedback;
use ZendService\Apple\Apns\Response\Message as ServiceResponse;
/**
* @uses \Sly\NotificationPusher\Adapter\BaseAdapter
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class Apns extends BaseAdapter implements FeedbackAdapterInterface
{
/**
* @var ServiceClient
*/
private $openedClient;
/**
* @var ServiceFeedbackClient
*/
private $feedbackClient;
/**
* {@inheritdoc}
*
* @throws AdapterException
*/
public function __construct(array $parameters = [])
{
parent::__construct($parameters);
$cert = $this->getParameter('certificate');
if (false === file_exists($cert)) {
throw new AdapterException(sprintf('Certificate %s does not exist', $cert));
}
}
/**
* {@inheritdoc}
*
* @throws PushException
*/
public function push(PushInterface $push)
{
$client = $this->getOpenedServiceClient();
$pushedDevices = new DeviceCollection();
foreach ($push->getDevices() as $device) {
$message = $this->getServiceMessageFromOrigin($device, $push->getMessage());
try {
/** @var ServiceResponse $response */
$response = $client->send($message);
$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());
}
}
return $pushedDevices;
}
/**
* @return array
*/
public function getFeedback()
{
$client = $this->getOpenedFeedbackClient();
$responses = [];
$serviceResponses = $client->feedback();
/** @var Feedback $response */
foreach ($serviceResponses as $response) {
$responses[$response->getToken()] = new \DateTime(date('c', $response->getTime()));
}
return $responses;
}
/**
* @param ServiceAbstractClient|null $client Client
*
* @return ServiceAbstractClient
*/
public function getOpenedClient(ServiceAbstractClient $client = null)
{
if (!$client) {
$client = new ServiceClient();
}
$client->open(
$this->isProductionEnvironment() ? ServiceClient::PRODUCTION_URI : ServiceClient::SANDBOX_URI,
$this->getParameter('certificate'),
$this->getParameter('passPhrase')
);
return $client;
}
/**
* @return ServiceClient
*/
protected function getOpenedServiceClient()
{
if (!isset($this->openedClient)) {
$this->openedClient = $this->getOpenedClient(new ServiceClient());
}
return $this->openedClient;
}
/**
* @return ServiceFeedbackClient
*/
private function getOpenedFeedbackClient()
{
if (!isset($this->feedbackClient)) {
$this->feedbackClient = $this->getOpenedClient(new ServiceFeedbackClient());
}
return $this->feedbackClient;
}
/**
* @param DeviceInterface $device Device
* @param BaseOptionedModel|MessageInterface $message Message
*
* @return ServiceMessage
*/
public function getServiceMessageFromOrigin(DeviceInterface $device, BaseOptionedModel $message)
{
$badge = ($message->hasOption('badge'))
? (int) ($message->getOption('badge') + $device->getParameter('badge', 0))
: false;
$sound = $message->getOption('sound');
$contentAvailable = $message->getOption('content-available');
$mutableContent = $message->getOption('mutable-content');
$category = $message->getOption('category');
$urlArgs = $message->getOption('urlArgs');
$expire = $message->getOption('expire');
$alert = new ServiceAlert(
$message->getText(),
$message->getOption('actionLocKey'),
$message->getOption('locKey'),
$message->getOption('locArgs'),
$message->getOption('launchImage'),
$message->getOption('title'),
$message->getOption('titleLocKey'),
$message->getOption('titleLocArgs')
);
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);
}
if ($title = $message->getOption('title')) {
$alert->setTitle($title);
}
if ($titleLocKey = $message->getOption('titleLocKey')) {
$alert->setTitleLocKey($titleLocKey);
}
if ($titleLocArgs = $message->getOption('titleLocArgs')) {
$alert->setTitleLocArgs($titleLocArgs);
}
$serviceMessage = new ServiceMessage();
$serviceMessage->setId(sha1($device->getToken() . $message->getText()));
$serviceMessage->setAlert($alert);
$serviceMessage->setToken($device->getToken());
if (false !== $badge) {
$serviceMessage->setBadge($badge);
}
$serviceMessage->setCustom($message->getOption('custom', []));
if (null !== $sound) {
$serviceMessage->setSound($sound);
}
if (null !== $contentAvailable) {
$serviceMessage->setContentAvailable($contentAvailable);
}
if (null !== $mutableContent) {
$serviceMessage->setMutableContent($mutableContent);
}
if (null !== $category) {
$serviceMessage->setCategory($category);
}
if (null !== $urlArgs) {
$serviceMessage->setUrlArgs($urlArgs);
}
if (null !== $expire) {
$serviceMessage->setExpire($expire);
}
return $serviceMessage;
}
/**
* {@inheritdoc}
*/
public function supports($token)
{
return ctype_xdigit($token);
}
/**
* {@inheritdoc}
*/
public function getDefinedParameters()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getDefaultParameters()
{
return ['passPhrase' => null];
}
/**
* {@inheritdoc}
*/
public function getRequiredParameters()
{
return ['certificate'];
}
}

View File

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

View File

@@ -1,124 +0,0 @@
<?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\BaseParameteredModel;
use Sly\NotificationPusher\Model\Response;
use Sly\NotificationPusher\Model\ResponseInterface;
use Sly\NotificationPusher\PushManager;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class BaseAdapter extends BaseParameteredModel implements AdapterInterface
{
/**
* @var string
*/
protected $adapterKey;
/**
* @var string
*/
protected $environment;
/**
* @var ResponseInterface
*/
protected $response;
/**
* @param array $parameters Adapter specific parameters
*/
public function __construct(array $parameters = [])
{
$resolver = new OptionsResolver();
$resolver->setDefined($this->getDefinedParameters());
$resolver->setDefaults($this->getDefaultParameters());
$resolver->setRequired($this->getRequiredParameters());
$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;
}
/**
* @return string
*/
public function __toString()
{
return ucfirst($this->getAdapterKey());
}
/**
* @return string
*/
public function getAdapterKey()
{
return $this->adapterKey;
}
/**
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* @param string $environment Environment value to set
*
* @return AdapterInterface
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}
/**
* @return boolean
*/
public function isDevelopmentEnvironment()
{
return (PushManager::ENVIRONMENT_DEV === $this->getEnvironment());
}
/**
* @return boolean
*/
public function isProductionEnvironment()
{
return (PushManager::ENVIRONMENT_PROD === $this->getEnvironment());
}
}

View File

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

View File

@@ -1,243 +0,0 @@
<?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 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\MessageInterface;
use Sly\NotificationPusher\Model\PushInterface;
use Zend\Http\Client as HttpClient;
use Zend\Http\Client\Adapter\Socket as HttpSocketAdapter;
use ZendService\Google\Exception\InvalidArgumentException as ZendInvalidArgumentException;
use ZendService\Google\Exception\RuntimeException as ServiceRuntimeException;
use ZendService\Google\Gcm\Client as ServiceClient;
use ZendService\Google\Gcm\Message as ServiceMessage;
use ZendService\Google\Gcm\Response;
/**
* @uses \Sly\NotificationPusher\Adapter\BaseAdapter
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class Gcm extends BaseAdapter
{
/**
* @var HttpClient
*/
private $httpClient;
/**
* @var ServiceClient
*/
private $openedClient;
/**
* {@inheritdoc}
*/
public function supports($token)
{
return is_string($token) && $token !== '';
}
/**
* {@inheritdoc}
*
* @throws PushException
*/
public function push(PushInterface $push)
{
$client = $this->getOpenedClient();
$pushedDevices = new DeviceCollection();
$tokens = array_chunk($push->getDevices()->getTokens(), 100);
foreach ($tokens as $tokensRange) {
$message = $this->getServiceMessageFromOrigin($tokensRange, $push->getMessage());
try {
/** @var 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());
}
}
return $pushedDevices;
}
/**
* Get opened client.
*
* @return ServiceClient
*/
public function getOpenedClient()
{
if (!isset($this->openedClient)) {
$this->openedClient = new ServiceClient();
$this->openedClient->setApiKey($this->getParameter('apiKey'));
$newClient = new HttpClient(
null,
[
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'sslverifypeer' => false,
]
);
$this->openedClient->setHttpClient($newClient);
}
return $this->openedClient;
}
/**
* Get service message from origin.
*
* @param array $tokens Tokens
* @param BaseOptionedModel|MessageInterface $message Message
*
* @return ServiceMessage
* @throws ZendInvalidArgumentException
*/
public function getServiceMessageFromOrigin(array $tokens, BaseOptionedModel $message)
{
$data = $message->getOptions();
$data['message'] = $message->getText();
$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->setPriority($this->getParameter('priority', 'normal'));
$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 getDefinedParameters()
{
return [
'collapseKey',
'priority',
'delayWhileIdle',
'ttl',
'restrictedPackageName',
'dryRun',
];
}
/**
* {@inheritdoc}
*/
public function getDefaultParameters()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getRequiredParameters()
{
return ['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 = [])
{
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);
}
}

View File

@@ -1,212 +0,0 @@
<?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;
/**
* @package Sly\NotificationPusher
* @author Oleg Abrazhaev <seyferseed@gmail.com>
*/
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).
$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

@@ -1,104 +0,0 @@
<?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\Collection;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use SeekableIterator;
use Sly\NotificationPusher\Model\MessageInterface;
/**
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class AbstractCollection implements IteratorAggregate, Countable
{
/**
* @var ArrayIterator
*/
protected $coll;
/**
* @inheritdoc
* @return ArrayIterator|SeekableIterator
*/
abstract public function getIterator();
/**
* @param string $key Key
*
* @return MessageInterface|false
*/
public function get($key)
{
return isset($this->coll[$key]) ? $this->coll[$key] : false;
}
/**
* @return integer
*/
public function count()
{
return $this->getIterator()->count();
}
/**
* @return boolean
*/
public function isEmpty()
{
return $this->count() === 0;
}
/**
* Clear categories.
*/
public function clear()
{
$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

@@ -1,64 +0,0 @@
<?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\Collection;
use Sly\NotificationPusher\Model\DeviceInterface;
/**
* @uses \Sly\NotificationPusher\Collection\AbstractCollection
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
class DeviceCollection extends AbstractCollection
{
/**
* @param array $devices Devices
*/
public function __construct(array $devices = [])
{
$this->coll = new \ArrayIterator();
foreach ($devices as $device) {
$this->add($device);
}
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->coll;
}
/**
* @param DeviceInterface $device Device
*/
public function add(DeviceInterface $device)
{
$this->coll[$device->getToken()] = $device;
}
/**
* @return array
*/
public function getTokens()
{
$tokens = [];
foreach ($this as $device) {
$tokens[] = $device->getToken();
}
return array_unique(array_filter($tokens));
}
}

View File

@@ -1,43 +0,0 @@
<?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\Collection;
use Sly\NotificationPusher\Model\MessageInterface;
/**
* @uses \Sly\NotificationPusher\Collection\AbstractCollection
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
class MessageCollection extends AbstractCollection
{
public function __construct()
{
$this->coll = new \ArrayIterator();
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->coll;
}
/**
* @param MessageInterface $message Message
*/
public function add(MessageInterface $message)
{
$this->coll[] = $message;
}
}

View File

@@ -1,43 +0,0 @@
<?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\Collection;
use Sly\NotificationPusher\Model\PushInterface;
/**
* @uses \Sly\NotificationPusher\Collection\AbstractCollection
* @uses \IteratorAggregate
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushCollection extends AbstractCollection
{
public function __construct()
{
$this->coll = new \ArrayIterator();
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->coll;
}
/**
* @param PushInterface $push Push
*/
public function add(PushInterface $push)
{
$this->coll[] = $push;
}
}

View File

@@ -1,46 +0,0 @@
<?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
{
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

@@ -1,32 +0,0 @@
<?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\Console;
use Sly\NotificationPusher\Console\Command\PushCommand;
use Sly\NotificationPusher\NotificationPusher;
use Symfony\Component\Console\Application as BaseApplication;
/**
* @uses \Symfony\Component\Console\Application
* @author Cédric Dugat <cedric@dugat.me>
*/
class Application extends BaseApplication
{
public function __construct()
{
error_reporting(-1);
parent::__construct('NotificationPusher version', NotificationPusher::VERSION);
$this->add(new PushCommand());
}
}

View File

@@ -1,156 +0,0 @@
<?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\Console\Command;
use Doctrine\Common\Inflector\Inflector;
use Exception;
use Sly\NotificationPusher\Adapter\AdapterInterface;
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;
/**
* @uses \Symfony\Component\Console\Command\Command
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this
->setName('push')
->setDescription('Manual notification push')
->addArgument(
'adapter',
InputArgument::REQUIRED,
'Adapter (apns, gcm, specific class name, ...)'
)
->addArgument(
'token',
InputArgument::REQUIRED,
'Device Token or Registration ID'
)
->addArgument(
'message',
InputArgument::REQUIRED,
'Message'
)
->addOption(
'certificate',
null,
InputOption::VALUE_OPTIONAL,
'Certificate path (for APNS adapter)'
)
->addOption(
'api-key',
null,
InputOption::VALUE_OPTIONAL,
'API key (for GCM adapter)'
)
->addOption(
'env',
PushManager::ENVIRONMENT_DEV,
InputOption::VALUE_OPTIONAL,
sprintf(
'Environment (%s, %s)',
PushManager::ENVIRONMENT_DEV,
PushManager::ENVIRONMENT_PROD
)
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$adapter = $this->getReadyAdapter($input, $output);
$pushManager = new PushManager($input->getOption('env'));
$message = new Message($input->getArgument('message'));
$push = new Push($adapter, new Device($input->getArgument('token')), $message);
$pushManager->add($push);
$pushManager->push();
}
/**
* @param string $argument Given argument
*
* @return string
*
* @throws AdapterException When given adapter class doesn't exist
*/
private function getAdapterClassFromArgument($argument)
{
if (!class_exists($adapterClass = $argument) &&
!class_exists($adapterClass = '\\Sly\\NotificationPusher\\Adapter\\' . ucfirst($argument))) {
throw new AdapterException(
sprintf(
'Adapter class %s does not exist',
$adapterClass
)
);
}
return $adapterClass;
}
/**
* {@inheritdoc}
*
* @return AdapterInterface
*/
private function getReadyAdapter(InputInterface $input, OutputInterface $output)
{
$adapterClass = $this->getAdapterClassFromArgument($input->getArgument('adapter'));
try {
$adapter = new $adapterClass();
} catch (Exception $e) {
$adapterData = [];
preg_match_all('/"(.*)"/i', $e->getMessage(), $matches);
foreach ($matches[1] as $match) {
$optionKey = str_replace('_', '-', Inflector::tableize($match));
$option = $input->getOption($optionKey);
if (!$option) {
throw new AdapterException(
sprintf(
'The option "%s" is needed by %s adapter',
$optionKey,
$adapterClass
)
);
}
$adapterData[$match] = $option;
}
$adapter = new $adapterClass($adapterData);
}
return $adapter;
}
}

View File

@@ -1,22 +0,0 @@
<?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\Exception;
/**
* @uses \RuntimeException
* @uses \Sly\NotificationPusher\Exception\ExceptionInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class AdapterException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -1,19 +0,0 @@
<?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\Exception;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
interface ExceptionInterface
{
}

View File

@@ -1,22 +0,0 @@
<?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\Exception;
/**
* @uses \RuntimeException
* @uses \Sly\NotificationPusher\Exception\ExceptionInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class InvalidException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -1,22 +0,0 @@
<?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\Exception;
/**
* @uses \RuntimeException
* @uses \Sly\NotificationPusher\Exception\ExceptionInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -1,22 +0,0 @@
<?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\Exception;
/**
* @uses \RuntimeException
* @uses \Sly\NotificationPusher\Exception\ExceptionInterface
*
* @author Cédric Dugat <cedric@dugat.me>
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -1,158 +0,0 @@
<?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
* @author Oleg Abrazhaev <seyferseed@gmail.com>
*/
class GcmPushService extends AbstractPushService
{
/**
* @var string
*/
private $apiKey = '';
/**
* @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).
$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)) {
$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)) {
$tokens[] = $token;
}
}
return $tokens;
}
}

View File

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

View File

@@ -1,77 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class BaseOptionedModel
{
/**
* @var array
*/
protected $options = [];
/**
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* @param string $key Key
*
* @return boolean
*/
public function hasOption($key)
{
return array_key_exists($key, $this->options);
}
/**
* @param string $key Key
* @param mixed $default Default
*
* @return mixed
*/
public function getOption($key, $default = null)
{
return $this->hasOption($key) ? $this->options[$key] : $default;
}
/**
* @param array $options Options
*
* @return BaseOptionedModel
*/
public function setOptions($options)
{
$this->options = $options;
return $this;
}
/**
* @param string $key Key
* @param mixed $value Value
*
* @return mixed
*/
public function setOption($key, $value)
{
$this->options[$key] = $value;
return $value;
}
}

View File

@@ -1,77 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
abstract class BaseParameteredModel
{
/**
* @var array
*/
protected $parameters = [];
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @param string $key Key
*
* @return boolean
*/
public function hasParameter($key)
{
return array_key_exists($key, $this->parameters);
}
/**
* @param string $key Key
* @param mixed $default Default
*
* @return mixed
*/
public function getParameter($key, $default = null)
{
return $this->hasParameter($key) ? $this->parameters[$key] : $default;
}
/**
* @param array $parameters Parameters
*
* @return BaseParameteredModel
*/
public function setParameters($parameters)
{
$this->parameters = $parameters;
return $this;
}
/**
* @param string $key Key
* @param mixed $value Value
*
* @return mixed
*/
public function setParameter($key, $value)
{
$this->parameters[$key] = $value;
return $value;
}
}

View File

@@ -1,53 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
class Device extends BaseParameteredModel implements DeviceInterface
{
/**
* @var string
*/
private $token;
/**
* @param string $token Token
* @param array $parameters Parameters
*/
public function __construct($token, array $parameters = [])
{
$this->token = $token;
$this->parameters = $parameters;
}
/**
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* @param string $token Token
*
* @return DeviceInterface
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
}

View File

@@ -1,30 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
interface DeviceInterface
{
/**
* @return string
*/
public function getToken();
/**
* @param string $token Token
*
* @return DeviceInterface
*/
public function setToken($token);
}

View File

@@ -1,28 +0,0 @@
<?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

@@ -1,53 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
class Message extends BaseOptionedModel implements MessageInterface
{
/**
* @var string
*/
protected $text;
/**
* @param string $text Text
* @param array $options Options
*/
public function __construct($text, array $options = [])
{
$this->text = $text;
$this->options = $options;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @param string $text Text
*
* @return MessageInterface
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
}

View File

@@ -1,30 +0,0 @@
<?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\Model;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
interface MessageInterface
{
/**
* @return string
*/
public function getText();
/**
* @param string $text Text
*
* @return MessageInterface
*/
public function setText($text);
}

View File

@@ -1,242 +0,0 @@
<?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\Model;
use Sly\NotificationPusher\Adapter\AdapterInterface;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Collection\ResponseCollection;
use Sly\NotificationPusher\Exception\AdapterException;
/**
* @author Cédric Dugat <cedric@dugat.me>
*/
class Push extends BaseOptionedModel implements PushInterface
{
/**
* @var string
*/
private $status;
/**
* @var AdapterInterface
*/
private $adapter;
/**
* @var MessageInterface
*/
private $message;
/**
* @var DeviceCollection
*/
private $devices;
/**
* @var \DateTime
*/
private $pushedAt;
/**
* @var ResponseCollection
*/
private $responses;
/**
* @param AdapterInterface $adapter Adapter
* @param DeviceInterface|DeviceCollection $devices Device(s)
* @param 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.
*
* @throws AdapterException
*/
public function __construct(AdapterInterface $adapter, $devices, MessageInterface $message, array $options = [])
{
if ($devices instanceof DeviceInterface) {
$devices = new DeviceCollection([$devices]);
}
$this->adapter = $adapter;
$this->devices = $devices;
$this->message = $message;
$this->options = $options;
$this->status = self::STATUS_PENDING;
$this->checkDevicesTokens();
}
/**
* @throws AdapterException
*/
private function checkDevicesTokens()
{
$devices = $this->getDevices();
$adapter = $this->getAdapter();
foreach ($devices as $device) {
if (false === $adapter->supports($device->getToken())) {
throw new AdapterException(
sprintf(
'Adapter %s does not support %s token\'s device',
(string) $adapter,
$device->getToken()
)
);
}
}
}
/**
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* @param string $status Status
*
* @return PushInterface
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* @return boolean
*/
public function isPushed()
{
return (bool) (self::STATUS_PUSHED === $this->status);
}
/**
* @return PushInterface
*/
public function pushed()
{
$this->status = self::STATUS_PUSHED;
$this->pushedAt = new \DateTime();
return $this;
}
/**
* @return AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* @param AdapterInterface $adapter Adapter
*
* @return PushInterface
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* @return MessageInterface
*/
public function getMessage()
{
return $this->message;
}
/**
* @param MessageInterface $message Message
*
* @return PushInterface
*/
public function setMessage(MessageInterface $message)
{
$this->message = $message;
return $this;
}
/**
* @return DeviceCollection
*/
public function getDevices()
{
return $this->devices;
}
/**
* @param DeviceCollection $devices Devices
*
* @return PushInterface
*/
public function setDevices(DeviceCollection $devices)
{
$this->devices = $devices;
$this->checkDevicesTokens();
return $this;
}
/**
* @return ResponseCollection
*/
public function getResponses()
{
if (!$this->responses)
$this->responses = new ResponseCollection();
return $this->responses;
}
/**
* @param DeviceInterface $device
* @param mixed $response
*/
public function addResponse(DeviceInterface $device, $response)
{
$this->getResponses()->add($device->getToken(), $response);
}
/**
* @return \DateTime
*/
public function getPushedAt()
{
return $this->pushedAt;
}
/**
* @param \DateTime $pushedAt PushedAt
*
* @return PushInterface
*/
public function setPushedAt(\DateTime $pushedAt)
{
$this->pushedAt = $pushedAt;
return $this;
}
}

View File

@@ -1,107 +0,0 @@
<?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\Model;
use DateTime;
use Sly\NotificationPusher\Adapter\AdapterInterface;
use Sly\NotificationPusher\Collection\DeviceCollection;
use Sly\NotificationPusher\Collection\ResponseCollection;
interface PushInterface
{
/**
* Constants define available statuses
*/
const STATUS_PENDING = 'pending';
const STATUS_PUSHED = 'sent';
/**
* @return string
*/
public function getStatus();
/**
* @param string $status Status
*
* @return PushInterface
*/
public function setStatus($status);
/**
* @return boolean
*/
public function isPushed();
/**
* @return PushInterface
*/
public function pushed();
/**
* @return AdapterInterface
*/
public function getAdapter();
/**
* @param AdapterInterface $adapter Adapter
*
* @return PushInterface
*/
public function setAdapter(AdapterInterface $adapter);
/**
* @return MessageInterface
*/
public function getMessage();
/**
* @param MessageInterface $message Message
*
* @return PushInterface
*/
public function setMessage(MessageInterface $message);
/**
* @return DeviceCollection
*/
public function getDevices();
/**
* @param DeviceCollection $devices Devices
*
* @return PushInterface
*/
public function setDevices(DeviceCollection $devices);
/**
* @return ResponseCollection
*/
public function getResponses();
/**
* @param DeviceInterface $device
* @param mixed $response
*/
public function addResponse(DeviceInterface $device, $response);
/**
* @return DateTime
*/
public function getPushedAt();
/**
* @param DateTime $pushedAt PushedAt
*
* @return PushInterface
*/
public function setPushedAt(DateTime $pushedAt);
}

View File

@@ -1,92 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 09.08.17
* Time: 17:57
*/
namespace Sly\NotificationPusher\Model;
use Sly\NotificationPusher\Collection\PushCollection;
/**
* @author Oleg Abrazhaev <seyferseed@gmail.com>
*/
class Response implements ResponseInterface
{
/**
* @var array
*/
private $parsedResponses = [];
/**
* @var array
*/
private $originalResponses = [];
/**
* @var PushCollection
*/
private $pushCollection;
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 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

@@ -1,49 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: seyfer
* Date: 10.08.17
* Time: 10:30
*/
namespace Sly\NotificationPusher\Model;
use Sly\NotificationPusher\Collection\PushCollection;
/**
* @author Oleg Abrazhaev <seyferseed@gmail.com>
*/
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 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

@@ -1,17 +0,0 @@
<?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;
class NotificationPusher
{
const VERSION = '2.0';
}

View File

@@ -1,132 +0,0 @@
<?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;
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;
/**
* @uses \Sly\NotificationPusher\Collection\PushCollection
* @author Cédric Dugat <cedric@dugat.me>
*/
class PushManager
{
const ENVIRONMENT_DEV = 'dev';
const ENVIRONMENT_PROD = 'prod';
/**
* @var string
*/
private $environment;
/**
* @var PushCollection
*/
private $pushCollection;
/**
* @var ResponseInterface
*/
private $response;
/**
* @param string $environment Environment
*/
public function __construct($environment = self::ENVIRONMENT_DEV)
{
$this->environment = $environment;
$this->pushCollection = new PushCollection();
}
/**
* @param PushInterface $push Push
*/
public function add(PushInterface $push)
{
$this->pushCollection->add($push);
}
/**
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* @return PushCollection
*/
public function push()
{
/** @var Push $push */
foreach ($this->pushCollection as $push) {
$adapter = $push->getAdapter();
$adapter->setEnvironment($this->getEnvironment());
if ($adapter->push($push)) {
$push->pushed();
}
}
if ($this->pushCollection && !$this->pushCollection->isEmpty()) {
/** @var Push $push */
$push = $this->pushCollection->first();
$this->response = $push->getAdapter()->getResponse();
}
return $this->pushCollection;
}
/**
* @param AdapterInterface $adapter Adapter
*
* @return array
*
* @throws AdapterException When the adapter has no dedicated `getFeedback` method
*/
public function getFeedback(AdapterInterface $adapter)
{
if (!$adapter instanceof FeedbackAdapterInterface) {
throw new AdapterException(
sprintf(
'%s adapter has no dedicated "getFeedback" method',
(string) $adapter
)
);
}
$adapter->setEnvironment($this->getEnvironment());
return $adapter->getFeedback();
}
/**
* @return PushCollection
*/
public function getPushCollection()
{
return $this->pushCollection;
}
/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}