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
; 
  15 use Symfony\Component\Cache\Adapter\FilesystemAdapter
; 
  16 use Symfony\Component\Console\Input\InputInterface
; 
  17 use Symfony\Component\Console\Output\OutputInterface
; 
  18 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  19 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  20 use Symfony\Component\Routing\RouterInterface
; 
  21 use Symfony\Contracts\Translation\TranslatorInterface
; 
  22 use Twig\Extra\Markdown\DefaultMarkdown
; 
  24 use Rapsys\AirBundle\Command
; 
  25 use Rapsys\AirBundle\Entity\Session
; 
  27 use Rapsys\PackBundle\Util\SluggerUtil
; 
  29 class CalendarCommand 
extends Command 
{ 
  31          * Creates new calendar command 
  33          * @param ManagerRegistry $doctrine The doctrine instance 
  34          * @param string $locale The default locale 
  35          * @param RouterInterface $router The router instance 
  36          * @param SluggerUtil $slugger The slugger instance 
  37          * @param TranslatorInterface $translator The translator instance 
  38          * @param string $namespace The cache namespace 
  39          * @param int $lifetime The cache lifetime 
  40          * @param string $path The cache path 
  42         public function __construct(protected ManagerRegistry 
$doctrine, protected string $locale, protected RouterInterface 
$router, protected SluggerUtil 
$slugger, protected TranslatorInterface 
$translator, protected string $namespace, protected int $lifetime, protected string $path) { 
  43                 //Call parent constructor 
  44                 parent
::__construct($this->doctrine
, $this->locale
, $this->router
, $this->slugger
, $this->translator
); 
  48          * Configure attribute command 
  50         protected function configure() { 
  54                         ->setName('rapsysair:calendar') 
  55                         //Set description shown with bin/console list 
  56                         ->setDescription('Synchronize sessions in calendar') 
  57                         //Set description shown with bin/console --help airlibre:attribute 
  58                         ->setHelp('This command synchronize sessions in google calendar'); 
  62          * Process the attribution 
  64         protected function execute(InputInterface 
$input, OutputInterface 
$output): int { 
  66                 $period = new \
DatePeriod( 
  67                         //Start from last week 
  68                         new \
DateTime('-1 week'), 
  70                         new \
DateInterval('P1D'), 
  71                         //End with next 2 week 
  72                         new \
DateTime('+2 week') 
  75                 //Retrieve events to update 
  76                 $sessions = $this->doctrine
->getRepository(Session
::class)->fetchAllByDatePeriod($period, $this->locale
); 
  78                 //Markdown converted instance 
  79                 $markdown = new DefaultMarkdown
; 
  81                 //Retrieve cache object 
  82                 //XXX: by default stored in /tmp/symfony-cache/@/W/3/6SEhFfeIW4UMDlAII+Dg 
  83                 //XXX: stored in %kernel.project_dir%/var/cache/airlibre/0/P/IA20X0K4dkMd9-+Ohp9Q 
  84                 $cache = new FilesystemAdapter($this->namespace, $this->lifetime
, $this->path
); 
  87                 $cacheCalendars = $cache->getItem('calendars'); 
  90                 if (!$cacheCalendars->isHit()) { 
  96                 $calendars = $cacheCalendars->get(); 
  98                 //XXX: calendars content 
  99                 #var_export($calendars); 
 101                 //Check expired token 
 102                 foreach($calendars as $clientId => $client) { 
 104                         $googleClient = new \Google\
Client(['application_name' => $client['project'], 'client_id' => $clientId, 'client_secret' => $client['secret'], 'redirect_uri' => $client['redirect']]); 
 106                         //Iterate on each tokens 
 107                         foreach($client['tokens'] as $tokenId => $token) { 
 109                                 $googleClient->setAccessToken( 
 111                                                 'access_token' => $tokenId, 
 112                                                 'refresh_token' => $token['refresh'], 
 113                                                 'expires_in' => $token['expire'], 
 114                                                 'scope' => $token['scope'], 
 115                                                 'token_type' => $token['type'], 
 116                                                 'created' => $token['created'] 
 121                                 if ($exp = $googleClient->isAccessTokenExpired()) { 
 123                                         if (($refreshToken = $googleClient->getRefreshToken()) && ($googleToken = $googleClient->fetchAccessTokenWithRefreshToken($refreshToken)) && empty($googleToken['error'])) { 
 124                                                 //Add refreshed token 
 125                                                 $calendars[$clientId]['tokens'][$googleToken['access_token']] = [ 
 126                                                         'calendar' => $token['calendar'], 
 127                                                         'prefix' => $token['prefix'], 
 128                                                         'refresh' => $googleToken['refresh_token'], 
 129                                                         'expire' => $googleToken['expires_in'], 
 130                                                         'scope' => $googleToken['scope'], 
 131                                                         'type' => $googleToken['token_type'], 
 132                                                         'created' => $googleToken['created'] 
 136                                                 unset($calendars[$clientId]['tokens'][$tokenId]); 
 139                                                 unset($calendars[$clientId]['tokens'][$tokenId]); 
 142                                                 if (empty($calendars[$clientId]['tokens'])) { 
 144                                                         unset($calendars[$clientId]); 
 148                                                 $cacheCalendars->set($calendars); 
 151                                                 $cache->save($cacheCalendars); 
 153                                                 //Drop token and report 
 154                                                 //XXX: submit app to avoid expiration 
 155                                                 //XXX: see https://console.cloud.google.com/apis/credentials/consent?project=calendar-317315 
 156                                                 echo 'Token '.$tokenId.' for calendar '.$token['calendar'].' has expired and is not refreshable'."\n"; 
 159                                                 //XXX: we want that mail and stop here 
 160                                                 return self
::FAILURE
; 
 167                 $cacheCalendars->set($calendars); 
 170                 $cache->save($cacheCalendars); 
 172                 //Iterate on each calendar client 
 173                 foreach($calendars as $clientId => $client) { 
 175                         $googleClient = new \Google\
Client(['application_name' => $client['project'], 'client_id' => $clientId, 'client_secret' => $client['secret'], 'redirect_uri' => $client['redirect']]); 
 177                         //Iterate on each tokens 
 178                         foreach($client['tokens'] as $tokenId => $token) { 
 180                                 $googleClient->setAccessToken( 
 182                                                 'access_token' => $tokenId, 
 183                                                 'refresh_token' => $token['refresh'], 
 184                                                 'expires_in' => $token['expire'], 
 185                                                 'scope' => $token['scope'], 
 186                                                 'token_type' => $token['type'], 
 187                                                 'created' => $token['created'] 
 192                                 if ($exp = $googleClient->isAccessTokenExpired()) { 
 193                                         //Last chance to skip this run 
 197                                 //Get google calendar 
 198                                 $googleCalendar = new \Google\Service\
Calendar($googleClient); 
 202                                         $calendar = $googleCalendar->calendars
->get($token['calendar']); 
 204                                 } catch(\Google\Service\Exception 
$e) { 
 206                                         //TODO: handle codes here https://developers.google.com/calendar/api/guides/errors 
 207                                         echo 'Exception '.$e->getCode().':'.$e->getMessage().' in '.$e->getFile().' +'.$e->getLine()."\n"; 
 208                                         echo $e->getTraceAsString()."\n"; 
 211                                         return self
::FAILURE
; 
 219                                         //XXX: show even deleted event to be able to update them 
 220                                         'showDeleted' => true, 
 221                                         //TODO: fetch events one day before and one day after to avoid triggering double insert duplicate key 409 errors :=) on google 
 222                                         'timeMin' => $period->getStartDate()->format(\DateTime
::ISO8601
), 
 223                                         'timeMax' => $period->getEndDate()->format(\DateTime
::ISO8601
) 
 224                                         /*, 'iCalUID' => 'airlibre/?????'*//*'orderBy' => 'startTime', */ 
 227                                 //Retrieve event collection 
 228                                 $googleEvents = $googleCalendar->events
->listEvents($token['calendar'], $filters); 
 230                                 //Iterate until reached end 
 232                                         //Iterate on each event 
 233                                         foreach ($googleEvents->getItems() as $event) { 
 235                                                 if (preg_match('/^'.$token['prefix'].'([0-9]+)$/', $id = $event->getId(), $matches)) { 
 236                                                         $events[$matches[1]] = $event; 
 237                                                 //XXX: 3rd party events with id not matching prefix are skipped 
 239                                                 #       echo 'Skipping '.$event->getId().':'.$event->getSummary()."\n";*/ 
 244                                         $pageToken = $googleEvents->getNextPageToken(); 
 248                                                 //Replace collection with next one 
 249                                                 $googleEvents = $service->events
->listEvents($token['calendar'], $filters+
['pageToken' => $pageToken]); 
 255                                 //Iterate on each session to sync 
 256                                 foreach($sessions as $sessionId => $session) { 
 257                                         //Init shared properties 
 258                                         //TODO: validate for constraints here ??? https://developers.google.com/calendar/api/guides/extended-properties 
 259                                         //TODO: drop shared as unused ??? 
 261                                                 'gps' => $session['l_latitude'].','.$session['l_longitude'] 
 266                                                 'title' => $this->translator
->trans('%dance% %id% by %pseudonym%', ['%id%' => $sessionId, '%dance%' => $this->translator
->trans($session['ad_name'].' '.lcfirst($session['ad_type'])), '%pseudonym%' => $session['au_pseudonym']]).' '.$this->translator
->trans('at '.$session['l_title']), 
 267                                                 'url' => $this->router
->generate('rapsysair_session_view', ['id' => $sessionId, 'location' => $this->slugger
->slug($this->translator
->trans($session['l_title'])), 'dance' => $this->slugger
->slug($this->translator
->trans($session['ad_name'].' '.lcfirst($session['ad_type']))), 'user' => $this->slugger
->slug($session['au_pseudonym'])], UrlGeneratorInterface
::ABSOLUTE_URL
) 
 271                                         $description = 'Emplacement :'."\n".$this->translator
->trans($session['l_description']); 
 272                                         $shared['location'] = $markdown->convert(strip_tags($session['l_description'])); 
 275                                         $description .= "\n\n".'Description :'."\n".strip_tags(preg_replace('!<a href="([^"]+)"(?: title="[^"]+")?'.'>([^<]+)</a>!', '\1', $markdown->convert(strip_tags($session['p_description'])))); 
 276                                         $shared['description'] = $markdown->convert(strip_tags($session['p_description'])); 
 278                                         //Add class when available 
 279                                         if (!empty($session['p_class'])) { 
 280                                                 $shared['class'] = $session['p_class']; 
 281                                                 $description .= "\n\n".'Classe :'."\n".$session['p_class']; 
 284                                         //Add contact when available 
 285                                         if (!empty($session['p_contact'])) { 
 286                                                 $shared['contact'] = $session['p_contact']; 
 287                                                 $description .= "\n\n".'Contact :'."\n".$session['p_contact']; 
 290                                         //Add donate when available 
 291                                         if (!empty($session['p_donate'])) { 
 292                                                 $shared['donate'] = $session['p_donate']; 
 293                                                 $description .= "\n\n".'Contribuer :'."\n".$session['p_donate']; 
 296                                         //Add link when available 
 297                                         if (!empty($session['p_link'])) { 
 298                                                 $shared['link'] = $session['p_link']; 
 299                                                 $description .= "\n\n".'Site :'."\n".$session['p_link']; 
 302                                         //Add profile when available 
 303                                         if (!empty($session['p_profile'])) { 
 304                                                 $shared['profile'] = $session['p_profile']; 
 305                                                 $description .= "\n\n".'Réseau social :'."\n".$session['p_profile']; 
 309                                         if (!empty($session['locked']) && $events[$sessionId]) { 
 311                                                 if (!empty($event = $events[$sessionId])) { 
 314                                                                 $googleCalendar->events
->delete($token['calendar'], $event->getId()); 
 316                                                         } catch(\Google\Service\Exception 
$e) { 
 318                                                                 //TODO: handle codes here https://developers.google.com/calendar/api/guides/errors 
 319                                                                 echo 'Exception '.$e->getCode().':'.$e->getMessage().' in '.$e->getFile().' +'.$e->getLine()."\n"; 
 320                                                                 echo $e->getTraceAsString()."\n"; 
 323                                                                 return self
::FAILURE
; 
 327                                         } elseif (empty($events[$sessionId])) { 
 329                                                 $event = new \Google\Service\Calendar\
Event( 
 331                                                                 //TODO: replace 'airlibre' with $this->config['calendar']['prefix'] when possible with prefix validating [a-v0-9]{5,} 
 332                                                                 //XXX: see https://developers.google.com/calendar/api/v3/reference/events/insert#id 
 333                                                                 'id' => $token['prefix'].$sessionId, 
 334                                                                 #'summary' => $session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_title']), 
 335                                                                 'summary' => $source['title'], 
 336                                                                 #'description' => $markdown->convert(strip_tags($session['p_description'])), 
 337                                                                 'description' => $description, 
 338                                                                 'status' => empty($session['a_canceled'])?'confirmed':'cancelled', 
 339                                                                 'location' => implode(' ', [$session['l_address'], $session['l_zipcode'], $session['l_city']]), 
 341                                                                 'extendedProperties' => [ 
 345                                                                 //TODO: attendees[] ? 
 347                                                                         'dateTime' => $session['start']->format(\DateTime
::ISO8601
) 
 350                                                                         'dateTime' => $session['stop']->format(\DateTime
::ISO8601
) 
 357                                                         $googleCalendar->events
->insert($token['calendar'], $event); 
 359                                                 } catch(\Google\Service\Exception 
$e) { 
 361                                                         //TODO: handle codes here https://developers.google.com/calendar/api/guides/errors 
 362                                                         echo 'Exception '.$e->getCode().':'.$e->getMessage().' in '.$e->getFile().' +'.$e->getLine()."\n"; 
 363                                                         echo $e->getTraceAsString()."\n"; 
 366                                                         return self
::FAILURE
; 
 371                                                 $event = $events[$sessionId]; 
 374                                                 if ($session['updated'] >= (new \
DateTime($event->getUpdated()))) { 
 376                                                         #$event->setSummary($session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_title'])); 
 377                                                         $event->setSummary($source['title']); 
 380                                                         $event->setDescription($description); 
 383                                                         $event->setStatus(empty($session['a_canceled'])?'confirmed':'cancelled'); 
 386                                                         $event->setLocation(implode(' ', [$session['l_address'], $session['l_zipcode'], $session['l_city']])); 
 389                                                         $eventSource = $event->getSource(); 
 391                                                         //Update source title 
 392                                                         $eventSource->setTitle($source['title']); 
 395                                                         $eventSource->setUrl($source['url']); 
 398                                                         #$event->setSource($source); 
 400                                                         //Get extended properties 
 401                                                         $extendedProperties = $event->getExtendedProperties(); 
 404                                                         $extendedProperties->setShared($shared); 
 407                                                         //TODO: attendees[] ? 
 410                                                         $start = $event->getStart(); 
 412                                                         //Update start datetime 
 413                                                         $start->setDateTime($session['start']->format(\DateTime
::ISO8601
)); 
 416                                                         $end = $event->getEnd(); 
 418                                                         //Update stop datetime 
 419                                                         $end->setDateTime($session['stop']->format(\DateTime
::ISO8601
)); 
 423                                                                 $updatedEvent = $googleCalendar->events
->update($token['calendar'], $event->getId(), $event); 
 425                                                         } catch(\Google\Service\Exception 
$e) { 
 427                                                                 //TODO: handle codes here https://developers.google.com/calendar/api/guides/errors 
 428                                                                 echo 'Exception '.$e->getCode().':'.$e->getMessage().' in '.$e->getFile().' +'.$e->getLine()."\n"; 
 429                                                                 echo $e->getTraceAsString()."\n"; 
 432                                                                 return self
::FAILURE
; 
 436                                                 //Drop from events array 
 437                                                 unset($events[$sessionId]); 
 441                                 //Remaining events to drop 
 442                                 foreach($events as $eventId => $event) { 
 443                                         //Non canceled events 
 444                                         if ($event->getStatus() == 'confirmed') { 
 447                                                         $googleCalendar->events
->delete($token['calendar'], $event->getId()); 
 449                                                 } catch(\Google\Service\Exception 
$e) { 
 451                                                         //TODO: handle codes here https://developers.google.com/calendar/api/guides/errors 
 452                                                         echo 'Exception '.$e->getCode().':'.$e->getMessage().' in '.$e->getFile().' +'.$e->getLine()."\n"; 
 453                                                         echo $e->getTraceAsString()."\n"; 
 456                                                         return self
::FAILURE
; 
 464                 return self
::SUCCESS
;