Passed
Pull Request — master (#163)
by Matt
04:38
created

ImageRepository::findWithNoWander()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
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 Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
use Doctrine\ORM\Query;
10
use Doctrine\ORM\QueryBuilder;
11
use Doctrine\Persistence\ManagerRegistry;
12
use Knp\Component\Pager\Paginator;
13
14
/**
15
 * @method Image|null find($id, $lockMode = null, $lockVersion = null)
16
 * @method Image|null findOneBy(array $criteria, array $orderBy = null)
17
 * @method Image[]    findAll()
18
 * @method Image[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
19
 */
20
class ImageRepository extends ServiceEntityRepository
21
{
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, Image::class);
25
    }
26
27
    public function standardQueryBuilder(): QueryBuilder
28
    {
29
        return $this->createQueryBuilder('i');
30
    }
31
32
    public function findBetweenDates(DateTime $from, DateTime $to)
33
    {
34
        return $this->createQueryBuilder('i')
35
            ->andWhere('i.capturedAt BETWEEN :from AND :to')
36
            ->setParameter('from', $from)
37
            ->setParameter('to', $to)
38
            ->orderBy('i.capturedAt')
39
            ->getQuery()
40
            ->getResult();
41
    }
42
43
    public function findWithNoWander()
44
    {
45
        return $this->createQueryBuilder('i')
46
            ->andWhere('i.wander IS NULL')
47
            ->orderBy('i.capturedAt', 'desc')
48
            ->getQuery()
49
            ->getResult();
50
    }
51
52
    public function findWithNoLocation()
53
    {
54
        return $this->createQueryBuilder('i')
55
        ->andWhere('i.location IS NULL')
56
        ->orWhere("i.location = ''")
57
        ->orderBy('i.capturedAt', 'desc')
58
        ->getQuery()
59
        ->getResult();
60
    }
61
62
    public function findWithNoLocationButHasLatLng()
63
    {
64
        $qb = $this->createQueryBuilder('i');
65
        return $qb
66
            ->add('where',
67
                $qb->expr()->andX(
68
                    $qb->expr()->orX(
69
                        $qb->expr()->eq('i.location', "''"),
70
                        $qb->expr()->isNull('i.location')
71
                    ),
72
                    $qb->expr()->isNotNull('i.latlng')
73
                )
74
            )
75
            ->addOrderBy('i.capturedAt', 'desc')
76
            ->getQuery()
77
            ->getResult();
78
    }
79
80
81
    public function findFromIdOnwards(int $id)
82
    {
83
        return $this->createQueryBuilder('i')
84
            ->andWhere('i.id >= :id')
85
            ->setParameter('id', $id)
86
            ->orderBy('i.id')
87
            ->getQuery()
88
            ->getResult();
89
    }
90
91
    public function getPaginatorQueryBuilder(?Wander $wander = null): QueryBuilder
92
    {
93
        $qb = $this->createQueryBuilder('i')
94
            ->addOrderBy('i.capturedAt')
95
            ->addOrderBy('i.id'); // tie-breaker
96
        if ($wander !== null) {
97
            $qb
98
                ->andWhere('i.wander = :wander')
99
                ->setParameter('wander', $wander);
100
        }
101
102
        return $qb;
103
    }
104
105
    public function findNext(Image $image)
106
    {
107
        $wander = $image->getWander();
108
        if ($wander === null) {
109
            return null;
110
        }
111
112
        $qb = $this->createQueryBuilder('i');
113
        $qb->add('where', $qb->expr()->andX(
114
            $qb->expr()->eq('i.wander', ':wander'),
115
            $qb->expr()->orX(
116
                $qb->expr()->gt('i.capturedAt', ':capturedAt'),
117
                $qb->expr()->andX(
118
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
119
                    $qb->expr()->gt('i.id', ':id')
120
                )
121
            )
122
        ));
123
        $qb->setParameter('wander', $wander)
124
            ->setParameter('capturedAt', $image->getCapturedAt())
125
            ->setParameter('id', $image->getId());
126
127
        $qb->addOrderBy('i.capturedAt')
128
            ->addOrderBy('i.id');
129
        $qb->setMaxResults(1);
130
        return $qb->getQuery()->getOneOrNullResult();
131
    }
132
133
    public function findPrev(Image $image)
134
    {
135
        $wander = $image->getWander();
136
        if ($wander === null) {
137
            return null;
138
        }
139
140
        $qb = $this->createQueryBuilder('i');
141
        $qb->add('where', $qb->expr()->andX(
142
            $qb->expr()->eq('i.wander', ':wander'),
143
            $qb->expr()->orX(
144
                $qb->expr()->lt('i.capturedAt', ':capturedAt'),
145
                $qb->expr()->andX(
146
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
147
                    $qb->expr()->lt('i.id', ':id')
148
                )
149
            )
150
        ));
151
        $qb->setParameter('wander', $wander)
152
            ->setParameter('capturedAt', $image->getCapturedAt())
153
            ->setParameter('id', $image->getId());
154
155
        $qb->addOrderBy('i.capturedAt', 'desc')
156
            ->addOrderBy('i.id', 'desc');
157
        $qb->setMaxResults(1);
158
        return $qb->getQuery()->getOneOrNullResult();
159
    }
160
161
    // /**
162
    //  * @return Image[] Returns an array of Image objects
163
    //  */
164
    /*
165
    public function findByExampleField($value)
166
    {
167
        return $this->createQueryBuilder('i')
168
            ->andWhere('i.exampleField = :val')
169
            ->setParameter('val', $value)
170
            ->orderBy('i.id', 'ASC')
171
            ->setMaxResults(10)
172
            ->getQuery()
173
            ->getResult()
174
        ;
175
    }
176
    */
177
178
    /*
179
    public function findOneBySomeField($value): ?Image
180
    {
181
        return $this->createQueryBuilder('i')
182
            ->andWhere('i.exampleField = :val')
183
            ->setParameter('val', $value)
184
            ->getQuery()
185
            ->getOneOrNullResult()
186
        ;
187
    }
188
    */
189
}
190