Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
UserRepository
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getByEmail
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getById
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 list
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Repository;
6
7use App\Entity\User;
8use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9use Doctrine\Persistence\ManagerRegistry;
10
11/**
12 * @extends ServiceEntityRepository<User>
13 */
14class UserRepository extends ServiceEntityRepository implements UserRepositoryInterface
15{
16    public function __construct(ManagerRegistry $registry)
17    {
18        parent::__construct($registry, User::class);
19    }
20
21    public function save(User $entity, bool $flush = false): void
22    {
23        $this->getEntityManager()->persist($entity);
24
25        if ($flush) {
26            $this->getEntityManager()->flush();
27        }
28    }
29
30    public function getByEmail(string $email): ?User
31    {
32        return $this->findOneBy(['email' => $email]);
33    }
34
35    public function getById(int $id): ?User
36    {
37        return $this->find($id);
38    }
39
40    public function list(?string $nameCriteria = null, ?string $emailCriteria = null): array
41    {
42        $queryBuilder = $this->createQueryBuilder('u')->orderBy('u.name', 'ASC');
43
44        if (!empty($nameCriteria)) {
45            $queryBuilder
46                ->andWhere($queryBuilder->expr()->like('u.name', ':name'))
47                ->setParameter('name', '%' . $nameCriteria . '%')
48            ;
49        }
50        if (!empty($emailCriteria)) {
51            $queryBuilder
52                ->andWhere($queryBuilder->expr()->like('u.email', ':email'))
53                ->setParameter('email', '%' . $emailCriteria . '%')
54            ;
55        }
56
57        return $queryBuilder->getQuery()->getResult();
58    }
59}