]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
ec5ffa36837cdd122c8ad386d7963c3381260685
[userbundle] / Repository / UserRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle 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\UserBundle\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15
16 use Rapsys\UserBundle\Repository;
17
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
20 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
21
22 /**
23 * UserRepository
24 */
25 class UserRepository extends Repository implements PasswordUpgraderInterface {
26 /**
27 * Find user count as int
28 *
29 * @return integer The keywords count
30 */
31 public function findCountAsInt(): int {
32 //Set the request
33 $req = <<<SQL
34 SELECT COUNT(u.id) AS count
35 FROM RapsysUserBundle:User AS u
36 SQL;
37
38 //Get result set mapping instance
39 $req = $this->replace($req);
40
41 //Get result set mapping instance
42 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
43 $rsm = new ResultSetMapping();
44
45 //Declare all fields
46 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
47 //addScalarResult($sqlColName, $resColName, $type = 'string');
48 $rsm->addScalarResult('count', 'count', 'integer');
49
50 //Get result
51 return $this->_em
52 ->createNativeQuery($req, $rsm)
53 ->getSingleScalarResult();
54 }
55
56 /**
57 * Find all users grouped by translated group
58 *
59 * @param integer $page The page
60 * @param integer $count The count
61 * @return array The user keyed by group and id
62 */
63 public function findIndexByGroupId(int $page, int $count): array {
64 //Set the request
65 $req = <<<SQL
66 SELECT
67 t.id,
68 t.mail,
69 t.forename,
70 t.surname,
71 t.g_id,
72 t.g_title
73 FROM (
74 SELECT
75 u.id,
76 u.mail,
77 u.forename,
78 u.surname,
79 IFNULL(g.id, :guestid) AS g_id,
80 IFNULL(g.title, :guesttitle) AS g_title
81 FROM RapsysUserBundle:User AS u
82 LEFT JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
83 LEFT JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
84 ORDER BY NULL
85 LIMIT 0, :limit
86 ) AS t
87 GROUP BY t.g_id, t.id
88 ORDER BY t.g_id DESC, t.id ASC
89 LIMIT :offset, :count
90 SQL;
91
92 //Replace bundle entity name by table name
93 $req = $this->replace($req);
94
95 //Get result set mapping instance
96 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
97 $rsm = new ResultSetMapping();
98
99 //Declare all fields
100 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
101 //addScalarResult($sqlColName, $resColName, $type = 'string');
102 $rsm->addScalarResult('id', 'id', 'integer')
103 ->addScalarResult('mail', 'mail', 'string')
104 ->addScalarResult('forename', 'forename', 'string')
105 ->addScalarResult('surname', 'surname', 'string')
106 ->addScalarResult('g_id', 'g_id', 'integer')
107 ->addScalarResult('g_title', 'g_title', 'string');
108
109 //Fetch result
110 $res = $this->_em
111 ->createNativeQuery($req, $rsm)
112 ->setParameter('offset', $page * $count)
113 ->setParameter('count', $count)
114 ->getResult();
115
116 //Init return
117 $ret = [];
118
119 //Process result
120 foreach($res as $data) {
121 //Get translated group
122 $group = $this->translator->trans($data['g_title'], [], $this->alias);
123
124 //Init group subarray
125 if (!isset($ret[$group])) {
126 $ret[$group] = [];
127 }
128
129 //Set data
130 $ret[$group][$data['id']] = [
131 'mail' => $data['mail'],
132 'forename' => $data['forename'],
133 'surname' => $data['surname'],
134 //Milonga Raphaël exception
135 'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)])
136 ];
137 }
138
139 //Send result
140 return $ret;
141 }
142
143 /**
144 * {@inheritdoc}
145 */
146 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
147 //Set new hashed password
148 $user->setPassword($hash);
149
150 //Flush data to database
151 $this->getEntityManager()->flush();
152 }
153 }