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