Completed
Push — master ( 85508a...fbb5f8 )
by Kamil
24:40
created

createQueryBuilderWithLocaleCodeAndTaxonId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 2
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
namespace Sylius\Bundle\CoreBundle\Doctrine\ORM;
13
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Sylius\Component\Core\Model\TaxonInterface;
18
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 * @author Gonzalo Vilaseca <[email protected]>
23
 */
24
class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createQueryBuilderWithLocaleCodeAndTaxonId($localeCode, $taxonId = null)
30
    {
31
        $queryBuilder = $this->createQueryBuilder('o');
32
33
        $queryBuilder
34
            ->addSelect('translation')
35
            ->leftJoin('o.translations', 'translation')
36
            ->andWhere('translation.locale = :localeCode')
37
            ->setParameter('localeCode', $localeCode)
38
        ;
39
40
        if (null !== $taxonId) {
41
            $queryBuilder
42
                ->innerJoin('o.taxons', 'taxon')
43
                ->andWhere('taxon.id = :taxonId')
44
                ->setParameter('taxonId', $taxonId)
45
            ;
46
        }
47
48
        return $queryBuilder;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createQueryBuilderForEnabledByTaxonCodeAndChannel($code, ChannelInterface $channel)
55
    {
56
        return $this->createQueryBuilder('o')
57
            ->innerJoin('o.taxons', 'taxon')
58
            ->andWhere('taxon.code = :code')
59
            ->innerJoin('o.channels', 'channel')
60
            ->andWhere('channel = :channel')
61
            ->andWhere('o.enabled = true')
62
            ->setParameter('code', $code)
63
            ->setParameter('channel', $channel)
64
        ;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function findLatestByChannel(ChannelInterface $channel, $count)
71
    {
72
        return $this->createQueryBuilder('o')
73
            ->innerJoin('o.channels', 'channel')
74
            ->addOrderBy('o.createdAt', 'desc')
75
            ->andWhere('o.enabled = true')
76
            ->andWhere('channel = :channel')
77
            ->setParameter('channel', $channel)
78
            ->setMaxResults($count)
79
            ->getQuery()
80
            ->getResult()
81
        ;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function findOneByIdAndChannel($id, ChannelInterface $channel = null)
88
    {
89
        $queryBuilder = $this->createQueryBuilder('o')
90
            ->addSelect('image')
91
            ->select('o, option, variant')
92
            ->leftJoin('o.options', 'option')
93
            ->leftJoin('o.variants', 'variant')
94
            ->leftJoin('variant.images', 'image')
95
            ->innerJoin('o.channels', 'channel')
96
        ;
97
98
        $queryBuilder
99
            ->andWhere($queryBuilder->expr()->eq('o.id', ':id'))
100
            ->setParameter('id', $id)
101
        ;
102
103
        if (null !== $channel) {
104
            $queryBuilder
105
                ->andWhere('channel = :channel')
106
                ->setParameter('channel', $channel);
107
        }
108
109
        return $queryBuilder
110
            ->getQuery()
111
            ->getOneOrNullResult()
112
        ;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function findEnabledByTaxonCodeAndChannel($code, ChannelInterface $channel)
119
    {
120
        return $this->createQueryBuilder('o')
121
            ->innerJoin('o.taxons', 'taxon')
122
            ->andWhere('taxon.code = :code')
123
            ->innerJoin('o.channels', 'channel')
124
            ->andWhere('channel = :channel')
125
            ->andWhere('o.enabled = true')
126
            ->setParameter('code', $code)
127
            ->setParameter('channel', $channel)
128
            ->getQuery()
129
            ->getResult()
130
        ;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function findOneBySlugAndChannel($slug, ChannelInterface $channel)
137
    {
138
        return $this->createQueryBuilder('o')
139
            ->leftJoin('o.translations', 'translation')
140
            ->innerJoin('o.channels', 'channel')
141
            ->andWhere('channel = :channel')
142
            ->andWhere('o.enabled = true')
143
            ->andWhere('translation.slug = :slug')
144
            ->setParameter('slug', $slug)
145
            ->setParameter('channel', $channel)
146
            ->getQuery()
147
            ->getOneOrNullResult()
148
        ;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function findOneBySlug($slug)
155
    {
156
        return $this->createQueryBuilder('o')
157
            ->leftJoin('o.translations', 'translation')
158
            ->andWhere('o.enabled = true')
159
            ->andWhere('translation.slug = :slug')
160
            ->setParameter('slug', $slug)
161
            ->getQuery()
162
            ->getOneOrNullResult()
163
        ;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = null)
170
    {
171
        if (isset($criteria['channels'])) {
172
            $queryBuilder
173
                ->innerJoin('o.channels', 'channel')
174
                ->andWhere('channel = :channel')
175
                ->setParameter('channel', $criteria['channels'])
176
            ;
177
            unset($criteria['channels']);
178
        }
179
180
        parent::applyCriteria($queryBuilder, $criteria);
181
    }
182
}
183