WanderRepository::addWhereHasImages()   A
last analyzed

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\Query;
10
use Doctrine\ORM\QueryBuilder;
11
use Doctrine\Persistence\ManagerRegistry;
12
13
/**
14
 * @method Wander|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method Wander|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method Wander[]    findAll()
17
 * @method Wander[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class WanderRepository extends ServiceEntityRepository
20
{
21
    public function __construct(ManagerRegistry $registry)
22
    {
23
        parent::__construct($registry, Wander::class);
24
    }
25
26
    public function findAll(): array
27
    {
28
        return $this->findBy(array(), array('startTime' => 'DESC'));
29
    }
30
31
    public function standardQueryBuilder(): QueryBuilder
32
    {
33
        return $this->createQueryBuilder('w')
34
            ->orderBy('w.startTime', 'desc');
35
    }
36
37
    public function wandersWithImageCountQueryBuilder(): QueryBuilder
38
    {
39
        return $this->standardQueryBuilder()
40
            ->select('w as wander')
41
            // TODO: Is there a better way to do this without namespacing? This is *fast* compared
42
            // to a grouped query and works fine, though.
43
            ->addSelect('(SELECT COUNT(i) FROM App\Entity\Image i WHERE i.wander = w) AS imageCount');
44
    }
45
46
    public function addWhereHasImages(QueryBuilder $qb, ?bool $hasImages = null)
47
    {
48
        if ($hasImages !== null) {
49
            return $qb->andWhere('w.images is ' . ($hasImages ? 'not' : '') . ' empty');
50
        }
51
        return $qb;
52
    }
53
54
    /**
55
     * Used by Elastica to transform results to model
56
     *
57
     * @param string $entityAlias
58
     * @return QueryBuilder
59
     */
60
    public function createSearchQueryBuilder(string $entityAlias): QueryBuilder
61
    {
62
        $qb = $this->createQueryBuilder($entityAlias);
63
        $qb->select($entityAlias, 'i')
64
            ->leftJoin($entityAlias.'.images', 'i');
65
        return $qb;
66
    }
67
68
    // TODO: Do we still use this? We might only be using findFirstWhereIncludesDate now
69
    public function findWhereIncludesDate(DateTimeInterface $target)
70
    {
71
        return $this->createQueryBuilder('w')
72
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
73
            ->setParameter('target', $target)
74
            ->orderBy('w.startTime')
75
            ->getQuery()
76
            ->getResult();
77
    }
78
79
    public function findFirstWhereIncludesDate(DateTimeInterface $target)
80
    {
81
        return $this->createQueryBuilder('w')
82
            ->andWhere(':target BETWEEN w.startTime AND w.endTime')
83
            ->setParameter('target', $target)
84
            ->orderBy('w.startTime')
85
            ->setMaxResults(1)
86
            ->getQuery()
87
            ->getOneOrNullResult();
88
    }
89
90
    public function findShortest()
91
    {
92
        return $this->createQueryBuilder('w')
93
            ->select('w.id, w.distance')
94
            ->orderBy('w.distance', 'asc')
95
            ->setMaxResults(1)
96
            ->getQuery()
97
            ->getOneOrNullResult();
98
    }
99
100
    public function findLongest()
101
    {
102
        return $this->createQueryBuilder('w')
103
            ->select('w.id, w.distance')
104
            ->orderBy('w.distance', 'desc')
105
            ->setMaxResults(1)
106
            ->getQuery()
107
            ->getOneOrNullResult();
108
    }
109
110
    public function findAverageDistance()
111
    {
112
        return $this->createQueryBuilder('w')
113
            ->select('AVG(w.distance)')
114
            ->getQuery()
115
            ->getSingleScalarResult() ?? 0;
116
    }
117
118
    public function findNext(Wander $wander): ?Wander
119
    {
120
        $qb = $this->createQueryBuilder('w');
121
        return $qb->add('where', $qb->expr()->gt('w.startTime', ':startTime'))
122
            ->setParameter('startTime', $wander->getStartTime())
123
            ->addOrderBy('w.startTime')
124
            ->setMaxResults(1)
125
            ->getQuery()
126
            ->getOneOrNullResult();
127
    }
128
129
    public function findPrev(Wander $wander): ?Wander
130
    {
131
        $qb = $this->createQueryBuilder('w');
132
        return $qb->add('where', $qb->expr()->lt('w.startTime', ':startTime'))
133
            ->setParameter('startTime', $wander->getStartTime())
134
            ->addOrderBy('w.startTime', 'desc')
135
            ->setMaxResults(1)
136
            ->getQuery()
137
            ->getOneOrNullResult();
138
    }
139
140
141
    // /**
142
    //  * @return Wander[] Returns an array of Wander objects
143
    //  */
144
    /*
145
    public function findByExampleField($value)
146
    {
147
        return $this->createQueryBuilder('w')
148
            ->andWhere('w.exampleField = :val')
149
            ->setParameter('val', $value)
150
            ->orderBy('w.id', 'ASC')
151
            ->setMaxResults(10)
152
            ->getQuery()
153
            ->getResult()
154
        ;
155
    }
156
    */
157
158
    /*
159
    public function findOneBySomeField($value): ?Wander
160
    {
161
        return $this->createQueryBuilder('w')
162
            ->andWhere('w.exampleField = :val')
163
            ->setParameter('val', $value)
164
            ->getQuery()
165
            ->getOneOrNullResult()
166
        ;
167
    }
168
    */
169
}
170