Completed
Pull Request — master (#124)
by
unknown
01:38
created

ProductRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findByNamePart() 0 9 1
A createTranslationBasedQueryBuilder() 0 16 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Repository;
14
15
use Doctrine\ORM\QueryBuilder;
16
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
17
18
class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function findByNamePart(string $phrase, ?string $locale = null): array
24
    {
25
        return $this->createTranslationBasedQueryBuilder($locale)
26
            ->andWhere('translation.name LIKE :name')
27
            ->setParameter('name', '%' . $phrase . '%')
28
            ->getQuery()
29
            ->getResult()
30
        ;
31
    }
32
33
    /**
34
     * @param $locale
35
     *
36
     * @return QueryBuilder
37
     */
38
    private function createTranslationBasedQueryBuilder($locale): QueryBuilder
39
    {
40
        $queryBuilder = $this->createQueryBuilder('o')
41
            ->addSelect('translation')
42
            ->leftJoin('o.translations', 'translation')
43
        ;
44
45
        if (null !== $locale) {
46
            $queryBuilder
47
                ->andWhere('translation.locale = :locale')
48
                ->setParameter('locale', $locale)
49
            ;
50
        }
51
52
        return $queryBuilder;
53
    }
54
}
55