PageRepository::findEnabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Repository;
12
13
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
16
use Sylius\Component\Core\Model\ProductInterface;
17
18
class PageRepository extends EntityRepository implements PageRepositoryInterface
19
{
20
    public function createListQueryBuilder(string $localeCode): QueryBuilder
21
    {
22
        return $this->createQueryBuilder('o')
23
            ->addSelect('translation')
24
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
25
            ->leftJoin('o.sections', 'sections')
26
            ->setParameter('localeCode', $localeCode)
27
        ;
28
    }
29
30
    public function findEnabled(bool $enabled): array
31
    {
32
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
33
            ->addSelect('translation')
34
            ->innerJoin('o.translations', 'translation')
35
            ->andWhere('o.enabled = :enabled')
36
            ->setParameter('enabled', $enabled)
37
            ->getQuery()
38
            ->getResult()
39
        ;
40
    }
41
42
    public function findOneEnabledByCode(string $code, ?string $localeCode): ?PageInterface
43
    {
44
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return BitBag\SyliusCmsPlugin\Entity\PageInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
45
            ->leftJoin('o.translations', 'translation')
46
            ->where('translation.locale = :localeCode')
47
            ->andWhere('o.code = :code')
48
            ->andWhere('o.enabled = true')
49
            ->setParameter('code', $code)
50
            ->setParameter('localeCode', $localeCode)
51
            ->getQuery()
52
            ->getOneOrNullResult()
53
        ;
54
    }
55
56
    public function findOneEnabledBySlugAndChannelCode(
57
        string $slug,
58
        ?string $localeCode,
59
        string $channelCode
60
    ): ?PageInterface {
61
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return BitBag\SyliusCmsPlugin\Entity\PageInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
62
            ->leftJoin('o.translations', 'translation')
63
            ->innerJoin('o.channels', 'channels')
64
            ->where('translation.locale = :localeCode')
65
            ->andWhere('translation.slug = :slug')
66
            ->andWhere('channels.code = :channelCode')
67
            ->andWhere('o.enabled = true')
68
            ->setParameter('localeCode', $localeCode)
69
            ->setParameter('slug', $slug)
70
            ->setParameter('channelCode', $channelCode)
71
            ->getQuery()
72
            ->getOneOrNullResult()
73
        ;
74
    }
75
76
    public function createShopListQueryBuilder(string $sectionCode, string $channelCode): QueryBuilder
77
    {
78
        return $this->createQueryBuilder('o')
79
            ->innerJoin('o.sections', 'section')
80
            ->innerJoin('o.channels', 'channels')
81
            ->where('section.code = :sectionCode')
82
            ->andWhere('o.enabled = true')
83
            ->andWhere('channels.code = :channelCode')
84
            ->setParameter('sectionCode', $sectionCode)
85
            ->setParameter('channelCode', $channelCode)
86
        ;
87
    }
88
89
    public function findBySectionCode(string $sectionCode, ?string $localeCode): array
90
    {
91
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
92
            ->leftJoin('o.translations', 'translation')
93
            ->innerJoin('o.sections', 'section')
94
            ->where('translation.locale = :localeCode')
95
            ->andWhere('section.code = :sectionCode')
96
            ->andWhere('o.enabled = true')
97
            ->setParameter('sectionCode', $sectionCode)
98
            ->setParameter('localeCode', $localeCode)
99
            ->getQuery()
100
            ->getResult()
101
        ;
102
    }
103
104
    public function findByProduct(
105
        ProductInterface $product,
106
        string $channelCode,
107
        ?\DateTimeInterface $date = null
108
    ): array {
109
        $qb = $this->createQueryBuilder('o')
110
            ->innerJoin('o.products', 'product')
111
            ->innerJoin('o.channels', 'channel')
112
            ->where('o.enabled = true')
113
            ->andWhere('product = :product')
114
            ->andWhere('channel.code = :channelCode')
115
            ->setParameter('product', $product)
116
            ->setParameter('channelCode', $channelCode)
117
        ;
118
119
        if (null !== $date) {
120
            $this->addDateFilter($qb, $date);
121
        }
122
123
        return $qb
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
124
            ->getQuery()
125
            ->getResult()
126
        ;
127
    }
128
129
    public function findByProductAndSectionCode(
130
        ProductInterface $product,
131
        string $sectionCode,
132
        string $channelCode,
133
        ?\DateTimeInterface $date = null
134
    ): array {
135
        $qb = $this->createQueryBuilder('o')
136
            ->innerJoin('o.products', 'product')
137
            ->innerJoin('o.sections', 'section')
138
            ->innerJoin('o.channels', 'channel')
139
            ->where('o.enabled = true')
140
            ->andWhere('product = :product')
141
            ->andWhere('section.code = :sectionCode')
142
            ->andWhere('channel.code = :channelCode')
143
            ->setParameter('product', $product)
144
            ->setParameter('sectionCode', $sectionCode)
145
            ->setParameter('channelCode', $channelCode)
146
        ;
147
148
        if (null !== $date) {
149
            $this->addDateFilter($qb, $date);
150
        }
151
152
        return $qb->getQuery()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
153
            ->getResult()
154
        ;
155
    }
156
157
    private function addDateFilter(QueryBuilder $qb, \DateTimeInterface $date): void
158
    {
159
        $qb
160
            ->andWhere(
161
                $qb->expr()->orX(
162
                    'o.publishAt is NULL',
163
                    'o.publishAt <= :date'
164
                )
165
            )
166
            ->setParameter('date', $date);
167
    }
168
}
169