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
; 
  17 use Google\Service\Calendar
; 
  18 use Google\Service\Oauth2
; 
  20 use Symfony\Component\Cache\Adapter\FilesystemAdapter
; 
  21 use Symfony\Component\Console\Input\InputInterface
; 
  22 use Symfony\Component\Console\Output\OutputInterface
; 
  23 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  24 use Symfony\Component\Routing\RouterInterface
; 
  25 use Symfony\Contracts\Translation\TranslatorInterface
; 
  27 use Twig\Extra\Markdown\DefaultMarkdown
; 
  29 use Rapsys\AirBundle\Entity\Session
; 
  30 use Rapsys\AirBundle\Entity\GoogleCalendar
; 
  31 use Rapsys\AirBundle\Entity\GoogleToken
; 
  33 use Rapsys\PackBundle\Util\SluggerUtil
; 
  35 class Calendar2Command 
extends Command 
{ 
  39     protected static $defaultName = 'rapsysair:calendar2'; 
  42          * Set default description 
  44     protected static $defaultDescription = 'Synchronize sessions in users\' calendar'; 
  47          * Set google client scopes 
  49         private array $scopes = [ 
  50                 Calendar
::CALENDAR_EVENTS
, 
  52                 Oauth2
::USERINFO_EMAIL
 
  56          * Set google client instance 
  58         private Client 
$client; 
  61          * Set markdown instance 
  63         private DefaultMarkdown 
$markdown; 
  66          * Set date period instance 
  68         private \DatePeriod 
$period; 
  73          * @param string $project The google project 
  74          * @param string $client The google client 
  75          * @param string $secret The google secret 
  77         public function __construct(ManagerRegistry 
$doctrine, RouterInterface 
$router, SluggerUtil 
$slugger, TranslatorInterface 
$translator, string $locale, string $project, string $client, string $secret) { 
  78                 //Call parent constructor 
  79                 parent
::__construct($doctrine, $router, $slugger, $translator, $locale); 
  82                 $this->client 
= new Client( 
  84                                 'application_name' => $project, 
  85                                 'client_id' => $client, 
  86                                 'client_secret' => $secret, 
  87                                 'redirect_uri' => $this->router
->generate('rapsys_air_google_callback', [], UrlGeneratorInterface
::ABSOLUTE_URL
), 
  88                                 'scopes' => $this->scopes
, 
  89                                 'access_type' => 'offline', 
  90                                 #'login_hint' => $user->getMail(), 
  91                                 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token 
  92                                 #'approval_prompt' => 'force' 
  97                 //Set Markdown instance 
  98                 $this->markdown 
= new DefaultMarkdown
; 
 102          * Process the attribution 
 104         protected function execute(InputInterface 
$input, OutputInterface 
$output) { 
 105                 //Iterate on google tokens 
 106                 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) { 
 107                         //Iterate on google calendars 
 108                         foreach($calendars = $token['calendars'] as $cid => $calendar) { 
 110                                 $this->period 
= new \
DatePeriod( 
 111                                         //Start from last week 
 112                                         new \
DateTime('-1 week'), 
 113                                         //Iterate on each day 
 114                                         new \
DateInterval('P1D'), 
 115                                         //End with next 2 week 
 116                                         new \
DateTime('+2 week') 
 119                                 #$calendar['synchronized'] 
 122                                 //TODO: see if we may be smarter here ? 
 124                                 //TODO: load all calendar events here ? 
 126                                 //Iterate on sessions to update 
 127                                 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized'])) { 
 128                                         //TODO: insert/update/delete events here ? 
 131                                 //TODO: delete remaining events here ? 
 135                 //TODO: get user filter ? (users_subscriptions+users_dances) 
 137                 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances 
 146         GROUP_CONCAT(us.user_id) AS users 
 150                 GROUP_CONCAT(ud.dance_id) AS dances 
 159                         GROUP_CONCAT(c.id) AS cids, 
 160                         GROUP_CONCAT(c.mail) AS cmails, 
 161                         GROUP_CONCAT(c.summary) AS csummaries, 
 162                         GROUP_CONCAT(c.synchronized) AS csynchronizeds 
 163                 FROM google_tokens AS t 
 164                 JOIN google_calendars AS c ON (c.google_token_id = t.id) 
 169         LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id) 
 174 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id) 
 178                 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); 
 179                 //Iterate on google tokens 
 180                 foreach($tokens as $token) { 
 181                         //TODO: clear google client cache 
 182                         //TODO: set google token 
 183                         //Iterate on google calendars 
 184                         foreach($calendars as $calendar) { 
 185                                 //Fetch sessions to sync 
 186                                 $sessions = $this->doctrine
->getRepository(Session
::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); 
 191                 return self
::SUCCESS
;