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