]> Raphaël G. Git Repositories - airbundle/blobdiff - Command/CalendarCommand.php
Update composer requires
[airbundle] / Command / CalendarCommand.php
index 84e096f895ec5a6fd16c82a82ae54f6603eabb20..f22fb091fce435767252c1dcd5a960a615db2dad 100644 (file)
@@ -108,7 +108,7 @@ class CalendarCommand extends Command {
                //Retrieve cache object
                //XXX: by default stored in /tmp/symfony-cache/@/W/3/6SEhFfeIW4UMDlAII+Dg
                //XXX: stored in %kernel.project_dir%/var/cache/airlibre/0/P/IA20X0K4dkMd9-+Ohp9Q
-               $cache = new FilesystemAdapter($this->config['cache']['namespace'], $this->config['cache']['lifetime'], $this->config['cache']['directory']);
+               $cache = new FilesystemAdapter($this->config['cache']['namespace'], $this->config['cache']['lifetime'], $this->config['path']['cache']);
 
                //Retrieve calendars
                $cacheCalendars = $cache->getItem('calendars');
@@ -122,25 +122,6 @@ class CalendarCommand extends Command {
                //Retrieve calendars
                $calendars = $cacheCalendars->get();
 
-               //XXX: calendars content
-               #var_export($calendars);
-               #$debug = [
-               #       '635317121880-usqucmne71jnmprl8br9khh2om4n8cmh.apps.googleusercontent.com' => [
-               #               'project' => 'calendar-317315',
-               #               'secret' => 'HRsKd4FIc9gxQHM4IoBWnlbD',
-               #               'redirect' => 'https://airlibre.eu/calendar/callback',
-               #               'tokens' => [
-               #                       'ya29.a0ARrdaM_cNpedJ-B3irC76_0-C7cfF-WmMh0smAs4m7cSvBChnniWr-e79q0IfAbh5DSG4FlHbCMvmaYb7xX4V45PujT2U4InZmpHfspiPv-QeR4XeZJp7bLXwnw7A4M0imeeYyQcwCW7GJ8O7dGLBQlBZAvt_Q' => [
-               #                               'calendar' => 'airlibre',
-               #                               'expire' => 3599,
-               #                               'scope' => 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events',
-               #                               'type' => 'Bearer',
-               #                               'created' => 1625417137,
-               #                       ],
-               #               ],
-               #       ],
-               #];
-
                //Check expired token
                foreach($calendars as $clientId => $client) {
                        //Get google client
@@ -163,10 +144,7 @@ class CalendarCommand extends Command {
                                //With expired token
                                if ($exp = $googleClient->isAccessTokenExpired()) {
                                        //Refresh token
-                                       if ($googleClient->getRefreshToken()) {
-                                               //Retrieve refreshed token
-                                               $googleToken = $googleClient->fetchAccessTokenWithRefreshToken($googleClient->getRefreshToken());
-
+                                       if (($refreshToken = $googleClient->getRefreshToken()) && ($googleToken = $googleClient->fetchAccessTokenWithRefreshToken($refreshToken)) && empty($googleToken['error'])) {
                                                //Add refreshed token
                                                $calendars[$clientId]['tokens'][$googleToken['access_token']] = [
                                                        'calendar' => $token['calendar'],
@@ -190,7 +168,15 @@ class CalendarCommand extends Command {
                                                        unset($calendars[$clientId]);
                                                }
 
+                                               //Save calendars
+                                               $cacheCalendars->set($calendars);
+
+                                               //Save calendar
+                                               $cache->save($cacheCalendars);
+
                                                //Drop token and report
+                                               //XXX: submit app to avoid expiration
+                                               //XXX: see https://console.cloud.google.com/apis/credentials/consent?project=calendar-317315
                                                echo 'Token '.$tokenId.' for calendar '.$token['calendar'].' has expired and is not refreshable'."\n";
 
                                                //Return failure
@@ -294,6 +280,7 @@ class CalendarCommand extends Command {
                                foreach($sessions as $sessionId => $session) {
                                        //Init shared properties
                                        //TODO: validate for constraints here ??? https://developers.google.com/calendar/api/guides/extended-properties
+                                       //TODO: drop shared as unused ???
                                        $shared = [
                                                'gps' => $session['l_latitude'].','.$session['l_longitude']
                                        ];
@@ -305,38 +292,37 @@ class CalendarCommand extends Command {
                                        ];
 
                                        //Init description
-                                       #$description = '<dl><dt>Description</dt><dd>'.$markdown->convert(strip_tags(str_replace(["\r", "\n\n"], ['', "\n"], $session['p_description']))).'</dd></dl>';
-                                       $description = '<dl><dt>Description</dt><dd>'.$markdown->convert(strip_tags($session['p_description'])).'</dd></dl>';
+                                       $description = 'Description :'."\n".strip_tags(preg_replace('!<a href="([^"]+)"(?: title="[^"]+")?'.'>([^<]+)</a>!', '\1', $markdown->convert(strip_tags($session['p_description']))));
+                                       $shared['description'] = $markdown->convert(strip_tags($session['p_description']));
 
                                        //Add class when available
                                        if (!empty($session['p_class'])) {
                                                $shared['class'] = $session['p_class'];
-                                               #$description .= '<dl><dt>Classe</dt><dd>'.$markdown->convert(strip_tags(str_replace(["\r", "\n\n"], ['', "\n"], $session['p_class']))).'</dd></dl>';
-                                               $description .= '<dl><dt>Classe</dt><dd><p>'.$session['p_class'].'</p></dd></dl>';
+                                               $description .= "\n\n".'Classe :'."\n".$session['p_class'];
                                        }
 
                                        //Add contact when available
                                        if (!empty($session['p_contact'])) {
                                                $shared['contact'] = $session['p_contact'];
-                                               $description .= '<dl><dt>Contacter</dt><dd><p>'.$session['p_contact'].'</p></dd></dl>';
+                                               $description .= "\n\n".'Contact :'."\n".$session['p_contact'];
                                        }
 
                                        //Add donate when available
                                        if (!empty($session['p_donate'])) {
                                                $shared['donate'] = $session['p_donate'];
-                                               $description .= '<dl><dt>Contribuer</dt><dd><p>'.$session['p_donate'].'</p></dd></dl>';
+                                               $description .= "\n\n".'Contribuer :'."\n".$session['p_donate'];
                                        }
 
                                        //Add link when available
                                        if (!empty($session['p_link'])) {
                                                $shared['link'] = $session['p_link'];
-                                               $description .= '<dl><dt>Site</dt><dd><p>'.$session['p_link'].'</p></dd></dl>';
+                                               $description .= "\n\n".'Site :'."\n".$session['p_link'];
                                        }
 
                                        //Add profile when available
                                        if (!empty($session['p_profile'])) {
                                                $shared['profile'] = $session['p_profile'];
-                                               $description .= '<dl><dt>Réseau social</dt><dd><p>'.$session['p_profile'].'</p></dd></dl>';
+                                               $description .= "\n\n".'Réseau social :'."\n".$session['p_profile'];
                                        }
 
                                        //Locked session
@@ -404,8 +390,7 @@ class CalendarCommand extends Command {
                                                $event = $events[$sessionId];
 
                                                //With updated event
-                                               #if ($session['updated'] >= (new \DateTime($event->getUpdated()))) {
-                                               {
+                                               if ($session['updated'] >= (new \DateTime($event->getUpdated()))) {
                                                        //Set summary
                                                        $event->setSummary($session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_short']));
 
@@ -452,7 +437,7 @@ class CalendarCommand extends Command {
                                                        $end->setDateTime($session['stop']->format(\DateTime::ISO8601));
 
                                                        try {
-                                                               //Insert the event
+                                                               //Update the event
                                                                $updatedEvent = $googleCalendar->events->update($token['calendar'], $event->getId(), $event);
                                                        //Catch exception
                                                        } catch(\Google\Service\Exception $e) {