Completed
Pull Request — master (#123)
by
unknown
01:41
created

createTranslationBasedQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
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\ResourceBundle\Doctrine\ORM\EntityRepository;
17
18
class SectionRepository extends EntityRepository implements SectionRepositoryInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function createListQueryBuilder(): QueryBuilder
24
    {
25
        return $this->createQueryBuilder('o')
26
            ->leftJoin('o.translations', 'translation')
27
        ;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function findByNamePart(string $phrase, ?string $locale = null): array
34
    {
35
        return $this->createTranslationBasedQueryBuilder($locale)
36
            ->andWhere('translation.name LIKE :name')
37
            ->setParameter('name', '%' . $phrase . '%')
38
            ->getQuery()
39
            ->getResult()
40
        ;
41
    }
42
43
    /**
44
     * @param $locale
45
     *
46
     * @return QueryBuilder
47
     */
48
    private function createTranslationBasedQueryBuilder($locale): QueryBuilder
49
    {
50
        $queryBuilder = $this->createQueryBuilder('o')
51
            ->leftJoin('o.translations', 'translation')
52
        ;
53
54
        if (null !== $locale) {
55
            $queryBuilder
56
                ->andWhere('translation.locale = :locale')
57
                ->setParameter('locale', $locale)
58
            ;
59
        }
60
61
        return $queryBuilder;
62
    }
63
}
64