]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
Add alias
[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, 0) AS g_id,
80 IFNULL(g.title, :defaultgroup) 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_title', 'g_title', 'string');
107
108 //Fetch result
109 $res = $this->_em
110 ->createNativeQuery($req, $rsm)
111 ->setParameter('offset', $page * $count)
112 ->setParameter('count', $count)
113 ->getResult();
114
115 //Init return
116 $ret = [];
117
118 //Process result
119 foreach($res as $data) {
120 //Get translated group
121 $group = $this->translator->trans($data['g_title'], [], $this->alias);
122
123 //Init group subarray
124 if (!isset($ret[$group])) {
125 $ret[$group] = [];
126 }
127
128 //Set data
129 $ret[$group][$data['id']] = [
130 'mail' => $data['mail'],
131 'forename' => $data['forename'],
132 'surname' => $data['surname'],
133 //Milonga Raphaël exception
134 'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)])
135 ];
136 }
137
138 //Send result
139 return $ret;
140 }
141
142 /**
143 * {@inheritdoc}
144 */
145 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
146 //Set new hashed password
147 $user->setPassword($hash);
148
149 //Flush data to database
150 $this->getEntityManager()->flush();
151 }
152 }