Completed
Push — master ( 99e4c0...e9ccac )
by Matt
04:11
created

ImageRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 82
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findBetweenDates() 0 9 1
A findFromIdOnwards() 0 8 1
A findPrev() 0 26 2
A findNext() 0 26 2
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 findFromIdOnwards(int $id)
35
    {
36
        return $this->createQueryBuilder('i')
37
            ->andWhere('i.id >= :id')
38
            ->setParameter('id', $id)
39
            ->orderBy('i.id')
40
            ->getQuery()
41
            ->getResult();
42
    }
43
44
    public function findNext(Image $image)
45
    {
46
        $wander = $image->getWander();
47
        if ($wander === null) {
48
            return null;
49
        }
50
51
        $qb = $this->createQueryBuilder('i');
52
        $qb->add('where', $qb->expr()->andX(
53
            $qb->expr()->eq('i.wander', ':wander'),
54
            $qb->expr()->orX(
55
                $qb->expr()->gt('i.capturedAt', ':capturedAt'),
56
                $qb->expr()->andX(
57
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
58
                    $qb->expr()->gt('i.id', ':id')
59
                )
60
            )
61
        ));
62
        $qb->setParameter('wander', $wander)
63
            ->setParameter('capturedAt', $image->getCapturedAt())
64
            ->setParameter('id', $image->getId());
65
66
        $qb->addOrderBy('i.capturedAt')
67
            ->addOrderBy('i.id');
68
        $qb->setMaxResults(1);
69
        return $qb->getQuery()->getOneOrNullResult();
70
    }
71
72
    public function findPrev(Image $image)
73
    {
74
        $wander = $image->getWander();
75
        if ($wander === null) {
76
            return null;
77
        }
78
79
        $qb = $this->createQueryBuilder('i');
80
        $qb->add('where', $qb->expr()->andX(
81
            $qb->expr()->eq('i.wander', ':wander'),
82
            $qb->expr()->orX(
83
                $qb->expr()->lt('i.capturedAt', ':capturedAt'),
84
                $qb->expr()->andX(
85
                    $qb->expr()->eq('i.capturedAt', ':capturedAt'),
86
                    $qb->expr()->lt('i.id', ':id')
87
                )
88
            )
89
        ));
90
        $qb->setParameter('wander', $wander)
91
            ->setParameter('capturedAt', $image->getCapturedAt())
92
            ->setParameter('id', $image->getId());
93
94
        $qb->addOrderBy('i.capturedAt', 'desc')
95
            ->addOrderBy('i.id', 'desc');
96
        $qb->setMaxResults(1);
97
        return $qb->getQuery()->getOneOrNullResult();
98
    }
99
100
    // /**
101
    //  * @return Image[] Returns an array of Image objects
102
    //  */
103
    /*
104
    public function findByExampleField($value)
105
    {
106
        return $this->createQueryBuilder('i')
107
            ->andWhere('i.exampleField = :val')
108
            ->setParameter('val', $value)
109
            ->orderBy('i.id', 'ASC')
110
            ->setMaxResults(10)
111
            ->getQuery()
112
            ->getResult()
113
        ;
114
    }
115
    */
116
117
    /*
118
    public function findOneBySomeField($value): ?Image
119
    {
120
        return $this->createQueryBuilder('i')
121
            ->andWhere('i.exampleField = :val')
122
            ->setParameter('val', $value)
123
            ->getQuery()
124
            ->getOneOrNullResult()
125
        ;
126
    }
127
    */
128
}
129