Completed
Push — missing-product-variant-variab... ( a08960 )
by Kamil
22:45
created

ProductVariantRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createQueryBuilderByProductId() 0 10 1
A createQueryBuilderByProductCode() 0 11 1
A findByName() 0 12 1
A findByNameAndProduct() 0 14 1
A findOneByCodeAndProductCode() 0 12 1
A findByCodeAndProductCode() 0 12 1
A findOneByIdAndProductId() 0 11 1
A findByPhraseAndProductCode() 0 19 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\ProductBundle\Doctrine\ORM;
13
14
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
15
use Sylius\Component\Product\Model\ProductInterface;
16
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
17
18
/**
19
 * @author Alexandre Bacco <[email protected]>
20
 */
21
class ProductVariantRepository extends EntityRepository implements ProductVariantRepositoryInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function createQueryBuilderByProductId($locale, $productId)
27
    {
28
        return $this->createQueryBuilder('o')
29
            ->innerJoin('o.translations', 'translation')
30
            ->andWhere('translation.locale = :locale')
31
            ->andWhere('o.product = :productId')
32
            ->setParameter('locale', $locale)
33
            ->setParameter('productId', $productId)
34
        ;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function createQueryBuilderByProductCode($locale, $productCode)
41
    {
42
        return $this->createQueryBuilder('o')
43
            ->innerJoin('o.translations', 'translation')
44
            ->innerJoin('o.product', 'product')
45
            ->andWhere('translation.locale = :locale')
46
            ->andWhere('product.code = :productCode')
47
            ->setParameter('locale', $locale)
48
            ->setParameter('productCode', $productCode)
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function findByName($name, $locale)
56
    {
57
        return $this->createQueryBuilder('o')
58
            ->innerJoin('o.translations', 'translation')
59
            ->andWhere('translation.name = :name')
60
            ->andWhere('translation.locale = :locale')
61
            ->setParameter('name', $name)
62
            ->setParameter('locale', $locale)
63
            ->getQuery()
64
            ->getResult()
65
        ;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function findByNameAndProduct($name, $locale, ProductInterface $product)
72
    {
73
        return $this->createQueryBuilder('o')
74
            ->innerJoin('o.translations', 'translation')
75
            ->andWhere('translation.name = :name')
76
            ->andWhere('translation.locale = :locale')
77
            ->andWhere('o.product = :product')
78
            ->setParameter('name', $name)
79
            ->setParameter('locale', $locale)
80
            ->setParameter('product', $product)
81
            ->getQuery()
82
            ->getResult()
83
        ;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function findOneByCodeAndProductCode($code, $productCode)
90
    {
91
        return $this->createQueryBuilder('o')
92
            ->innerJoin('o.product', 'product')
93
            ->andWhere('product.code = :productCode')
94
            ->andWhere('o.code = :code')
95
            ->setParameter('productCode', $productCode)
96
            ->setParameter('code', $code)
97
            ->getQuery()
98
            ->getOneOrNullResult()
99
        ;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function findByCodeAndProductCode($code, $productCode)
106
    {
107
        return $this->createQueryBuilder('o')
108
            ->innerJoin('o.product', 'product')
109
            ->andWhere('product.code = :productCode')
110
            ->andWhere('o.code = :code')
111
            ->setParameter('productCode', $productCode)
112
            ->setParameter('code', $code)
113
            ->getQuery()
114
            ->getResult()
115
        ;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function findOneByIdAndProductId($id, $productId)
122
    {
123
        return $this->createQueryBuilder('o')
124
            ->andWhere('o.product = :productId')
125
            ->andWhere('o.id = :id')
126
            ->setParameter('productId', $productId)
127
            ->setParameter('id', $id)
128
            ->getQuery()
129
            ->getOneOrNullResult()
130
        ;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function findByPhraseAndProductCode($phrase, $locale, $productCode)
137
    {
138
        $expr = $this->getEntityManager()->getExpressionBuilder();
139
140
        return $this->createQueryBuilder('o')
141
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
142
            ->innerJoin('o.product', 'product')
143
            ->andWhere('product.code = :productCode')
144
            ->andWhere($expr->orX(
145
                'translation.name LIKE :phrase',
146
                'o.code LIKE :phrase'
147
            ))
148
            ->setParameter('phrase', '%'.$phrase.'%')
149
            ->setParameter('locale', $locale)
150
            ->setParameter('productCode', $productCode)
151
            ->getQuery()
152
            ->getResult()
153
        ;
154
    }
155
}
156