Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
digitalascetic/notifications / Service / NotificationService.php
Size: Mime:
<?php


namespace DigitalAscetic\NotificationsBundle\Service;


use DigitalAscetic\NotificationsBundle\Entity\Receiver;
use DigitalAscetic\NotificationsBundle\Entity\Notification;
use DigitalAscetic\NotificationsBundle\Entity\NotificationObject;
use DigitalAscetic\NotificationsBundle\Model\NotificationOptions;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class NotificationService
{
    const SERVICE_NAME = 'digital_ascetic.notification.service';

    const SENDER_SYSTEM = 0;

    /** @var EntityManagerInterface */
    private $em;

    /** @var EventDispatcherInterface */
    private $dispatcher;

    /** @var LoggerInterface */
    private $logger;

    /**
     * NotificationService constructor.
     * @param EntityManagerInterface $em
     * @param EventDispatcherInterface $dispatcher
     * @param LoggerInterface $logger
     */
    public function __construct(
        EntityManagerInterface $em,
        EventDispatcherInterface $dispatcher,
        LoggerInterface $logger
    ) {
        $this->em = $em;
        $this->dispatcher = $dispatcher;
        $this->logger = $logger;
    }

    /**
     * @param string $message
     * @param int $topic
     * @param Receiver $receiver
     * @param int|null $senderId
     * @param NotificationOptions|null $options
     * @return Notification
     */
    public function createNotification(
        string $message,
        int $topic,
        Receiver $receiver,
        int $senderId = null,
        NotificationOptions $options = null
    ) {
        if (!isset($senderId)) {
            $senderId = NotificationService::SENDER_SYSTEM;
        }

        $notObj = new NotificationObject($senderId, $message, $topic);

        if (isset($options)) {
            $notifiableEntity = $options->getNotifiableEntity();

            if (isset($notifiableEntity)) {
                $objType = $this->em->getMetadataFactory()->getMetadataFor(get_class($notifiableEntity))->getName();
                $objTypeId = $notifiableEntity->getId();

                $notObj->setEntityType($objType);
                $notObj->setEntityTypeId($objTypeId);
            }
        }

        $this->em->persist($notObj);
        $this->em->flush();

        $notification = new Notification($notObj, $receiver);

        if (isset($options)) {
            $type = $options->getType();
            if (isset($type)) {
                $notification->setType($type);
            }

            $status = $options->getStatus();
            if (isset($status)) {
                $notification->setStatus($status);
            }

            $data = $options->getData();
            if (isset($data)) {
                $notification->setData($data);
            }
        }

        $this->em->persist($notification);
        $this->em->flush();
        $this->dispatcher->dispatch(
            new NotificationEvent($notification),
            NotificationEvent::EVENT_NOTIFICATION_PERSISTED
        );

        return $notification;
    }

    public function getPagedNotifications(NotificationQueryFilter $filter)
    {
        return $filter->getPagedResults();
    }

    public function getNotifications(NotificationQueryFilter $filter)
    {
        return $filter->getResults();
    }

    public function markAsRead(int $receiverId, Notification $notification = null)
    {
        $sql = 'UPDATE notification SET status = "'.Notification::STATUS_READ.'" WHERE receiver_id = '.$receiverId;

        if (isset($notification)) {
            $sql .= ' AND id = '.$notification->getId();
        }
        $con = $this->em->getConnection();
        $con->executeStatement($sql);
    }

    /**
     * @param Notification $notification
     */
    public function updateNotification(Notification $notification)
    {
        $this->em->persist($notification);
        $this->em->flush();
        $this->dispatcher->dispatch(
            new NotificationEvent($notification),
            NotificationEvent::EVENT_NOTIFICATION_UPDATED
        );
    }
}