X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/43e41ea4716c341aad3c983ddd0c4860100f7184..28ffa9276b13abd463ef5bf926d8af2f5169d4fb:/Command/CalendarCommand.php
diff --git a/Command/CalendarCommand.php b/Command/CalendarCommand.php
index 34b1005..dca096b 100644
--- a/Command/CalendarCommand.php
+++ b/Command/CalendarCommand.php
@@ -1,81 +1,52 @@
-
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
namespace Rapsys\AirBundle\Command;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
-use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
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\Translation\TranslatorInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extra\Markdown\DefaultMarkdown;
+use Rapsys\AirBundle\Command;
use Rapsys\AirBundle\Entity\Session;
-class CalendarCommand extends Command {
- //Set failure constant
- const FAILURE = 1;
-
- ///Set success constant
- const SUCCESS = 0;
-
- ///Config array
- protected $config;
-
- /**
- * Doctrine instance
- *
- * @var ManagerRegistry
- */
- protected $doctrine;
-
- ///Locale
- protected $locale;
-
- ///Translator instance
- protected $translator;
+use Rapsys\PackBundle\Util\SluggerUtil;
+class CalendarCommand extends Command {
/**
- * Inject doctrine, container and translator interface
+ * Creates new calendar command
*
- * @param ContainerInterface $container The container instance
* @param ManagerRegistry $doctrine The doctrine instance
+ * @param string $locale The default locale
* @param RouterInterface $router The router instance
+ * @param SluggerUtil $slugger The slugger 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
*/
- public function __construct(ContainerInterface $container, ManagerRegistry $doctrine, RouterInterface $router, TranslatorInterface $translator) {
+ 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) {
//Call parent constructor
- parent::__construct();
-
- //Retrieve config
- $this->config = $container->getParameter($this->getAlias());
-
- //Retrieve locale
- $this->locale = $container->getParameter('kernel.default_locale');
-
- //Store doctrine
- $this->doctrine = $doctrine;
-
- //Store router
- $this->router = $router;
-
- //Get router context
- $context = $this->router->getContext();
-
- //Set host
- $context->setHost('airlibre.eu');
-
- //Set scheme
- $context->setScheme('https');
-
- //Set the translator
- $this->translator = $translator;
+ parent::__construct($this->doctrine, $this->locale, $this->router, $this->slugger, $this->translator);
}
- ///Configure attribute command
+ /**
+ * Configure attribute command
+ */
protected function configure() {
//Configure the class
$this
@@ -87,8 +58,10 @@ class CalendarCommand extends Command {
->setHelp('This command synchronize sessions in google calendar');
}
- ///Process the attribution
- protected function execute(InputInterface $input, OutputInterface $output) {
+ /**
+ * Process the attribution
+ */
+ protected function execute(InputInterface $input, OutputInterface $output): int {
//Compute period
$period = new \DatePeriod(
//Start from last week
@@ -108,7 +81,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->namespace, $this->lifetime, $this->path);
//Retrieve calendars
$cacheCalendars = $cache->getItem('calendars');
@@ -147,10 +120,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'],
@@ -174,7 +144,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
@@ -278,18 +256,23 @@ 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']
];
//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('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)
];
- //Init description
- $description = 'Description :'."\n".strip_tags(preg_replace('!([^<]+)!', '\1', $markdown->convert(strip_tags($session['p_description']))));
+ //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('!([^<]+)!', '\1', $markdown->convert(strip_tags($session['p_description']))));
$shared['description'] = $markdown->convert(strip_tags($session['p_description']));
//Add class when available
@@ -348,7 +331,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,
- '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',
@@ -389,7 +373,8 @@ class CalendarCommand extends Command {
//With updated event
if ($session['updated'] >= (new \DateTime($event->getUpdated()))) {
//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);
@@ -434,7 +419,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) {
@@ -478,13 +463,4 @@ class CalendarCommand extends Command {
//Return success
return self::SUCCESS;
}
-
- /**
- * Return the bundle alias
- *
- * {@inheritdoc}
- */
- public function getAlias(): string {
- return 'rapsys_air';
- }
}