Completed
Push — master ( c66308...141223 )
by Kamil
28:38 queued 09:41
created

ProductRepository::findOneByIdAndChannel()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 18
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 findEnabledByTaxonCodeAndChannel($code, ChannelInterface $channel)
92
    {
93
        return $this->createQueryBuilder('o')
94
            ->innerJoin('o.taxons', 'taxon')
95
            ->andWhere('taxon.code = :code')
96
            ->innerJoin('o.channels', 'channel')
97
            ->andWhere('channel = :channel')
98
            ->andWhere('o.enabled = true')
99
            ->setParameter('code', $code)
100
            ->setParameter('channel', $channel)
101
            ->getQuery()
102
            ->getResult()
103
        ;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function findOneBySlugAndChannel($slug, ChannelInterface $channel)
110
    {
111
        return $this->createQueryBuilder('o')
112
            ->leftJoin('o.translations', 'translation')
113
            ->innerJoin('o.channels', 'channel')
114
            ->andWhere('channel = :channel')
115
            ->andWhere('o.enabled = true')
116
            ->andWhere('translation.slug = :slug')
117
            ->setParameter('slug', $slug)
118
            ->setParameter('channel', $channel)
119
            ->getQuery()
120
            ->getOneOrNullResult()
121
        ;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function findOneBySlug($slug)
128
    {
129
        return $this->createQueryBuilder('o')
130
            ->leftJoin('o.translations', 'translation')
131
            ->andWhere('o.enabled = true')
132
            ->andWhere('translation.slug = :slug')
133
            ->setParameter('slug', $slug)
134
            ->getQuery()
135
            ->getOneOrNullResult()
136
        ;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = null)
143
    {
144
        if (isset($criteria['channels'])) {
145
            $queryBuilder
146
                ->innerJoin('o.channels', 'channel')
147
                ->andWhere('channel = :channel')
148
                ->setParameter('channel', $criteria['channels'])
149
            ;
150
            unset($criteria['channels']);
151
        }
152
153
        parent::applyCriteria($queryBuilder, $criteria);
154
    }
155
}
156