Completed
Push — master ( a09c02...265d88 )
by Mikołaj
02:00
created

SectionRepository::findByCodesAndLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 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 BitBag\SyliusCmsPlugin\Entity\SectionInterface;
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
19
class SectionRepository extends EntityRepository implements SectionRepositoryInterface
20
{
21
    public function createListQueryBuilder(string $localeCode): QueryBuilder
22
    {
23
        return $this->createQueryBuilder('o')
24
            ->addSelect('translation')
25
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
26
            ->setParameter('localeCode', $localeCode)
27
        ;
28
    }
29
30
    public function findByNamePart(string $phrase, ?string $locale = null): array
31
    {
32
        return $this->createTranslationBasedQueryBuilder($locale)
33
            ->andWhere('translation.name LIKE :name')
34
            ->setParameter('name', '%' . $phrase . '%')
35
            ->getQuery()
36
            ->getResult()
37
        ;
38
    }
39
40
    private function createTranslationBasedQueryBuilder(?string $locale = null): QueryBuilder
41
    {
42
        $queryBuilder = $this->createQueryBuilder('o')
43
            ->addSelect('translation')
44
            ->leftJoin('o.translations', 'translation')
45
        ;
46
47
        if (null !== $locale) {
48
            $queryBuilder
49
                ->andWhere('translation.locale = :locale')
50
                ->setParameter('locale', $locale)
51
            ;
52
        }
53
54
        return $queryBuilder;
55
    }
56
57
    public function findOneByCode(string $code, ?string $localeCode): ?SectionInterface
58
    {
59
        return $this->createQueryBuilder('o')
60
            ->leftJoin('o.translations', 'translation')
61
            ->where('translation.locale = :localeCode')
62
            ->andWhere('o.code = :code')
63
            ->setParameter('code', $code)
64
            ->setParameter('localeCode', $localeCode)
65
            ->getQuery()
66
            ->getOneOrNullResult()
67
        ;
68
    }
69
70
    public function findByCodesAndLocale(string $codes, string $localeCode): array
71
    {
72
        return $this->createQueryBuilder('o')
73
            ->leftJoin('o.translations', 'translation')
74
            ->where('translation.locale = :localeCode')
75
            ->andWhere('o.code IN(:codes)')
76
            ->setParameter('codes', explode(',', $codes))
77
            ->setParameter('localeCode', $localeCode)
78
            ->getQuery()
79
            ->getResult()
80
            ;
81
    }
82
}
83