Completed
Push — master ( 97ef90...b0c954 )
by Matt
04:54
created

WanderRepository::addWhereHasImages()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Wander;
6
use DateTime;
7
use DateTimeInterface;
8
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
use Doctrine\ORM\QueryBuilder;
10
use Doctrine\Persistence\ManagerRegistry;
11
12
/**
13
 * @method Wander|null find($id, $lockMode = null, $lockVersion = null)
14
 * @method Wander|null findOneBy(array $criteria, array $orderBy = null)
15
 * @method Wander[]    findAll()
16
 * @method Wander[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17
 */
18
class WanderRepository extends ServiceEntityRepository
19
{
20
    public function __construct(ManagerRegistry $registry)
21
    {
22
        parent::__construct($registry, Wander::class);
23
    }
24
25
    public function findAll()
26
    {
27
        return $this->findBy(array(), array('startTime' => 'DESC'));
28
    }
29
30
    public function standardQueryBuilder() {
31
        return $this->createQueryBuilder('w')
32
            ->orderBy('w.startTime', 'desc');
33
    }
34
35
    public function addWhereHasImages(QueryBuilder $qb, ?bool $hasImages = null)
36
    {
37
        if ($hasImages !== null) {
38
            return $qb->andWhere('w.images is ' . ($hasImages ? 'not' : '') . ' empty');
39
        }
40
        return $qb;
41
    }
42
43
    // TODO: Do we still use this? We might only be using findFirstWhereIncludesDate now
44
    public function findWhereIncludesDate(DateTimeInterface $target)
45
    {
46
        return $this->createQueryBuilder('w')
47
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
48
            ->setParameter('target', $target)
49
            ->orderBy('w.startTime')
50
            ->getQuery()
51
            ->getResult();
52
    }
53
54
    public function findFirstWhereIncludesDate(DateTimeInterface $target)
55
    {
56
        return $this->createQueryBuilder('w')
57
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
58
            ->setParameter('target', $target)
59
            ->orderBy('w.startTime')
60
            ->setMaxResults(1)
61
            ->getQuery()
62
            ->getOneOrNullResult();
63
    }
64
65
    public function findShortest()
66
    {
67
        return $this->createQueryBuilder('w')
68
            ->select('w.id, w.distance')
69
            ->orderBy('w.distance', 'asc')
70
            ->setMaxResults(1)
71
            ->getQuery()
72
            ->getOneOrNullResult();
73
    }
74
75
    public function findLongest()
76
    {
77
        return $this->createQueryBuilder('w')
78
            ->select('w.id, w.distance')
79
            ->orderBy('w.distance', 'desc')
80
            ->setMaxResults(1)
81
            ->getQuery()
82
            ->getOneOrNullResult();
83
    }
84
85
    public function findAverageDistance()
86
    {
87
        return $this->createQueryBuilder('w')
88
            ->select('AVG(w.distance)')
89
            ->getQuery()
90
            ->getSingleScalarResult() ?? 0;
91
    }
92
93
    // /**
94
    //  * @return Wander[] Returns an array of Wander objects
95
    //  */
96
    /*
97
    public function findByExampleField($value)
98
    {
99
        return $this->createQueryBuilder('w')
100
            ->andWhere('w.exampleField = :val')
101
            ->setParameter('val', $value)
102
            ->orderBy('w.id', 'ASC')
103
            ->setMaxResults(10)
104
            ->getQuery()
105
            ->getResult()
106
        ;
107
    }
108
    */
109
110
    /*
111
    public function findOneBySomeField($value): ?Wander
112
    {
113
        return $this->createQueryBuilder('w')
114
            ->andWhere('w.exampleField = :val')
115
            ->setParameter('val', $value)
116
            ->getQuery()
117
            ->getOneOrNullResult()
118
        ;
119
    }
120
    */
121
}
122