]> Raphaël G. Git Repositories - airbundle/blob - Repository/GoogleTokenRepository.php
Cleanup
[airbundle] / Repository / GoogleTokenRepository.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\Repository;
13
14 use Doctrine\ORM\AbstractQuery;
15 use Doctrine\ORM\Query\ResultSetMapping;
16
17 use Rapsys\AirBundle\Repository;
18
19 /**
20 * GoogleTokenRepository
21 */
22 class GoogleTokenRepository extends Repository {
23 /**
24 * Find google tokens indexed by id
25 *
26 * @return array The google tokens array
27 */
28 public function findAllIndexed(): array {
29 //Set the request
30 $req = <<<SQL
31 SELECT
32 t.id AS tid,
33 t.mail AS gmail,
34 t.user_id AS uid,
35 t.access,
36 t.refresh,
37 t.expired,
38 GROUP_CONCAT(c.id ORDER BY c.id SEPARATOR "\\n") AS cids,
39 GROUP_CONCAT(c.mail ORDER BY c.id SEPARATOR "\\n") AS cmails,
40 GROUP_CONCAT(c.summary ORDER BY c.id SEPARATOR "\\n") AS csummaries,
41 GROUP_CONCAT(IFNULL(c.synchronized, 'NULL') ORDER BY c.id SEPARATOR "\\n") AS csynchronizeds
42 FROM Rapsys\AirBundle\Entity\GoogleToken AS t
43 JOIN Rapsys\AirBundle\Entity\GoogleCalendar AS c ON (c.google_token_id = t.id)
44 GROUP BY t.id
45 ORDER BY NULL
46 SQL;
47
48 //Replace bundle entity name by table name
49 $req = str_replace($this->tableKeys, $this->tableValues, $req);
50
51 //Get result set mapping instance
52 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
53 $rsm = new ResultSetMapping();
54
55 //Declare all fields
56 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
57 //addScalarResult($sqlColName, $resColName, $type = 'string');
58 $rsm
59 ->addScalarResult('tid', 'tid', 'integer')
60 ->addScalarResult('gmail', 'gmail', 'string')
61 ->addScalarResult('uid', 'uid', 'integer')
62 ->addScalarResult('access', 'access', 'string')
63 ->addScalarResult('refresh', 'refresh', 'string')
64 ->addScalarResult('expired', 'expired', 'datetime')
65 ->addScalarResult('cids', 'cids', 'string')
66 ->addScalarResult('cmails', 'cmails', 'string')
67 ->addScalarResult('csummaries', 'csummaries', 'string')
68 ->addScalarResult('csynchronizeds', 'csynchronizeds', 'string')
69 ->addIndexByScalar('tid');
70
71 //Set result array
72 $result = [];
73
74 //Get tokens
75 $tokens = $this->_em
76 ->createNativeQuery($req, $rsm)
77 ->getArrayResult();
78
79 //Iterate on tokens
80 foreach($tokens as $tid => $token) {
81 //Set cids
82 $cids = explode("\n", $token['cids']);
83
84 //Set cmails
85 $cmails = explode("\n", $token['cmails']);
86
87 //Set csummaries
88 $csummaries = explode("\n", $token['csummaries']);
89
90 //Set csynchronizeds
91 $csynchronizeds = array_map(function($v){return new \DateTime($v);}, explode("\n", $token['csynchronizeds']));
92
93 //Set result
94 $result[$tid] = [
95 'id' => $tid,
96 'mail' => $token['gmail'],
97 'uid' => $token['uid'],
98 'access' => $token['access'],
99 'refresh' => $token['refresh'],
100 'expired' => $token['expired'],
101 'calendars' => []
102 ];
103
104 //Iterate on
105 foreach($cids as $k => $cid) {
106 $result[$tid]['calendars'][$cid] = [
107 'id' => $cid,
108 'mail' => $cmails[$k],
109 'summary' => $csummaries[$k],
110 'synchronized' => $csynchronizeds[$k]
111 ];
112 }
113 }
114
115 //Return result
116 return $result;
117 }
118 }