Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
15 / 20
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserRepository
75.00% covered (warning)
75.00%
15 / 20
66.67% covered (warning)
66.67%
2 / 3
4.25
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 upgradePassword
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 findAdminGuestsWithMediaCount
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Repository;
4
5use App\Entity\User;
6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7use Doctrine\Persistence\ManagerRegistry;
8use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
9use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
10use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
11
12/**
13 * @extends ServiceEntityRepository<User>
14 *
15 * @method User|null find($id, $lockMode = null, $lockVersion = null)
16 * @method User|null findOneBy(array<string, mixed> $criteria, array<string, mixed> $orderBy = null)
17 * @method User[]    findAll()
18 * @method User[]    findBy(array<string, mixed> $criteria, array<string, mixed> $orderBy = null, $limit = null, $offset = null)
19 */
20class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
21{
22    public function __construct(ManagerRegistry $registry)
23    {
24        parent::__construct($registry, User::class);
25    }
26
27    /**
28     * Used to upgrade (rehash) the user's password automatically over time.
29     */
30    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
31    {
32        if (!$user instanceof User) {
33            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
34        }
35
36        $user->setPassword($newHashedPassword);
37        $this->getEntityManager()->persist($user);
38        $this->getEntityManager()->flush();
39    }
40
41    /**
42     * Récupère les invités paginés (admin = true, super_admin = false) avec le nombre de médias associés à chacun.
43     *
44     * @param int $limit  Nombre d'invités à récupérer
45     * @param int $offset Nombre d'invités à passer pour la pagination
46     *
47     * @return list<array{id: int, name: string, mediasCount: int}> Retourne un tableau d'invités composé d'id, nom et nombre de médias
48     */
49    public function findAdminGuestsWithMediaCount(int $limit, int $offset): array
50    {
51        return $this->createQueryBuilder('u')
52            ->select('u.id AS id', 'u.name AS name', 'COUNT(m.id) AS mediasCount')
53            ->leftJoin('u.medias', 'm')
54            ->andWhere('u.super_admin = :superAdmin')
55            ->andWhere('u.admin = :admin')
56            ->setParameter('superAdmin', false)
57            ->setParameter('admin', true)
58            ->groupBy('u.id')
59            ->orderBy('u.id', 'ASC')
60            ->setFirstResult($offset)
61            ->setMaxResults($limit)
62            ->getQuery()
63            ->getArrayResult()
64        ;
65    }
66
67    //    /**
68    //     * @return User[] Returns an array of User objects
69    //     */
70    //    public function findByExampleField($value): array
71    //    {
72    //        return $this->createQueryBuilder('u')
73    //            ->andWhere('u.exampleField = :val')
74    //            ->setParameter('val', $value)
75    //            ->orderBy('u.id', 'ASC')
76    //            ->setMaxResults(10)
77    //            ->getQuery()
78    //            ->getResult()
79    //        ;
80    //    }
81
82    //    public function findOneBySomeField($value): ?User
83    //    {
84    //        return $this->createQueryBuilder('u')
85    //            ->andWhere('u.exampleField = :val')
86    //            ->setParameter('val', $value)
87    //            ->getQuery()
88    //            ->getOneOrNullResult()
89    //        ;
90    //    }
91}