Test Failed
Pull Request — master (#228)
by Guilherme
03:49
created

getLastDeliveredVerifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\PhoneVerificationBundle\Entity;
12
13
use Doctrine\ORM\EntityRepository;
14
use LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface;
15
use LoginCidadao\PhoneVerificationBundle\Model\SentVerificationInterface;
16
use Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType;
17
18
/**
19
 * @codeCoverageIgnore
20
 */
21
class SentVerificationRepository extends EntityRepository
22
{
23
    public function getLastVerificationSent(PhoneVerificationInterface $phoneVerification)
24
    {
25
        $query = $this->createQueryBuilder('s')
26
            ->where('s.phone = :phone')
27
            ->orderBy('s.sentAt', 'DESC')
28
            ->setParameter('phone', $phoneVerification->getPhone(), PhoneNumberType::NAME);
29
30
        // Filter only SentVerification that belong to this PhoneVerification
31
        if ($phoneVerification->isVerified()) {
32
            $query
33
                ->andWhere('s.sentAt BETWEEN :created AND :verified')
34
                ->setParameter('created', $phoneVerification->getCreatedAt())
35
                ->setParameter('verified', $phoneVerification->getVerifiedAt());
36
        } else {
37
            $query
38
                ->andWhere('s.sentAt >= :created')
39
                ->setParameter('created', $phoneVerification->getCreatedAt());
40
        }
41
42
        return $query->setMaxResults(1)->getQuery()->getOneOrNullResult();
43
    }
44
45
    /**
46
     * @return \Doctrine\ORM\Query
47
     */
48
    public function getPendingUpdateSentVerificationQuery()
49
    {
50
        $query = $this->createQueryBuilder('s')
51
            ->where('s.finished IS NULL OR s.finished != :finished')
52
            ->setParameter('finished', true, \PDO::PARAM_BOOL)
53
            ->orderBy('s.sentAt', 'DESC')
54
            ->getQuery();
55
56
        return $query;
57
    }
58
59
    public function countPendingUpdateSentVerification()
60
    {
61
        $count = $this->createQueryBuilder('s')
62
            ->select('COUNT(s)')
63
            ->where('s.finished IS NULL OR s.finished != :finished')
64
            ->setParameter('finished', true, \PDO::PARAM_BOOL)
65
            ->getQuery()
66
            ->getSingleScalarResult();
67
68
        return $count;
69
    }
70
71
    /**
72
     * @param int $limit
73
     * @return array|SentVerificationInterface[]
74
     */
75
    public function getLastDeliveredVerifications($limit = 10)
76
    {
77
        $query = $this->createQueryBuilder('s')
78
            ->where('s.deliveredAt IS NOT NULL')
79
            ->orderBy('s.deliveredAt', 'DESC')
80
            ->setMaxResults($limit)
81
            ->getQuery();
82
83
        return $query->getResult();
84
    }
85
86
    /**
87
     * @param \DateTime $date
88
     * @return SentVerification[]
89
     */
90
    public function getNotDeliveredSince(\DateTime $date)
91
    {
92
        $query = $this->createQueryBuilder('s')
93
            ->where('s.deliveredAt IS NULL')
94
            ->andWhere('s.sentAt <= :date')
95
            ->andWhere('s.finished IS NULL OR s.finished != :finished')
96
            ->orderBy('s.sentAt', 'ASC')
97
            ->setParameter('date', $date)
98
            ->setParameter('finished', true, \PDO::PARAM_BOOL)
99
            ->getQuery();
100
101
        return $query->getResult();
102
    }
103
}
104