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/google-api-client / EventListener / GoogleApiNotificationsListener.php
Size: Mime:
<?php
/**
 * Created by PhpStorm.
 * User: eduarddezacastellano
 * Date: 02/10/2018
 * Time: 14:21
 */

namespace DigitalAscetic\GoogleApiClientBundle\EventListener;

use DigitalAscetic\GoogleApiClientBundle\Event\GoogleApiNotificationsEvent;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleNotificationsService;
use DigitalAscetic\GoogleApiClientBundle\Service\IGoogleApiService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class GoogleApiNotificationsListener implements EventSubscriberInterface
{

    const SERVICE_NAME = 'ascetic_google_api_client.listener.notifications';

    /** @var GoogleNotificationsService $notificationsService */
    private $notificationsService;

    /** @var IGoogleApiService[] */
    private $googleApiServices;

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


    /**
     * GoogleApiNotificationsListener constructor.
     * @param GoogleNotificationsService $notificationsService
     * @param $configGoogleApiServices
     * @param EntityManagerInterface $em
     */
    public function __construct(GoogleNotificationsService $notificationsService,
                                $configGoogleApiServices,
                                EntityManagerInterface $em)
    {
        $this->notificationsService = $notificationsService;
        $this->googleApiServices = $configGoogleApiServices;
        $this->em = $em;
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return array(
            GoogleApiNotificationsEvent::EVENT_NAME => 'googleApiNotifications'
        );
    }

    public function googleApiNotifications(GoogleApiNotificationsEvent $event)
    {
        switch ($event->getType()) {
            case GoogleApiNotificationsEvent::TYPE_START:
                $this->handleStartWatching($event);
                break;

            case GoogleApiNotificationsEvent::TYPE_NOTIFIED:
                $this->handleGetNotifyFromChannel($event);
                break;

            case GoogleApiNotificationsEvent::TYPE_RENEW:
                $this->handleRenewNotificationsChannels($event);
                break;

            case GoogleApiNotificationsEvent::TYPE_STOP:
                $this->handleStopWatching($event);
                break;
        }
    }

    private function handleStartWatching(GoogleApiNotificationsEvent $event)
    {
        if ($event->getGoogleApiUser()) {
            /** @var IGoogleApiService $googleApiService */
            foreach ($this->googleApiServices as $googleApiService) {
                $googleApiService->watchResource($event->getGoogleApiUser(), $event->getHost());
            }
        }
    }

    private function handleStopWatching(GoogleApiNotificationsEvent $event)
    {
        if ($event->getGoogleApiUser()) {
            /** @var IGoogleApiService $googleApiService */
            foreach ($this->googleApiServices as $googleApiService) {
                $googleApiService->stopWatchingResources($event->getGoogleApiUser(), $event->getHost());
            }
        }
    }

    private function handleGetNotifyFromChannel(GoogleApiNotificationsEvent $event)
    {
        if ($event->getChannelId()) {
            $channel = $this->notificationsService->getChannelByChannelId($event->getChannelId());

            if (isset($channel)) {
                $channel->setLastNotificationHandledDate(new \DateTime());
                $channel->setHost($event->getHost());
                $this->em->persist($channel);
                $this->em->flush();
            }
        }
    }

    private function handleRenewNotificationsChannels(GoogleApiNotificationsEvent $event)
    {
        if ($event->getGoogleApiUser()) {
            /** @var IGoogleApiService $googleApiService */
            foreach ($this->googleApiServices as $googleApiService) {
                $googleApiService->renewWatchChannels($event->getGoogleApiUser(), $event->getHost());
            }
        }
    }
}