PromoCodeRepository   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 14
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A findActivePromoCodeByCodeAndEvent() 0 18 2
1
<?php
2
3
namespace Stfalcon\Bundle\EventBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\NonUniqueResultException;
7
use Stfalcon\Bundle\EventBundle\Entity\Event;
8
use Stfalcon\Bundle\EventBundle\Entity\PromoCode;
9
10
/**
11
 * PromoCodeRepository.
12
 */
13
class PromoCodeRepository extends EntityRepository
14
{
15
    /**
16
     * @param string $code
17
     * @param Event  $event
18
     *
19
     * @return PromoCode|null
20
     *
21
     * @throws \Exception
22
     */
23
    public function findActivePromoCodeByCodeAndEvent($code, $event)
24
    {
25
        $qb = $this->createQueryBuilder('pc');
26
        $qb->andWhere($qb->expr()->eq('pc.event', ':event'))
27
            ->andWhere($qb->expr()->eq('pc.code', ':code'))
28
            ->andWhere($qb->expr()->gte('pc.endDate', ':now'))
29
            ->setParameter('code', $code)
30
            ->setParameter('event', $event)
31
            ->setParameter('now', new \DateTime())
32
            ->setMaxResults(1)
33
        ;
34
        try {
35
            $result = $qb->getQuery()->getOneOrNullResult();
36
        } catch (NonUniqueResultException $e) {
37
            $result = null;
38
        }
39
40
        return $result;
41
    }
42
}
43