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