]> Raphaël G. Git Repositories - airbundle/blob - Command/Calendar2Command.php
257d2e313d4fe8a30554dbcef41eff6bfe9eac0a
[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 * Set markdown instance
58 */
59 #private DefaultMarkdown $markdown;
60
61 /**
62 * Set date period instance
63 */
64 #private \DatePeriod $period;
65
66 /**
67 * {@inheritdoc}
68 */
69 public function __construct(protected ManagerRegistry $doctrine, protected string $locale, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected Client $google, protected DefaultMarkdown $markdown) {
70 //Call parent constructor
71 parent::__construct($this->doctrine, $this->locale, $this->router, $this->slugger, $this->translator);
72
73 //Replace google client redirect uri
74 $this->google->setRedirectUri($this->router->generate($this->google->getRedirectUri(), [], UrlGeneratorInterface::ABSOLUTE_URL));
75
76 /*
77 //Set google client
78 $this->client = new Client(
79 [
80 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
81 'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
82 'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
83 'redirect_uri' => $this->router->generate('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
84 'scopes' => $this->scopes,
85 'access_type' => 'offline',
86 #'login_hint' => $user->getMail(),
87 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
88 #'approval_prompt' => 'force'
89 'prompt' => 'consent'
90 ]
91 );
92
93 //Set Markdown instance
94 $this->markdown = new DefaultMarkdown;*/
95 }
96
97 /**
98 * Configure attribute command
99 */
100 /*protected function configure() {
101 //Configure the class
102 $this
103 //Set name
104 ->setName('rapsysair:calendar2')
105 //Set description shown with bin/console list
106 ->setDescription('Synchronize sessions in users\' calendar')
107 //Set description shown with bin/console --help airlibre:attribute
108 ->setHelp('This command synchronize sessions in users\' google calendar');
109 }*/
110
111 /**
112 * Process the attribution
113 */
114 protected function execute(InputInterface $input, OutputInterface $output): int {
115 //Set period
116 $period = new \DatePeriod(
117 //Start from last week
118 new \DateTime('-1 week'),
119 //Iterate on each day
120 new \DateInterval('P1D'),
121 //End with next 2 week
122 new \DateTime('+2 week')
123 );
124
125 //Iterate on google tokens
126 foreach($tokens = $this->doctrine->getRepository(GoogleToken::class)->findAllIndexed() as $tid => $token) {
127 //Iterate on google calendars
128 foreach($calendars = $token['calendars'] as $cid => $calendar) {
129 #$calendar['synchronized']
130 var_dump($token);
131
132 //TODO: see if we may be smarter here ?
133
134 //TODO: load all calendar events here ?
135
136 //Iterate on sessions to update
137 foreach($sessions = $this->doctrine->getRepository(Session::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) {
138 //TODO: insert/update/delete events here ?
139 }
140
141 //TODO: delete remaining events here ?
142 }
143 }
144
145 //TODO: get user filter ? (users_subscriptions+users_dances)
146
147 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances
148
149 #var_dump($tokens);
150 exit;
151
152 //Set sql request
153 $sql =<<<SQL
154 SELECT
155 b.*,
156 GROUP_CONCAT(us.user_id) AS users
157 FROM (
158 SELECT
159 a.*,
160 GROUP_CONCAT(ud.dance_id) AS dances
161 FROM (
162 SELECT
163 t.id AS tid,
164 t.mail AS gmail,
165 t.user_id,
166 t.access,
167 t.refresh,
168 t.expired,
169 GROUP_CONCAT(c.id) AS cids,
170 GROUP_CONCAT(c.mail) AS cmails,
171 GROUP_CONCAT(c.summary) AS csummaries,
172 GROUP_CONCAT(c.synchronized) AS csynchronizeds
173 FROM google_tokens AS t
174 JOIN google_calendars AS c ON (c.google_token_id = t.id)
175 GROUP BY t.id
176 ORDER BY NULL
177 LIMIT 100000
178 ) AS a
179 LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id)
180 GROUP BY a.tid
181 ORDER BY NULL
182 LIMIT 100000
183 ) AS b
184 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id)
185 GROUP BY b.tid
186 ORDER BY NULL
187 SQL;
188 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
189 //Iterate on google tokens
190 foreach($tokens as $token) {
191 //TODO: clear google client cache
192 //TODO: set google token
193 //Iterate on google calendars
194 foreach($calendars as $calendar) {
195 //Fetch sessions to sync
196 $sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
197 }
198 }
199
200 //Return success
201 return self::SUCCESS;
202 }
203 }