Passed
Pull Request — master (#165)
by Matt
04:39
created

ImageRepository::getPaginatorQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Image;
6
use App\Entity\Wander;
7
use DateTime;
8
use DateTimeInterface;
9
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10
use Doctrine\ORM\Query;
11
use Doctrine\ORM\QueryBuilder;
12
use Doctrine\Persistence\ManagerRegistry;
13
use Knp\Component\Pager\Paginator;
14
15
/**
16
 * @method Image|null find($id, $lockMode = null, $lockVersion = null)
17
 * @method Image|null findOneBy(array $criteria, array $orderBy = null)
18
 * @method Image[]    findAll()
19
 * @method Image[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
20
 */
21
class ImageRepository extends ServiceEntityRepository
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, Image::class);
26
    }
27
28
    public function standardQueryBuilder(): QueryBuilder
29
    {
30
        return $this->createQueryBuilder('i');
31
    }
32
33
    public function findBetweenDates(DateTime $from, DateTime $to)
34
    {
35
        return $this->createQueryBuilder('i')
36
            ->andWhere('i.capturedAt BETWEEN :from AND :to')
37
            ->setParameter('from', $from)
38
            ->setParameter('to', $to)
39
            ->orderBy('i.capturedAt')
40
            ->getQuery()
41
            ->getResult();
42
    }
43
44
    public function findWithNoWander()
45
    {
46
        return $this->createQueryBuilder('i')
47
            ->andWhere('i.wander IS NULL')
48
            ->orderBy('i.capturedAt', 'desc')
49
            ->getQuery()
50
            ->getResult();
51
    }
52
53
    public function findWithNoLocation()
54
    {
55
        return $this->createQueryBuilder('i')
56
        ->andWhere('i.location IS NULL')
57
        ->orWhere("i.location = ''")
58
        ->orderBy('i.capturedAt', 'desc')
59
        ->getQuery()
60
        ->getResult();
61
    }
62
63
    public function findWithNoLocationButHasLatLng()
64
    {
65
        $qb = $this->createQueryBuilder('i');
66
        return $qb
67
            ->add('where',
68
                $qb->expr()->andX(
69
                    $qb->expr()->orX(
70
                        $qb->expr()->eq('i.location', "''"),
71
                        $qb->expr()->isNull('i.location')
72
                    ),
73
                    $qb->expr()->isNotNull('i.latlng')
74
                )
75
            )
76
            ->addOrderBy('i.capturedAt', 'desc')
77
            ->getQuery()
78
            ->getResult();
79
    }
80
81
    public function getAllLocations(): array {
82
        // TODO: What happens when there aren't any images/locations?
83
        $result = $this->createQueryBuilder('i')
84
            ->select('i.location')
85
            ->where('i.location IS NOT NULL')
86
            ->groupBy('i.location')
87
            ->orderBy('i.location')
88
            ->getQuery()
89
            ->getArrayResult();
90
        return array_column($result, 'location');
91
    }
92
93
    public function findFromIdOnwards(int $id)
94
    {
95
        return $this->createQueryBuilder('i')
96
            ->andWhere('i.id >= :id')
97
            ->setParameter('id', $id)
98
            ->orderBy('i.id')
99
            ->getQuery()
100
            ->getResult();
101
    }
102
103
    /**
104
     * Returns the earliest Image by capturedAt date. Ignores any
105
     * images without capturedAt dates. Will return null only if
106
     * there are no Images with capturedAt dates at all.
107
     */
108
    public function getEarliestImageOrNull(): ?Image {
109
        return $this->createQueryBuilder('i')
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return App\Entity\Image|null. Consider adding an additional type-check to rule them out.
Loading history...
110
            ->orderBy('i.capturedAt')
111
            ->orderBy('i.id') // might as well be deterministic
112
            ->andWhere('i.capturedAt IS NOT NULL')
113
            ->setMaxResults(1)
114
            ->getQuery()
115
            ->getOneOrNullResult();
116
    }
117
118
    public function getEarliestImageCaptureDate(): ?DateTimeInterface
119
    {
120
        $image = $this->getEarliestImageOrNull();
121
        if ($image === null) {
122
            return null;
123
        }
124
        return $image->getCapturedAt();
125
    }
126
127
    /**
128
     * Returns the most recent Image by capturedAt date. Ignores any
129
     * images without capturedAt dates. Will return null only if
130
     * there are no Images with capturedAt dates at all.
131
     */
132
    public function getLatestImageOrNull(): ?Image {
133
        return $this->createQueryBuilder('i')
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return App\Entity\Image|null. Consider adding an additional type-check to rule them out.
Loading history...
134
            ->orderBy('i.capturedAt', 'desc')
135
            ->orderBy('i.id', 'desc') // might as well be deterministic
136
            ->andWhere('i.capturedAt IS NOT NULL')
137
            ->setMaxResults(1)
138
            ->getQuery()
139
            ->getOneOrNullResult();
140
    }
141
142
    public function getLatestImageCaptureDate(): ?DateTimeInterface
143
    {
144
        $image = $this->getLatestImageOrNull();
145
        if ($image === null) {
146
            return null;
147
        }
148
        return $image->getCapturedAt();
149
    }
150
151
    public function getPaginatorQueryBuilder(?Wander $wander = null): QueryBuilder
152
    {
153
        $qb = $this->createQueryBuilder('i')
154
            ->addOrderBy('i.capturedAt')
155
            ->addOrderBy('i.id'); // tie-breaker
156
        if ($wander !== null) {
157
            $qb
158
                ->andWhere('i.wander = :wander')
159
                ->setParameter('wander', $wander);
160
        }
161
        return $qb;
162
    }
163
164
    public function getReversePaginatorQueryBuilder(?Wander $wander = null): QueryBuilder
165
    {
166
        $qb = $this->createQueryBuilder('i')
167
            ->addOrderBy('i.capturedAt', 'desc')
168
            ->addOrderBy('i.id', 'desc'); // tie-breaker
169
        if ($wander !== null) {
170
            $qb
171
                ->andWhere('i.wander = :wander')
172
                ->setParameter('wander', $wander);
173
        }
174
        return $qb;
175
    }
176
177
    public function findNext(Image $image)
178
    {
179
        $wander = $image->getWander();
180
        if ($wander === null) {
181
            return null;
182
        }
183
184
        $qb = $this->createQueryBuilder('i');
185
        $qb->add('where', $qb->expr()->andX(
186
            $qb->expr()->eq('i.wander', ':wander'),
187
            $qb->expr()->orX(
188
                $qb->expr()->gt('i.capturedAt', ':capturedAt'),
189
                $qb->expr()->andX(
190
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
191
                    $qb->expr()->gt('i.id', ':id')
192
                )
193
            )
194
        ));
195
        $qb->setParameter('wander', $wander)
196
            ->setParameter('capturedAt', $image->getCapturedAt())
197
            ->setParameter('id', $image->getId());
198
199
        $qb->addOrderBy('i.capturedAt')
200
            ->addOrderBy('i.id');
201
        $qb->setMaxResults(1);
202
        return $qb->getQuery()->getOneOrNullResult();
203
    }
204
205
    public function findPrev(Image $image)
206
    {
207
        $wander = $image->getWander();
208
        if ($wander === null) {
209
            return null;
210
        }
211
212
        $qb = $this->createQueryBuilder('i');
213
        $qb->add('where', $qb->expr()->andX(
214
            $qb->expr()->eq('i.wander', ':wander'),
215
            $qb->expr()->orX(
216
                $qb->expr()->lt('i.capturedAt', ':capturedAt'),
217
                $qb->expr()->andX(
218
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
219
                    $qb->expr()->lt('i.id', ':id')
220
                )
221
            )
222
        ));
223
        $qb->setParameter('wander', $wander)
224
            ->setParameter('capturedAt', $image->getCapturedAt())
225
            ->setParameter('id', $image->getId());
226
227
        $qb->addOrderBy('i.capturedAt', 'desc')
228
            ->addOrderBy('i.id', 'desc');
229
        $qb->setMaxResults(1);
230
        return $qb->getQuery()->getOneOrNullResult();
231
    }
232
233
    // /**
234
    //  * @return Image[] Returns an array of Image objects
235
    //  */
236
    /*
237
    public function findByExampleField($value)
238
    {
239
        return $this->createQueryBuilder('i')
240
            ->andWhere('i.exampleField = :val')
241
            ->setParameter('val', $value)
242
            ->orderBy('i.id', 'ASC')
243
            ->setMaxResults(10)
244
            ->getQuery()
245
            ->getResult()
246
        ;
247
    }
248
    */
249
250
    /*
251
    public function findOneBySomeField($value): ?Image
252
    {
253
        return $this->createQueryBuilder('i')
254
            ->andWhere('i.exampleField = :val')
255
            ->setParameter('val', $value)
256
            ->getQuery()
257
            ->getOneOrNullResult()
258
        ;
259
    }
260
    */
261
}
262