]> Raphaël G. Git Repositories - airbundle/blobdiff - Command/CalendarCommand.php
Add configure member function
[airbundle] / Command / CalendarCommand.php
index 25c45ca8e082f652ee05a0c8d6e268050fb9433a..2488f51d3840262a512c2c6a54abd0cdffa9c770 100644 (file)
@@ -10,11 +10,13 @@ use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Routing\RouterInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Routing\RouterInterface;
-use Symfony\Component\Translation\TranslatorInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
 use Twig\Extra\Markdown\DefaultMarkdown;
 
 use Rapsys\AirBundle\Entity\Session;
 
 use Twig\Extra\Markdown\DefaultMarkdown;
 
 use Rapsys\AirBundle\Entity\Session;
 
+use Rapsys\PackBundle\Util\SluggerUtil;
+
 class CalendarCommand extends Command {
        //Set failure constant
        const FAILURE = 1;
 class CalendarCommand extends Command {
        //Set failure constant
        const FAILURE = 1;
@@ -22,45 +24,67 @@ class CalendarCommand extends Command {
        ///Set success constant
        const SUCCESS = 0;
 
        ///Set success constant
        const SUCCESS = 0;
 
-       ///Config array
-       protected $config;
-
        /**
         * Doctrine instance
         *
         * @var ManagerRegistry
         */
        /**
         * Doctrine instance
         *
         * @var ManagerRegistry
         */
-       protected $doctrine;
+       protected ManagerRegistry $doctrine;
+
+       ///Router
+       protected RouterInterface $router;
+
+       ///Slugger
+       protected SluggerUtil $slugger;
+
+       ///Translator instance
+       protected TranslatorInterface $translator;
 
        ///Locale
        protected $locale;
 
 
        ///Locale
        protected $locale;
 
-       ///Translator instance
-       protected $translator;
+       ///Lifetime string
+       protected $lifetime;
+
+       ///Namespace string
+       protected $namespace;
+
+       ///Path string
+       protected $path;
 
        /**
 
        /**
-        * Inject doctrine, container and translator interface
+        * Creates new calendar command
         *
         *
-        * @param ContainerInterface $container The container instance
         * @param ManagerRegistry $doctrine The doctrine instance
         * @param RouterInterface $router The router instance
         * @param ManagerRegistry $doctrine The doctrine instance
         * @param RouterInterface $router The router instance
+        * @param SluggerUtil $slugger The slugger instance
         * @param TranslatorInterface $translator The translator instance
         * @param TranslatorInterface $translator The translator instance
+        * @param string $namespace The cache namespace
+        * @param int $lifetime The cache lifetime
+        * @param string $path The cache path
+        * @param string $locale The default locale
         */
         */
-    public function __construct(ContainerInterface $container, ManagerRegistry $doctrine, RouterInterface $router, TranslatorInterface $translator) {
+       public function __construct(ManagerRegistry $doctrine, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, string $namespace, int $lifetime, string $path, string $locale) {
                //Call parent constructor
                parent::__construct();
 
                //Call parent constructor
                parent::__construct();
 
-               //Retrieve config
-               $this->config = $container->getParameter($this->getAlias());
+               //Store doctrine
+               $this->doctrine = $doctrine;
 
 
-               //Retrieve locale
-               $this->locale = $container->getParameter('kernel.default_locale');
+               //Set lifetime
+               $this->lifetime = $lifetime;
 
 
-               //Store doctrine
-        $this->doctrine = $doctrine;
+               //Set namespace
+               $this->namespace = $namespace;
+
+               //Set path
+               $this->path = $path;
 
                //Store router
 
                //Store router
-        $this->router = $router;
+               $this->router = $router;
+
+               //Retrieve slugger
+               $this->slugger = $slugger;
 
                //Get router context
                $context = $this->router->getContext();
 
                //Get router context
                $context = $this->router->getContext();
@@ -88,7 +112,7 @@ class CalendarCommand extends Command {
        }
 
        ///Process the attribution
        }
 
        ///Process the attribution
-       protected function execute(InputInterface $input, OutputInterface $output) {
+       protected function execute(InputInterface $input, OutputInterface $output): int {
                //Compute period
                $period = new \DatePeriod(
                        //Start from last week
                //Compute period
                $period = new \DatePeriod(
                        //Start from last week
@@ -108,7 +132,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
                //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->namespace, $this->lifetime, $this->path);
 
                //Retrieve calendars
                $cacheCalendars = $cache->getItem('calendars');
 
                //Retrieve calendars
                $cacheCalendars = $cache->getItem('calendars');
@@ -147,10 +171,7 @@ class CalendarCommand extends Command {
                                //With expired token
                                if ($exp = $googleClient->isAccessTokenExpired()) {
                                        //Refresh token
                                //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'],
                                                //Add refreshed token
                                                $calendars[$clientId]['tokens'][$googleToken['access_token']] = [
                                                        'calendar' => $token['calendar'],
@@ -174,7 +195,15 @@ class CalendarCommand extends Command {
                                                        unset($calendars[$clientId]);
                                                }
 
                                                        unset($calendars[$clientId]);
                                                }
 
+                                               //Save calendars
+                                               $cacheCalendars->set($calendars);
+
+                                               //Save calendar
+                                               $cache->save($cacheCalendars);
+
                                                //Drop token and report
                                                //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
                                                echo 'Token '.$tokenId.' for calendar '.$token['calendar'].' has expired and is not refreshable'."\n";
 
                                                //Return failure
@@ -278,49 +307,53 @@ 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
                                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']
                                        ];
 
                                        //Init source
                                        $source = [
                                        $shared = [
                                                'gps' => $session['l_latitude'].','.$session['l_longitude']
                                        ];
 
                                        //Init source
                                        $source = [
-                                               'title' => $this->translator->trans('Session %id% by %pseudonym%', ['%id%' => $sessionId, '%pseudonym%' => $session['au_pseudonym']]).' '.$this->translator->trans('at '.$session['l_title']),
-                                               'url' => $this->router->generate('rapsys_air_session_view', ['id' => $sessionId], UrlGeneratorInterface::ABSOLUTE_URL)
+                                               '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']),
+                                               'url' => $this->router->generate('rapsys_air_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)
                                        ];
 
                                        ];
 
-                                       //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>';
+                                       //Init location
+                                       $description = 'Emplacement :'."\n".$this->translator->trans($session['l_description']);
+                                       $shared['location'] = $markdown->convert(strip_tags($session['l_description']));
+
+                                       //Add description
+                                       $description .= "\n\n".'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'];
 
                                        //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'];
                                        }
 
                                        //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'];
                                        }
 
                                        //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'];
                                        }
 
                                        //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'];
                                        }
 
                                        //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
                                        }
 
                                        //Locked session
@@ -349,7 +382,8 @@ class CalendarCommand extends Command {
                                                                //TODO: replace 'airlibre' with $this->config['calendar']['prefix'] when possible with prefix validating [a-v0-9]{5,}
                                                                //XXX: see https://developers.google.com/calendar/api/v3/reference/events/insert#id
                                                                'id' => $token['prefix'].$sessionId,
                                                                //TODO: replace 'airlibre' with $this->config['calendar']['prefix'] when possible with prefix validating [a-v0-9]{5,}
                                                                //XXX: see https://developers.google.com/calendar/api/v3/reference/events/insert#id
                                                                'id' => $token['prefix'].$sessionId,
-                                                               'summary' => $session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_short']),
+                                                               #'summary' => $session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_title']),
+                                                               'summary' => $source['title'],
                                                                #'description' => $markdown->convert(strip_tags($session['p_description'])),
                                                                'description' => $description,
                                                                'status' => empty($session['a_canceled'])?'confirmed':'cancelled',
                                                                #'description' => $markdown->convert(strip_tags($session['p_description'])),
                                                                'description' => $description,
                                                                'status' => empty($session['a_canceled'])?'confirmed':'cancelled',
@@ -388,10 +422,10 @@ class CalendarCommand extends Command {
                                                $event = $events[$sessionId];
 
                                                //With updated event
                                                $event = $events[$sessionId];
 
                                                //With updated event
-                                               #if ($session['updated'] >= (new \DateTime($event->getUpdated()))) {
-                                               {
+                                               if ($session['updated'] >= (new \DateTime($event->getUpdated()))) {
                                                        //Set summary
                                                        //Set summary
-                                                       $event->setSummary($session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_short']));
+                                                       #$event->setSummary($session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_title']));
+                                                       $event->setSummary($source['title']);
 
                                                        //Set description
                                                        $event->setDescription($description);
 
                                                        //Set description
                                                        $event->setDescription($description);
@@ -436,7 +470,7 @@ class CalendarCommand extends Command {
                                                        $end->setDateTime($session['stop']->format(\DateTime::ISO8601));
 
                                                        try {
                                                        $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) {
                                                                $updatedEvent = $googleCalendar->events->update($token['calendar'], $event->getId(), $event);
                                                        //Catch exception
                                                        } catch(\Google\Service\Exception $e) {