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