]> Raphaël G. Git Repositories - userbundle/blob - Repository/UserRepository.php
9f45b95ad3fd70f82121265513d3aaa1e610929d
[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 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
17 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
18
19 use Rapsys\UserBundle\Repository;
20
21 /**
22 * UserRepository
23 */
24 class UserRepository extends Repository implements PasswordUpgraderInterface {
25 /**
26 * Find user count as int
27 *
28 * @return integer The keywords count
29 */
30 public function findCountAsInt(): int {
31 //Set the request
32 $req = <<<SQL
33 SELECT COUNT(u.id) AS count
34 FROM RapsysUserBundle:User AS u
35 SQL;
36
37 //Get result set mapping instance
38 $req = $this->replace($req);
39
40 //Get result set mapping instance
41 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
42 $rsm = new ResultSetMapping();
43
44 //Declare all fields
45 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
46 //addScalarResult($sqlColName, $resColName, $type = 'string');
47 $rsm->addScalarResult('count', 'count', 'integer');
48
49 //Get result
50 return $this->_em
51 ->createNativeQuery($req, $rsm)
52 ->getSingleScalarResult();
53 }
54
55 /**
56 * Find all users as array
57 *
58 * @param integer $page The page
59 * @param integer $count The count
60 * @return array The users sorted by id
61 */
62 public function findAllAsArray(int $page, int $count): array {
63 //Set the request
64 $req = <<<SQL
65 SELECT
66 u.id,
67 u.mail,
68 u.forename,
69 u.surname,
70 CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
71 c.id AS c_id,
72 c.title AS c_title,
73 GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
74 GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
75 FROM RapsysUserBundle:User AS u
76 JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
77 JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
78 JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
79 GROUP BY u.id
80 ORDER BY u.id ASC
81 LIMIT :offset, :count
82 SQL;
83
84 //Replace bundle entity name by table name
85 $req = $this->replace($req);
86
87 //Get result set mapping instance
88 //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
89 $rsm = new ResultSetMapping();
90
91 //Declare all fields
92 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
93 //addScalarResult($sqlColName, $resColName, $type = 'string');
94 $rsm->addScalarResult('id', 'id', 'integer')
95 ->addScalarResult('mail', 'mail', 'string')
96 ->addScalarResult('forename', 'forename', 'string')
97 ->addScalarResult('surname', 'surname', 'string')
98 ->addScalarResult('pseudonym', 'pseudonym', 'string')
99 ->addScalarResult('c_id', 'c_id', 'integer')
100 ->addScalarResult('c_title', 'c_title', 'string')
101 //XXX: is a string because of \n separator
102 ->addScalarResult('g_ids', 'g_ids', 'string')
103 //XXX: is a string because of \n separator
104 ->addScalarResult('g_titles', 'g_titles', 'string');
105
106 //Fetch result
107 $res = $this->_em
108 ->createNativeQuery($req, $rsm)
109 ->setParameter('offset', $page * $count)
110 ->setParameter('count', $count)
111 ->getResult();
112
113 //Init return
114 $ret = [];
115
116 //Process result
117 foreach($res as $data) {
118 //Set data
119 $ret[$data['id']] = [
120 'mail' => $data['mail'],
121 'forename' => $data['forename'],
122 'surname' => $data['surname'],
123 'pseudonym' => $data['pseudonym'],
124 'groups' => [],
125 'slug' => $this->slugger->slug($data['pseudonym']),
126 'link' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)])
127 ];
128
129 //With groups
130 if (!empty($data['g_ids'])) {
131 //Set titles
132 $titles = explode("\n", $data['g_titles']);
133
134 //Iterate on each group
135 foreach(explode("\n", $data['g_ids']) as $k => $id) {
136 //Add group
137 $ret[$data['id']]['groups'][$id] = [
138 'title' => $group = $this->translator->trans($titles[$k]),
139 #'slug' => $this->slugger->slug($group)
140 #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
141 ];
142 }
143 }
144 }
145
146 //Send result
147 return $ret;
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $hash): void {
154 //Set new hashed password
155 $user->setPassword($hash);
156
157 //Flush data to database
158 $this->getEntityManager()->flush();
159 }
160 }