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 / Command / GoogleNotificationsCommand.php
Size: Mime:
<?php


namespace DigitalAscetic\GoogleApiClientBundle\Command;


use DigitalAscetic\GoogleApiClientBundle\Entity\GoogleApiNotificationsChannel;
use DigitalAscetic\GoogleApiClientBundle\Service\GoogleCalendarService;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: 'ascetic:google:calendar:sync',
    description: 'Sync Google Calendar',
    hidden: false
)]
class GoogleNotificationsCommand extends Command
{
    /** @var GoogleCalendarService */
    private $googleCalendarService;

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

    /**
     * GoogleNotificationsCommand constructor.
     * @param GoogleCalendarService $googleCalendarService
     * @param EntityManagerInterface $em
     */
    public function __construct(GoogleCalendarService $googleCalendarService, EntityManagerInterface $em)
    {
        parent::__construct();
        $this->googleCalendarService = $googleCalendarService;
        $this->em = $em;
    }

    protected function configure()
    {
        $this
            // the name of the command (the part after "bin/console")
            ->setName('ascetic:google:calendar:sync')
            ->addOption(
                'from',
                'fr',
                InputOption::VALUE_OPTIONAL,
                'Date value from we want to sync channel (format: d-m-Y; ex: 2019-01-01)'
            )
            // the short description shown while running "php bin/console list"
            ->setDescription('Sync Google Calendar');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /** @var QueryBuilder $qb */
        $qb = $this->em->getRepository(GoogleApiNotificationsChannel::class)->createQueryBuilder(GoogleApiNotificationsChannel::ALIAS);

        $fromOption = $input->getOption('from');

        if (isset($fromOption)) {
            $dateStartJob = \DateTime::createFromFormat('Y-m-d', $fromOption);

            $qb->andWhere(
                $qb->expr()->gt(
                    GoogleApiNotificationsChannel::ALIAS . '.lastNotificationHandledDate',
                    ':dateHold'
                )
            )->setParameter('dateHold', $dateStartJob, Types::DATE_MUTABLE);
        }

        $or = $qb->expr()->orX();

        $or->add(
            $qb->expr()->gt(
                GoogleApiNotificationsChannel::ALIAS . '.lastNotificationHandledDate',
                GoogleApiNotificationsChannel::ALIAS . '.lastSyncNotificationDate'
            )
        );
        $or->add(
            $qb->expr()->isNull(GoogleApiNotificationsChannel::ALIAS . '.lastSyncNotificationDate')
        );

        $qb->andWhere($or);

        $iterableResult = $qb->getQuery()->iterate();
        $batchProcess = 0;

        while (($row = $iterableResult->next()) !== false) {
            /** @var GoogleApiNotificationsChannel $channel */
            $channel = $row[0];

            $this->googleCalendarService->getNotifications($channel, $channel->getHost());
            $channel->setLastSyncNotificationDate(new \DateTime());
            $this->em->persist($channel);

            if ($batchProcess % 20 === 0) {
                $this->em->flush();
                $this->em->clear();

            }

            $batchProcess++;
        }

        $this->em->flush();

        return Command::SUCCESS;
    }
}