Passed
Push — master ( 028b7a...8da5b1 )
by Matt
05:44 queued 55s
created

ImageRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 120
c 2
b 0
f 0
dl 0
loc 213
rs 10
wmc 22

16 Methods

Rating   Name   Duplication   Size   Complexity  
A standardQueryBuilder() 0 3 1
A findBetweenDates() 0 9 1
A __construct() 0 3 1
A findWithNoWander() 0 7 1
A findWithNoLocation() 0 8 1
A findWithNoLocationButHasLatLng() 0 16 1
A findPrev() 0 26 2
A getReversePaginatorQueryBuilder() 0 11 2
A getAllLocations() 0 11 1
A getLatestImageCaptureDate() 0 7 2
A getEarliestImageOrNull() 0 9 1
A findFromIdOnwards() 0 8 1
A findNext() 0 26 2
A getPaginatorQueryBuilder() 0 11 2
A getEarliestImageCaptureDate() 0 7 2
A getLatestImageOrNull() 0 9 1
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
    {
83
        // TODO: What happens when there aren't any images/locations?
84
        $result = $this->createQueryBuilder('i')
85
            ->select('i.location')
86
            ->where('i.location IS NOT NULL')
87
            ->groupBy('i.location')
88
            ->orderBy('i.location')
89
            ->getQuery()
90
            ->getArrayResult();
91
        return array_column($result, 'location');
92
    }
93
94
    public function findFromIdOnwards(int $id)
95
    {
96
        return $this->createQueryBuilder('i')
97
            ->andWhere('i.id >= :id')
98
            ->setParameter('id', $id)
99
            ->orderBy('i.id')
100
            ->getQuery()
101
            ->getResult();
102
    }
103
104
    /**
105
     * Returns the earliest Image by capturedAt date. Ignores any
106
     * images without capturedAt dates. Will return null only if
107
     * there are no Images with capturedAt dates at all.
108
     */
109
    public function getEarliestImageOrNull(): ?Image
110
    {
111
        return $this->createQueryBuilder('i')
112
            ->orderBy('i.capturedAt')
113
            ->orderBy('i.id') // might as well be deterministic
114
            ->andWhere('i.capturedAt IS NOT NULL')
115
            ->setMaxResults(1)
116
            ->getQuery()
117
            ->getOneOrNullResult();
118
    }
119
120
    public function getEarliestImageCaptureDate(): ?DateTimeInterface
121
    {
122
        $image = $this->getEarliestImageOrNull();
123
        if ($image === null) {
124
            return null;
125
        }
126
        return $image->getCapturedAt();
127
    }
128
129
    /**
130
     * Returns the most recent Image by capturedAt date. Ignores any
131
     * images without capturedAt dates. Will return null only if
132
     * there are no Images with capturedAt dates at all.
133
     */
134
    public function getLatestImageOrNull(): ?Image
135
    {
136
        return $this->createQueryBuilder('i')
137
            ->orderBy('i.capturedAt', 'desc')
138
            ->orderBy('i.id', 'desc') // might as well be deterministic
139
            ->andWhere('i.capturedAt IS NOT NULL')
140
            ->setMaxResults(1)
141
            ->getQuery()
142
            ->getOneOrNullResult();
143
    }
144
145
    public function getLatestImageCaptureDate(): ?DateTimeInterface
146
    {
147
        $image = $this->getLatestImageOrNull();
148
        if ($image === null) {
149
            return null;
150
        }
151
        return $image->getCapturedAt();
152
    }
153
154
    public function getPaginatorQueryBuilder(?Wander $wander = null): QueryBuilder
155
    {
156
        $qb = $this->createQueryBuilder('i')
157
            ->addOrderBy('i.capturedAt')
158
            ->addOrderBy('i.id'); // tie-breaker
159
        if ($wander !== null) {
160
            $qb
161
                ->andWhere('i.wander = :wander')
162
                ->setParameter('wander', $wander);
163
        }
164
        return $qb;
165
    }
166
167
    public function getReversePaginatorQueryBuilder(?Wander $wander = null): QueryBuilder
168
    {
169
        $qb = $this->createQueryBuilder('i')
170
            ->addOrderBy('i.capturedAt', 'desc')
171
            ->addOrderBy('i.id', 'desc'); // tie-breaker
172
        if ($wander !== null) {
173
            $qb
174
                ->andWhere('i.wander = :wander')
175
                ->setParameter('wander', $wander);
176
        }
177
        return $qb;
178
    }
179
180
    public function findNext(Image $image)
181
    {
182
        $wander = $image->getWander();
183
        if ($wander === null) {
184
            return null;
185
        }
186
187
        $qb = $this->createQueryBuilder('i');
188
        $qb->add('where', $qb->expr()->andX(
189
            $qb->expr()->eq('i.wander', ':wander'),
190
            $qb->expr()->orX(
191
                $qb->expr()->gt('i.capturedAt', ':capturedAt'),
192
                $qb->expr()->andX(
193
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
194
                    $qb->expr()->gt('i.id', ':id')
195
                )
196
            )
197
        ));
198
        $qb->setParameter('wander', $wander)
199
            ->setParameter('capturedAt', $image->getCapturedAt())
200
            ->setParameter('id', $image->getId());
201
202
        $qb->addOrderBy('i.capturedAt')
203
            ->addOrderBy('i.id');
204
        $qb->setMaxResults(1);
205
        return $qb->getQuery()->getOneOrNullResult();
206
    }
207
208
    public function findPrev(Image $image)
209
    {
210
        $wander = $image->getWander();
211
        if ($wander === null) {
212
            return null;
213
        }
214
215
        $qb = $this->createQueryBuilder('i');
216
        $qb->add('where', $qb->expr()->andX(
217
            $qb->expr()->eq('i.wander', ':wander'),
218
            $qb->expr()->orX(
219
                $qb->expr()->lt('i.capturedAt', ':capturedAt'),
220
                $qb->expr()->andX(
221
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
222
                    $qb->expr()->lt('i.id', ':id')
223
                )
224
            )
225
        ));
226
        $qb->setParameter('wander', $wander)
227
            ->setParameter('capturedAt', $image->getCapturedAt())
228
            ->setParameter('id', $image->getId());
229
230
        $qb->addOrderBy('i.capturedAt', 'desc')
231
            ->addOrderBy('i.id', 'desc');
232
        $qb->setMaxResults(1);
233
        return $qb->getQuery()->getOneOrNullResult();
234
    }
235
236
    // /**
237
    //  * @return Image[] Returns an array of Image objects
238
    //  */
239
    /*
240
    public function findByExampleField($value)
241
    {
242
        return $this->createQueryBuilder('i')
243
            ->andWhere('i.exampleField = :val')
244
            ->setParameter('val', $value)
245
            ->orderBy('i.id', 'ASC')
246
            ->setMaxResults(10)
247
            ->getQuery()
248
            ->getResult()
249
        ;
250
    }
251
    */
252
253
    /*
254
    public function findOneBySomeField($value): ?Image
255
    {
256
        return $this->createQueryBuilder('i')
257
            ->andWhere('i.exampleField = :val')
258
            ->setParameter('val', $value)
259
            ->getQuery()
260
            ->getOneOrNullResult()
261
        ;
262
    }
263
    */
264
}
265