1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys AirBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\AirBundle\Command
; 
  14 use Doctrine\Persistence\ManagerRegistry
; 
  17 use Google\Service\Calendar
; 
  18 use Google\Service\Oauth2
; 
  20 use Symfony\Component\Cache\Adapter\FilesystemAdapter
; 
  21 use Symfony\Component\Console\Input\InputInterface
; 
  22 use Symfony\Component\Console\Output\OutputInterface
; 
  23 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  24 use Symfony\Component\Routing\RouterInterface
; 
  25 use Symfony\Contracts\Translation\TranslatorInterface
; 
  27 use Twig\Extra\Markdown\DefaultMarkdown
; 
  29 use Rapsys\AirBundle\Command
; 
  30 use Rapsys\AirBundle\Entity\GoogleCalendar
; 
  31 use Rapsys\AirBundle\Entity\GoogleToken
; 
  32 use Rapsys\AirBundle\Entity\Session
; 
  34 use Rapsys\PackBundle\Util\SluggerUtil
; 
  39  * Synchronize sessions in users' calendar 
  41 class Calendar2Command 
extends Command 
{ 
  45          * Shown with bin/console list 
  47         protected string $description = 'Synchronize sessions in users\' calendar'; 
  52          * Shown with bin/console --help rapsysair:calendar2 
  54         protected string $help = 'This command synchronize sessions in users\' google calendar'; 
  59         public function __construct(protected ManagerRegistry 
$doctrine, protected string $locale, protected RouterInterface 
$router, protected SluggerUtil 
$slugger, protected TranslatorInterface 
$translator, protected Client 
$google, protected DefaultMarkdown 
$markdown) { 
  60                 //Call parent constructor 
  61                 parent
::__construct($this->doctrine
, $this->locale
, $this->router
, $this->slugger
, $this->translator
); 
  63                 //Replace google client redirect uri 
  64                 $this->google
->setRedirectUri($this->router
->generate($this->google
->getRedirectUri(), [], UrlGeneratorInterface
::ABSOLUTE_URL
)); 
  68          * Process the attribution 
  70         protected function execute(InputInterface 
$input, OutputInterface 
$output): int { 
  72                 $period = new \
DatePeriod( 
  73                         //Start from last week 
  74                         new \
DateTime('-1 week'), 
  76                         new \
DateInterval('P1D'), 
  77                         //End with next 2 week 
  78                         new \
DateTime('+2 week') 
  81                 //Iterate on google tokens 
  82                 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) { 
  83                         //Iterate on google calendars 
  84                         foreach($calendars = $token['calendars'] as $cid => $calendar) { 
  85                                 #$calendar['synchronized'] 
  88                                 //TODO: see if we may be smarter here ? 
  90                                 //TODO: load all calendar events here ? 
  92                                 //Iterate on sessions to update 
  93                                 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) { 
  94                                         //TODO: insert/update/delete events here ? 
  97                                 //TODO: delete remaining events here ? 
 101                 //TODO: get user filter ? (users_subscriptions+users_dances) 
 103                 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances 
 108                 return self
::SUCCESS
;