Completed
Push — product-show-optimisation ( fda015 )
by Kamil
20:51
created

ProductRepository::findOneByCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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\EntityManager;
15
use Doctrine\ORM\Mapping;
16
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
17
use Sylius\Component\Core\Model\ChannelInterface;
18
use Sylius\Component\Core\Model\TaxonInterface;
19
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
20
21
/**
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 * @author Gonzalo Vilaseca <[email protected]>
24
 */
25
class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface
26
{
27
    /**
28
     * @var AssociationHydrator
29
     */
30
    private $associationHydrator;
31
32
    /**
33
     * @param EntityManager $em
34
     * @param Mapping\ClassMetadata $class
35
     */
36
    public function __construct(EntityManager $em, Mapping\ClassMetadata $class)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
37
    {
38
        parent::__construct($em, $class);
39
40
        $this->associationHydrator = new AssociationHydrator($em, $class);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function createListQueryBuilder($locale, $taxonId = null)
47
    {
48
        $queryBuilder = $this->createQueryBuilder('o')
49
            ->addSelect('translation')
50
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
51
            ->setParameter('locale', $locale)
52
        ;
53
54
        if (null !== $taxonId) {
55
            $queryBuilder
56
                ->innerJoin('o.productTaxons', 'productTaxon')
57
                ->andWhere('productTaxon.taxon = :taxonId')
58
                ->setParameter('taxonId', $taxonId)
59
            ;
60
        }
61
62
        return $queryBuilder;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function createShopListQueryBuilder(ChannelInterface $channel, TaxonInterface $taxon, $locale, array $sorting)
69
    {
70
        $queryBuilder = $this->createQueryBuilder('o')
71
            ->addSelect('translation')
72
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
73
            ->innerJoin('o.productTaxons', 'productTaxon')
74
            ->andWhere('productTaxon.taxon = :taxon')
75
            ->andWhere(':channel MEMBER OF o.channels')
76
            ->andWhere('o.enabled = true')
77
            ->addGroupBy('o.id')
78
            ->setParameter('locale', $locale)
79
            ->setParameter('taxon', $taxon)
80
            ->setParameter('channel', $channel)
81
        ;
82
83
        // Grid hack, we do not need to join these if we don't sort by price
84
        if (isset($sorting['price'])) {
85
            $queryBuilder
86
                ->innerJoin('o.variants', 'variant')
87
                ->innerJoin('variant.channelPricings', 'channelPricing')
88
                ->andWhere('channelPricing.channel = :channel')
89
            ;
90
        }
91
92
        return $queryBuilder;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function findLatestByChannel(ChannelInterface $channel, $locale, $count)
99
    {
100
        return $this->createQueryBuilder('o')
101
            ->addSelect('translation')
102
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
103
            ->andWhere(':channel MEMBER OF o.channels')
104
            ->andWhere('o.enabled = true')
105
            ->addOrderBy('o.createdAt', 'DESC')
106
            ->setParameter('channel', $channel)
107
            ->setParameter('locale', $locale)
108
            ->setMaxResults($count)
109
            ->getQuery()
110
            ->getResult()
111
        ;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function findOneByChannelAndSlug(ChannelInterface $channel, $locale, $slug)
118
    {
119
        $product = $this->createQueryBuilder('o')
120
            ->addSelect('translation')
121
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
122
            ->andWhere('translation.slug = :slug')
123
            ->andWhere(':channel MEMBER OF o.channels')
124
            ->andWhere('o.enabled = true')
125
            ->setParameter('channel', $channel)
126
            ->setParameter('locale', $locale)
127
            ->setParameter('slug', $slug)
128
            ->getQuery()
129
            ->getOneOrNullResult()
130
        ;
131
132
        $this->associationHydrator->hydrateAssociations($product, [
133
            'images',
134
            'options',
135
            'options.translations',
136
            'variants.channelPricings',
137
            'variants.optionValues',
138
            'variants.optionValues.translations',
139
        ]);
140
141
        return $product;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function findOneByCode($code)
148
    {
149
        return $this->createQueryBuilder('o')
150
            ->where('o.code = :code')
151
            ->setParameter('code', $code)
152
            ->getQuery()
153
            ->getOneOrNullResult()
154
        ;
155
    }
156
}
157