Completed
Pull Request — master (#142)
by
unknown
07:20
created

BlockRepository::findByProductCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Repository;
14
15
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
19
class BlockRepository extends EntityRepository implements BlockRepositoryInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function createListQueryBuilder(string $localeCode): QueryBuilder
25
    {
26
        return $this->createQueryBuilder('o')
27
            ->innerJoin('o.translations', 'translation')
28
            ->where('translation.locale = :localeCode')
29
            ->setParameter('localeCode', $localeCode)
30
        ;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function findOneEnabledByCodeAndChannelCode(string $code, string $channelCode): ?BlockInterface
37
    {
38
        return $this->createQueryBuilder('o')
39
            ->innerJoin('o.channels', 'channels')
40
            ->where('o.code = :code')
41
            ->andWhere('o.enabled = true')
42
            ->andWhere('channels.code = :channelCode')
43
            ->setParameter('code', $code)
44
            ->setParameter('channelCode', $channelCode)
45
            ->getQuery()
46
            ->getOneOrNullResult()
47
        ;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function findBySectionCodeAndChannelCode(
54
        string $sectionCode,
55
        string $localeCode,
56
        string $channelCode
57
    ): array {
58
        return $this->createQueryBuilder('o')
59
            ->leftJoin('o.translations', 'translation')
60
            ->innerJoin('o.sections', 'section')
61
            ->innerJoin('o.channels', 'channels')
62
            ->andWhere('translation.locale = :localeCode')
63
            ->andWhere('section.code = :sectionCode')
64
            ->andWhere('o.enabled = true')
65
            ->andWhere('channels.code = :channelCode')
66
            ->setParameter('localeCode', $localeCode)
67
            ->setParameter('sectionCode', $sectionCode)
68
            ->setParameter('channelCode', $channelCode)
69
            ->getQuery()
70
            ->getResult()
71
        ;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function findByProductCodeAndChannelCode(
78
        string $productCode,
79
        string $localeCode,
80
        string $channelCode
81
    ): array {
82
        return $this->createQueryBuilder('o')
83
            ->leftJoin('o.translations', 'translation')
84
            ->innerJoin('o.products', 'product')
85
            ->innerJoin('o.channels', 'channels')
86
            ->andWhere('translation.locale = :localeCode')
87
            ->andWhere('product.code = :productCode')
88
            ->andWhere('o.enabled = true')
89
            ->andWhere('channels.code = :channelCode')
90
            ->setParameter('localeCode', $localeCode)
91
            ->setParameter('productCode', $productCode)
92
            ->setParameter('channelCode', $channelCode)
93
            ->getQuery()
94
            ->getResult()
95
        ;
96
    }
97
}
98