Passed
Pull Request — master (#156)
by Matt
05:36
created

ImageRepository::standardQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 getPaginatorQuery(Wander $wander): Query
92
    {
93
        $qb = $this->createQueryBuilder('i');
94
        return $qb
95
            ->andWhere('i.wander = :wander')
96
            ->setParameter('wander', $wander)
97
            ->addOrderBy('i.capturedAt')
98
            ->addOrderBy('i.id') // tie-breaker
99
            ->getQuery();
100
    }
101
102
    public function findNext(Image $image)
103
    {
104
        $wander = $image->getWander();
105
        if ($wander === null) {
106
            return null;
107
        }
108
109
        $qb = $this->createQueryBuilder('i');
110
        $qb->add('where', $qb->expr()->andX(
111
            $qb->expr()->eq('i.wander', ':wander'),
112
            $qb->expr()->orX(
113
                $qb->expr()->gt('i.capturedAt', ':capturedAt'),
114
                $qb->expr()->andX(
115
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
116
                    $qb->expr()->gt('i.id', ':id')
117
                )
118
            )
119
        ));
120
        $qb->setParameter('wander', $wander)
121
            ->setParameter('capturedAt', $image->getCapturedAt())
122
            ->setParameter('id', $image->getId());
123
124
        $qb->addOrderBy('i.capturedAt')
125
            ->addOrderBy('i.id');
126
        $qb->setMaxResults(1);
127
        return $qb->getQuery()->getOneOrNullResult();
128
    }
129
130
    public function findPrev(Image $image)
131
    {
132
        $wander = $image->getWander();
133
        if ($wander === null) {
134
            return null;
135
        }
136
137
        $qb = $this->createQueryBuilder('i');
138
        $qb->add('where', $qb->expr()->andX(
139
            $qb->expr()->eq('i.wander', ':wander'),
140
            $qb->expr()->orX(
141
                $qb->expr()->lt('i.capturedAt', ':capturedAt'),
142
                $qb->expr()->andX(
143
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
144
                    $qb->expr()->lt('i.id', ':id')
145
                )
146
            )
147
        ));
148
        $qb->setParameter('wander', $wander)
149
            ->setParameter('capturedAt', $image->getCapturedAt())
150
            ->setParameter('id', $image->getId());
151
152
        $qb->addOrderBy('i.capturedAt', 'desc')
153
            ->addOrderBy('i.id', 'desc');
154
        $qb->setMaxResults(1);
155
        return $qb->getQuery()->getOneOrNullResult();
156
    }
157
158
    // /**
159
    //  * @return Image[] Returns an array of Image objects
160
    //  */
161
    /*
162
    public function findByExampleField($value)
163
    {
164
        return $this->createQueryBuilder('i')
165
            ->andWhere('i.exampleField = :val')
166
            ->setParameter('val', $value)
167
            ->orderBy('i.id', 'ASC')
168
            ->setMaxResults(10)
169
            ->getQuery()
170
            ->getResult()
171
        ;
172
    }
173
    */
174
175
    /*
176
    public function findOneBySomeField($value): ?Image
177
    {
178
        return $this->createQueryBuilder('i')
179
            ->andWhere('i.exampleField = :val')
180
            ->setParameter('val', $value)
181
            ->getQuery()
182
            ->getOneOrNullResult()
183
        ;
184
    }
185
    */
186
}
187