Completed
Push — master ( 192df5...70e954 )
by Matt
04:15
created

WanderRepository::findPrev()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
    /**
44
     * Used by Elastica to transform results to model
45
     *
46
     * @param string $entityAlias
47
     * @return QueryBuilder
48
     */
49
    public function createSearchQueryBuilder(string $entityAlias): QueryBuilder {
50
        $qb = $this->createQueryBuilder($entityAlias);
51
        $qb->select($entityAlias, 'i')
52
            ->innerJoin($entityAlias.'.images', 'i');
53
        return $qb;
54
    }
55
56
    // TODO: Do we still use this? We might only be using findFirstWhereIncludesDate now
57
    public function findWhereIncludesDate(DateTimeInterface $target)
58
    {
59
        return $this->createQueryBuilder('w')
60
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
61
            ->setParameter('target', $target)
62
            ->orderBy('w.startTime')
63
            ->getQuery()
64
            ->getResult();
65
    }
66
67
    public function findFirstWhereIncludesDate(DateTimeInterface $target)
68
    {
69
        return $this->createQueryBuilder('w')
70
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
71
            ->setParameter('target', $target)
72
            ->orderBy('w.startTime')
73
            ->setMaxResults(1)
74
            ->getQuery()
75
            ->getOneOrNullResult();
76
    }
77
78
    public function findShortest()
79
    {
80
        return $this->createQueryBuilder('w')
81
            ->select('w.id, w.distance')
82
            ->orderBy('w.distance', 'asc')
83
            ->setMaxResults(1)
84
            ->getQuery()
85
            ->getOneOrNullResult();
86
    }
87
88
    public function findLongest()
89
    {
90
        return $this->createQueryBuilder('w')
91
            ->select('w.id, w.distance')
92
            ->orderBy('w.distance', 'desc')
93
            ->setMaxResults(1)
94
            ->getQuery()
95
            ->getOneOrNullResult();
96
    }
97
98
    public function findAverageDistance()
99
    {
100
        return $this->createQueryBuilder('w')
101
            ->select('AVG(w.distance)')
102
            ->getQuery()
103
            ->getSingleScalarResult() ?? 0;
104
    }
105
106
    public function findNext(Wander $wander): ?Wander
107
    {
108
        $qb = $this->createQueryBuilder('w');
109
        return $qb->add('where', $qb->expr()->gt('w.startTime', ':startTime'))
110
            ->setParameter('startTime', $wander->getStartTime())
111
            ->addOrderBy('w.startTime')
112
            ->setMaxResults(1)
113
            ->getQuery()
114
            ->getOneOrNullResult();
115
    }
116
117
    public function findPrev(Wander $wander): ?Wander
118
    {
119
        $qb = $this->createQueryBuilder('w');
120
        return $qb->add('where', $qb->expr()->lt('w.startTime', ':startTime'))
121
            ->setParameter('startTime', $wander->getStartTime())
122
            ->addOrderBy('w.startTime', 'desc')
123
            ->setMaxResults(1)
124
            ->getQuery()
125
            ->getOneOrNullResult();
126
    }
127
128
129
    // /**
130
    //  * @return Wander[] Returns an array of Wander objects
131
    //  */
132
    /*
133
    public function findByExampleField($value)
134
    {
135
        return $this->createQueryBuilder('w')
136
            ->andWhere('w.exampleField = :val')
137
            ->setParameter('val', $value)
138
            ->orderBy('w.id', 'ASC')
139
            ->setMaxResults(10)
140
            ->getQuery()
141
            ->getResult()
142
        ;
143
    }
144
    */
145
146
    /*
147
    public function findOneBySomeField($value): ?Wander
148
    {
149
        return $this->createQueryBuilder('w')
150
            ->andWhere('w.exampleField = :val')
151
            ->setParameter('val', $value)
152
            ->getQuery()
153
            ->getOneOrNullResult()
154
        ;
155
    }
156
    */
157
}
158