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 / Controller / GoogleNotificationsController.php
Size: Mime:
<?php
/**
 * Created by PhpStorm.
 * User: eduarddezacastellano
 * Date: 05/09/2018
 * Time: 17:54
 */

namespace DigitalAscetic\GoogleApiClientBundle\Controller;


use DigitalAscetic\GoogleApiClientBundle\Event\GoogleApiNotificationsEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;


/**
 * Class GoogleNotificationsController
 * @package DigitalAscetic\GoogleApiClientBundle\Controller
 */
#[Route(path: '/google')]
class GoogleNotificationsController extends BaseWebServiceController
{
    const SERVICE_NAME = 'ascetic_google_api_client.calendar_controller';

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


    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }


    #[Route(path: '/notifications', methods: ['POST', 'HEAD'], name: 'da.google_api.calendar.notifications')]
    public function getNotificationsAction(Request $request)
    {
        $this->logger->info('GOOGLE NOTIFICATION RECEIVED');
        $channelId = $request->headers->get('X-Goog-Channel-ID');
        $resourceId = $request->headers->get('X-Goog-Resource-ID');

        /**
         * The new resource state, which triggered the notification. Possible values: sync, exists, or not_exists.
         * https://developers.google.com/calendar/v3/push
         */
        $resourceState = $request->headers->get('X-Goog-Resource-State');

        $this->logger->info($channelId);

        if ($resourceState !== 'sync') {
            $googleEvent = new GoogleApiNotificationsEvent(GoogleApiNotificationsEvent::TYPE_NOTIFIED, null, $request->getHost(), $channelId, $resourceId);
            $this->dispatcher->dispatch($googleEvent, GoogleApiNotificationsEvent::EVENT_NAME);
        }

        return $this->jsonResponse(['status' => 'ok']);
    }

}