Completed
Push — utc-datetime-storing ( 6fbf75...e1fcef )
by Kamil
25:51
created

createPaginatorForPromotion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\PromotionBundle\Doctrine\ORM;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
use Sylius\Component\Promotion\Model\PromotionCouponInterface;
19
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
class PromotionCouponRepository extends EntityRepository implements PromotionCouponRepositoryInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createQueryBuilderByPromotionId($promotionId): QueryBuilder
30
    {
31
        return $this->createQueryBuilder('o')
32
            ->andWhere('o.promotion = :promotionId')
33
            ->setParameter('promotionId', $promotionId)
34
        ;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function countByCodeLength(int $codeLength): int
41
    {
42
        return (int) $this->createQueryBuilder('o')
43
            ->select('COUNT(o.id)')
44
            ->andWhere('LENGTH(o.code) = :codeLength')
45
            ->setParameter('codeLength', $codeLength)
46
            ->getQuery()
47
            ->getSingleScalarResult()
48
        ;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function findOneByCodeAndPromotionCode(string $code, string $promotionCode): ?PromotionCouponInterface
55
    {
56
        return $this->createQueryBuilder('o')
57
            ->leftJoin('o.promotion', 'promotion')
58
            ->where('promotion.code = :promotionCode')
59
            ->andWhere('o.code = :code')
60
            ->setParameter('promotionCode', $promotionCode)
61
            ->setParameter('code', $code)
62
            ->getQuery()
63
            ->getOneOrNullResult()
64
        ;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function createPaginatorForPromotion(string $promotionCode): iterable
71
    {
72
        $queryBuilder = $this->createQueryBuilder('o')
73
            ->leftJoin('o.promotion', 'promotion')
74
            ->where('promotion.code = :promotionCode')
75
            ->setParameter('promotionCode', $promotionCode)
76
        ;
77
78
        return $this->getPaginator($queryBuilder);
79
    }
80
}
81