Completed
Push — product-browsing ( 842ff0 )
by Kamil
23:49
created

createQueryBuilderByChannelAndTaxonSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 3
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 Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
15
use Sylius\Component\Core\Model\ChannelInterface;
16
use Sylius\Component\Core\Model\TaxonInterface;
17
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 * @author Gonzalo Vilaseca <[email protected]>
22
 */
23
class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function createListQueryBuilder($locale, $taxonId = null)
29
    {
30
        $queryBuilder = $this->createQueryBuilder('o')
31
            ->addSelect('translation')
32
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
33
            ->setParameter('locale', $locale)
34
        ;
35
36
        if (null !== $taxonId) {
37
            $queryBuilder
38
                ->innerJoin('o.productTaxons', 'productTaxon')
39
                ->andWhere('productTaxon.taxon = :taxonId')
40
                ->setParameter('taxonId', $taxonId)
41
            ;
42
        }
43
44
        return $queryBuilder;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function createQueryBuilderByChannelAndTaxon(ChannelInterface $channel, TaxonInterface $taxon, $locale)
51
    {
52
        return $this->createQueryBuilder('o')
53
            ->addSelect('translation')
54
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
55
            ->innerJoin('o.productTaxons', 'productTaxon')
56
            ->andWhere('productTaxon.taxon = :taxon')
57
            ->andWhere(':channel MEMBER OF o.channels')
58
            ->andWhere('o.enabled = true')
59
            ->addGroupBy('o.id')
60
            ->setParameter('locale', $locale)
61
            ->setParameter('taxon', $taxon)
62
            ->setParameter('channel', $channel)
63
        ;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function findLatestByChannel(ChannelInterface $channel, $count)
70
    {
71
        return $this->createQueryBuilder('o')
72
            ->andWhere(':channel MEMBER OF o.channels')
73
            ->andWhere('o.enabled = true')
74
            ->addOrderBy('o.createdAt', 'DESC')
75
            ->setParameter('channel', $channel)
76
            ->setMaxResults($count)
77
            ->getQuery()
78
            ->getResult()
79
        ;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function findOneBySlugAndChannel($slug, ChannelInterface $channel)
86
    {
87
        return $this->createQueryBuilder('o')
88
            ->innerJoin('o.translations', 'translation')
89
            ->andWhere('translation.slug = :slug')
90
            ->andWhere(':channel MEMBER OF o.channels')
91
            ->andWhere('o.enabled = true')
92
            ->setParameter('slug', $slug)
93
            ->setParameter('channel', $channel)
94
            ->getQuery()
95
            ->getOneOrNullResult()
96
        ;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function findOneBySlug($slug)
103
    {
104
        return $this->createQueryBuilder('o')
105
            ->innerJoin('o.translations', 'translation')
106
            ->andWhere('translation.slug = :slug')
107
            ->andWhere('o.enabled = true')
108
            ->setParameter('slug', $slug)
109
            ->getQuery()
110
            ->getOneOrNullResult()
111
        ;
112
    }
113
}
114