Completed
Push — master ( 027cf3...96bb6d )
by Matt
04:34
created

ImageRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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