Completed
Push — master ( 1d6e78...3ea65a )
by Matt
05:11
created

ImageRepository::getPaginatorQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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