<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\PushNotification;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
class PushNotificationSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'push_notification.post_init' => ['onPushNotificationInit', 0],
'push_notification.post_create' => ['onPushNotificationCreate', 0],
];
}
public function onPushNotificationInit(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof PushNotification) {
$channel = $this->getKernel()->getContainer()->getParameter('pusher_channel');
$resource->setChannel($channel);
}
}
public function onPushNotificationCreate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof PushNotification) {
$this->getPusherHelper()->publishToInterests(
[$resource->getChannel()],
$resource->getTitle(),
$resource->getContent()
);
}
}
}