Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotesRepository
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 remove
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 findOneRandom
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Repository;
4
5use App\Entity\Quotes;
6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7use Doctrine\DBAL\Query\QueryBuilder;
8use Doctrine\ORM\NonUniqueResultException;
9use Doctrine\Persistence\ManagerRegistry;
10use Exception;
11
12/**
13 * @method Quotes|null find($id, $lockMode = null, $lockVersion = null)
14 * @method Quotes|null findOneBy(array $criteria, array $orderBy = null)
15 * @method Quotes[]    findAll()
16 * @method Quotes[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17 */
18class QuotesRepository extends ServiceEntityRepository
19{
20    public function __construct(ManagerRegistry $registry)
21    {
22        parent::__construct(registry: $registry, entityClass: Quotes::class);
23    }
24
25    public function add(Quotes $entity, bool $flush = true): void
26    {
27        $this->_em->persist(entity: $entity);
28        if ($flush) {
29            $this->_em->flush();
30        }
31    }
32
33    public function remove(Quotes $entity, bool $flush = true): void
34    {
35        $this->_em->remove(entity: $entity);
36        if ($flush) {
37            $this->_em->flush();
38        }
39    }
40
41    public function findOneRandom(): ?Quotes
42    {
43        try {
44            $idLimits = $this->createQueryBuilder(alias: 'q')
45                ->select('MIN(q.id)', 'MAX(q.id)')
46                ->getQuery()
47                ->getOneOrNullResult();
48        } catch (NonUniqueResultException) {
49            $idLimits = 0;
50        }
51
52        try {
53            $randomPossibleId = random_int(min: $idLimits[1], max: $idLimits[2]);
54        } catch(Exception) {
55            $randomPossibleId = 0; // return first, if any
56        }
57
58        try {
59            return $this->createQueryBuilder(alias: 'q')
60                ->where(predicates: 'q.id >= :random_id')
61                ->setParameter(key: 'random_id', value: $randomPossibleId)
62                ->setMaxResults(maxResults: 1)
63                ->getQuery()
64                ->getOneOrNullResult();
65        } catch (NonUniqueResultException) {
66            // max results is 1
67            return null;
68        }
69    }
70
71    // /**
72    //  * @return Quotes[] Returns an array of Quotes objects
73    //  */
74    /*
75    public function findByExampleField($value)
76    {
77        return $this->createQueryBuilder('q')
78            ->andWhere('q.exampleField = :val')
79            ->setParameter('val', $value)
80            ->orderBy('q.id', 'ASC')
81            ->setMaxResults(10)
82            ->getQuery()
83            ->getResult()
84        ;
85    }
86    */
87
88    /*
89    public function findOneBySomeField($value): ?Quotes
90    {
91        return $this->createQueryBuilder('q')
92            ->andWhere('q.exampleField = :val')
93            ->setParameter('val', $value)
94            ->getQuery()
95            ->getOneOrNullResult()
96        ;
97    }
98    */
99}