Completed
Push — master ( 23b980...7c13a2 )
by Paweł
36:20 queued 26:12
created

ProductRepository::findOneByChannelAndSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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 createShopListQueryBuilder(ChannelInterface $channel, TaxonInterface $taxon, $locale, array $sorting)
51
    {
52
        $queryBuilder = $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
        // Grid hack, we do not need to join these if we don't sort by price
66
        if (isset($sorting['price'])) {
67
            $queryBuilder
68
                ->innerJoin('o.variants', 'variant')
69
                ->innerJoin('variant.channelPricings', 'channelPricing')
70
                ->andWhere('channelPricing.channel = :channel')
71
            ;
72
        }
73
74
        return $queryBuilder;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function findLatestByChannel(ChannelInterface $channel, $locale, $count)
81
    {
82
        return $this->createQueryBuilder('o')
83
            ->addSelect('translation')
84
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
85
            ->andWhere(':channel MEMBER OF o.channels')
86
            ->andWhere('o.enabled = true')
87
            ->addOrderBy('o.createdAt', 'DESC')
88
            ->setParameter('channel', $channel)
89
            ->setParameter('locale', $locale)
90
            ->setMaxResults($count)
91
            ->getQuery()
92
            ->getResult()
93
        ;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function findOneByChannelAndSlug(ChannelInterface $channel, $locale, $slug)
100
    {
101
        return $this->createQueryBuilder('o')
102
            ->addSelect('translation')
103
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
104
            ->andWhere('translation.slug = :slug')
105
            ->andWhere(':channel MEMBER OF o.channels')
106
            ->andWhere('o.enabled = true')
107
            ->setParameter('channel', $channel)
108
            ->setParameter('locale', $locale)
109
            ->setParameter('slug', $slug)
110
            ->getQuery()
111
            ->getOneOrNullResult()
112
        ;
113
    }
114
}
115