Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
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 createQueryBuilderForEnabledByTaxonCodeAndChannelAndLocale($code, ChannelInterface $channel, $locale)
55
    {
56
        return $this->createQueryBuilder('o')
57
            ->addSelect('translation')
58
            ->leftJoin('o.translations', 'translation')
59
            ->innerJoin('o.taxons', 'taxon')
60
            ->innerJoin('o.channels', 'channel')
61
            ->andWhere('translation.locale = :locale')
62
            ->andWhere('taxon.code = :code')
63
            ->andWhere('channel = :channel')
64
            ->andWhere('o.enabled = true')
65
            ->setParameter('locale', $locale)
66
            ->setParameter('code', $code)
67
            ->setParameter('channel', $channel)
68
        ;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function findLatestByChannel(ChannelInterface $channel, $count)
75
    {
76
        return $this->createQueryBuilder('o')
77
            ->innerJoin('o.channels', 'channel')
78
            ->addOrderBy('o.createdAt', 'desc')
79
            ->andWhere('o.enabled = true')
80
            ->andWhere('channel = :channel')
81
            ->setParameter('channel', $channel)
82
            ->setMaxResults($count)
83
            ->getQuery()
84
            ->getResult()
85
        ;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function findOneByIdAndChannel($id, ChannelInterface $channel = null)
92
    {
93
        $queryBuilder = $this->createQueryBuilder('o')
94
            ->addSelect('image')
95
            ->select('o, option, variant')
96
            ->leftJoin('o.options', 'option')
97
            ->leftJoin('o.variants', 'variant')
98
            ->leftJoin('variant.images', 'image')
99
            ->innerJoin('o.channels', 'channel')
100
        ;
101
102
        $queryBuilder
103
            ->andWhere($queryBuilder->expr()->eq('o.id', ':id'))
104
            ->setParameter('id', $id)
105
        ;
106
107
        if (null !== $channel) {
108
            $queryBuilder
109
                ->andWhere('channel = :channel')
110
                ->setParameter('channel', $channel);
111
        }
112
113
        return $queryBuilder
114
            ->getQuery()
115
            ->getOneOrNullResult()
116
        ;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function findEnabledByTaxonCodeAndChannel($code, ChannelInterface $channel)
123
    {
124
        return $this->createQueryBuilder('o')
125
            ->innerJoin('o.taxons', 'taxon')
126
            ->andWhere('taxon.code = :code')
127
            ->innerJoin('o.channels', 'channel')
128
            ->andWhere('channel = :channel')
129
            ->andWhere('o.enabled = true')
130
            ->setParameter('code', $code)
131
            ->setParameter('channel', $channel)
132
            ->getQuery()
133
            ->getResult()
134
        ;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function findOneBySlugAndChannel($slug, ChannelInterface $channel)
141
    {
142
        return $this->createQueryBuilder('o')
143
            ->leftJoin('o.translations', 'translation')
144
            ->innerJoin('o.channels', 'channel')
145
            ->andWhere('channel = :channel')
146
            ->andWhere('o.enabled = true')
147
            ->andWhere('translation.slug = :slug')
148
            ->setParameter('slug', $slug)
149
            ->setParameter('channel', $channel)
150
            ->getQuery()
151
            ->getOneOrNullResult()
152
        ;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function findOneBySlug($slug)
159
    {
160
        return $this->createQueryBuilder('o')
161
            ->leftJoin('o.translations', 'translation')
162
            ->andWhere('o.enabled = true')
163
            ->andWhere('translation.slug = :slug')
164
            ->setParameter('slug', $slug)
165
            ->getQuery()
166
            ->getOneOrNullResult()
167
        ;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = null)
174
    {
175
        if (isset($criteria['channels'])) {
176
            $queryBuilder
177
                ->innerJoin('o.channels', 'channel')
178
                ->andWhere('channel = :channel')
179
                ->setParameter('channel', $criteria['channels'])
180
            ;
181
            unset($criteria['channels']);
182
        }
183
184
        parent::applyCriteria($queryBuilder, $criteria);
185
    }
186
}
187