]> Raphaël G. Git Repositories - airbundle/blob - Command/Calendar2Command.php
Replace dropped site.title with title.site
[airbundle] / Command / Calendar2Command.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Command;
13
14 use Doctrine\Persistence\ManagerRegistry;
15
16 use Google\Client;
17 use Google\Service\Calendar;
18 use Google\Service\Oauth2;
19
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;
26
27 use Twig\Extra\Markdown\DefaultMarkdown;
28
29 use Rapsys\AirBundle\Command;
30 use Rapsys\AirBundle\Entity\GoogleCalendar;
31 use Rapsys\AirBundle\Entity\GoogleToken;
32 use Rapsys\AirBundle\Entity\Session;
33
34 use Rapsys\PackBundle\Util\SluggerUtil;
35
36 /**
37 * {@inheritdoc}
38 *
39 * Synchronize sessions in users' calendar
40 */
41 class Calendar2Command extends Command {
42 /**
43 * Set description
44 *
45 * Shown with bin/console list
46 */
47 protected string $description = 'Synchronize sessions in users\' calendar';
48
49 /**
50 * Set help
51 *
52 * Shown with bin/console --help rapsysair:calendar2
53 */
54 protected string $help = 'This command synchronize sessions in users\' google calendar';
55
56 /**
57 * {@inheritdoc}
58 */
59 public function __construct(protected ManagerRegistry $doctrine, protected string $locale, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected Client $google, protected DefaultMarkdown $markdown) {
60 //Call parent constructor
61 parent::__construct($this->doctrine, $this->locale, $this->router, $this->slugger, $this->translator);
62
63 //Replace google client redirect uri
64 $this->google->setRedirectUri($this->router->generate($this->google->getRedirectUri(), [], UrlGeneratorInterface::ABSOLUTE_URL));
65 }
66
67 /**
68 * Process the attribution
69 */
70 protected function execute(InputInterface $input, OutputInterface $output): int {
71 //Set period
72 $period = new \DatePeriod(
73 //Start from last week
74 new \DateTime('-1 week'),
75 //Iterate on each day
76 new \DateInterval('P1D'),
77 //End with next 2 week
78 new \DateTime('+2 week')
79 );
80
81 //Iterate on google tokens
82 foreach($tokens = $this->doctrine->getRepository(GoogleToken::class)->findAllIndexed() as $tid => $token) {
83 //Iterate on google calendars
84 foreach($calendars = $token['calendars'] as $cid => $calendar) {
85 #$calendar['synchronized']
86 var_dump($token);
87
88 //TODO: see if we may be smarter here ?
89
90 //TODO: load all calendar events here ?
91
92 //Iterate on sessions to update
93 foreach($sessions = $this->doctrine->getRepository(Session::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) {
94 //TODO: insert/update/delete events here ?
95 }
96
97 //TODO: delete remaining events here ?
98 }
99 }
100
101 //TODO: get user filter ? (users_subscriptions+users_dances)
102
103 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances
104
105 exit;
106
107 //Return success
108 return self::SUCCESS;
109 }
110 }