X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/d8950f7e2adfbca7f28c64f4e0637e00359f9539..36367b3d9770a29f2e516e40524c5d4eef8a9aca:/Command/Calendar2Command.php diff --git a/Command/Calendar2Command.php b/Command/Calendar2Command.php index c0a739f..0ce4da8 100644 --- a/Command/Calendar2Command.php +++ b/Command/Calendar2Command.php @@ -33,90 +33,55 @@ use Rapsys\AirBundle\Entity\Session; use Rapsys\PackBundle\Util\SluggerUtil; +/** + * {@inheritdoc} + * + * Synchronize sessions in users' calendar + */ class Calendar2Command extends Command { /** - * Set google client scopes - */ - private array $scopes = [ - Calendar::CALENDAR_EVENTS, - Calendar::CALENDAR, - Oauth2::USERINFO_EMAIL - ]; - - /** - * Set google client instance + * Set description + * + * Shown with bin/console list */ - private Client $client; + protected string $description = 'Synchronize sessions in users\' calendar'; /** - * Set markdown instance + * Set help + * + * Shown with bin/console --help rapsysair:calendar2 */ - private DefaultMarkdown $markdown; - - /** - * Set date period instance - */ - private \DatePeriod $period; + protected string $help = 'This command synchronize sessions in users\' google calendar'; /** * {@inheritdoc} */ - public function __construct(ManagerRegistry $doctrine, string $locale, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator) { + public function __construct(protected ManagerRegistry $doctrine, protected string $locale, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected Client $google, protected DefaultMarkdown $markdown) { //Call parent constructor - parent::__construct($doctrine, $locale, $router, $slugger, $translator); - - //Set google client - $this->client = new Client( - [ - 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'], - 'client_id' => $_ENV['RAPSYSAIR_GOOGLE_CLIENT'], - 'client_secret' => $_ENV['RAPSYSAIR_GOOGLE_SECRET'], - 'redirect_uri' => $this->router->generate('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL), - 'scopes' => $this->scopes, - 'access_type' => 'offline', - #'login_hint' => $user->getMail(), - //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token - #'approval_prompt' => 'force' - 'prompt' => 'consent' - ] - ); - - //Set Markdown instance - $this->markdown = new DefaultMarkdown; - } + parent::__construct($this->doctrine, $this->locale, $this->router, $this->slugger, $this->translator); - /** - * Configure attribute command - */ - protected function configure() { - //Configure the class - $this - //Set name - ->setName('rapsysair:calendar2') - //Set description shown with bin/console list - ->setDescription('Synchronize sessions in users\' calendar') - //Set description shown with bin/console --help airlibre:attribute - ->setHelp('This command synchronize sessions in users\' google calendar'); + //Replace google client redirect uri + $this->google->setRedirectUri($this->router->generate($this->google->getRedirectUri(), [], UrlGeneratorInterface::ABSOLUTE_URL)); } /** * Process the attribution */ protected function execute(InputInterface $input, OutputInterface $output): int { + //Set period + $period = new \DatePeriod( + //Start from last week + new \DateTime('-1 week'), + //Iterate on each day + new \DateInterval('P1D'), + //End with next 2 week + new \DateTime('+2 week') + ); + //Iterate on google tokens foreach($tokens = $this->doctrine->getRepository(GoogleToken::class)->findAllIndexed() as $tid => $token) { //Iterate on google calendars foreach($calendars = $token['calendars'] as $cid => $calendar) { - //Set period - $this->period = new \DatePeriod( - //Start from last week - new \DateTime('-1 week'), - //Iterate on each day - new \DateInterval('P1D'), - //End with next 2 week - new \DateTime('+2 week') - ); - #$calendar['synchronized'] var_dump($token); @@ -137,56 +102,7 @@ class Calendar2Command extends Command { //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances - #var_dump($tokens); exit; - - //Set sql request - $sql =<<doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); - //Iterate on google tokens - foreach($tokens as $token) { - //TODO: clear google client cache - //TODO: set google token - //Iterate on google calendars - foreach($calendars as $calendar) { - //Fetch sessions to sync - $sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); - } - } //Return success return self::SUCCESS;