Completed
Push — master ( a09c02...265d88 )
by Mikołaj
02:00
created

PageRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 121
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createListQueryBuilder() 0 8 1
A findEnabled() 0 11 1
A findOneEnabledByCode() 0 13 1
A findOneEnabledBySlugAndChannelCode() 0 19 1
A createShopListQueryBuilder() 0 12 1
A findBySectionCode() 0 14 1
A findByProduct() 0 14 1
A findByProductAndSectionCode() 0 20 1
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\PageInterface;
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
use Sylius\Component\Core\Model\ProductInterface;
19
20
class PageRepository extends EntityRepository implements PageRepositoryInterface
21
{
22
    public function createListQueryBuilder(string $localeCode): QueryBuilder
23
    {
24
        return $this->createQueryBuilder('o')
25
            ->addSelect('translation')
26
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
27
            ->setParameter('localeCode', $localeCode)
28
        ;
29
    }
30
31
    public function findEnabled(bool $enabled): array
32
    {
33
        return $this->createQueryBuilder('o')
34
            ->addSelect('translation')
35
            ->innerJoin('o.translations', 'translation')
36
            ->andWhere('o.enabled = :enabled')
37
            ->setParameter('enabled', $enabled)
38
            ->getQuery()
39
            ->getResult()
40
        ;
41
    }
42
43
    public function findOneEnabledByCode(string $code, ?string $localeCode): ?PageInterface
44
    {
45
        return $this->createQueryBuilder('o')
46
            ->leftJoin('o.translations', 'translation')
47
            ->where('translation.locale = :localeCode')
48
            ->andWhere('o.code = :code')
49
            ->andWhere('o.enabled = true')
50
            ->setParameter('code', $code)
51
            ->setParameter('localeCode', $localeCode)
52
            ->getQuery()
53
            ->getOneOrNullResult()
54
        ;
55
    }
56
57
    public function findOneEnabledBySlugAndChannelCode(
58
        string $slug,
59
        ?string $localeCode,
60
        string $channelCode
61
    ): ?PageInterface {
62
        return $this->createQueryBuilder('o')
63
            ->leftJoin('o.translations', 'translation')
64
            ->innerJoin('o.channels', 'channels')
65
            ->where('translation.locale = :localeCode')
66
            ->andWhere('translation.slug = :slug')
67
            ->andWhere('channels.code = :channelCode')
68
            ->andWhere('o.enabled = true')
69
            ->setParameter('localeCode', $localeCode)
70
            ->setParameter('slug', $slug)
71
            ->setParameter('channelCode', $channelCode)
72
            ->getQuery()
73
            ->getOneOrNullResult()
74
        ;
75
    }
76
77
    public function createShopListQueryBuilder(string $sectionCode, string $channelCode): QueryBuilder
78
    {
79
        return $this->createQueryBuilder('o')
80
            ->innerJoin('o.sections', 'section')
81
            ->innerJoin('o.channels', 'channels')
82
            ->where('section.code = :sectionCode')
83
            ->andWhere('o.enabled = true')
84
            ->andWhere('channels.code = :channelCode')
85
            ->setParameter('sectionCode', $sectionCode)
86
            ->setParameter('channelCode', $channelCode)
87
        ;
88
    }
89
90
    public function findBySectionCode(string $sectionCode, ?string $localeCode): array
91
    {
92
        return $this->createQueryBuilder('o')
93
            ->leftJoin('o.translations', 'translation')
94
            ->innerJoin('o.sections', 'section')
95
            ->where('translation.locale = :localeCode')
96
            ->andWhere('section.code = :sectionCode')
97
            ->andWhere('o.enabled = true')
98
            ->setParameter('sectionCode', $sectionCode)
99
            ->setParameter('localeCode', $localeCode)
100
            ->getQuery()
101
            ->getResult()
102
        ;
103
    }
104
105
    public function findByProduct(ProductInterface $product, string $channelCode): array
106
    {
107
        return $this->createQueryBuilder('o')
108
            ->innerJoin('o.products', 'product')
109
            ->innerJoin('o.channels', 'channel')
110
            ->where('o.enabled = true')
111
            ->andWhere('product = :product')
112
            ->andWhere('channel.code = :channelCode')
113
            ->setParameter('product', $product)
114
            ->setParameter('channelCode', $channelCode)
115
            ->getQuery()
116
            ->getResult()
117
        ;
118
    }
119
120
    public function findByProductAndSectionCode(
121
        ProductInterface $product,
122
        string $sectionCode,
123
        string $channelCode
124
    ): array {
125
        return $this->createQueryBuilder('o')
126
            ->innerJoin('o.products', 'product')
127
            ->innerJoin('o.sections', 'section')
128
            ->innerJoin('o.channels', 'channel')
129
            ->where('o.enabled = true')
130
            ->andWhere('product = :product')
131
            ->andWhere('section.code = :sectionCode')
132
            ->andWhere('channel.code = :channelCode')
133
            ->setParameter('product', $product)
134
            ->setParameter('sectionCode', $sectionCode)
135
            ->setParameter('channelCode', $channelCode)
136
            ->getQuery()
137
            ->getResult()
138
        ;
139
    }
140
}
141