SectionRepository::createListQueryBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Repository;
12
13
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
16
17
class SectionRepository extends EntityRepository implements SectionRepositoryInterface
18
{
19
    public function createListQueryBuilder(string $localeCode): QueryBuilder
20
    {
21
        return $this->createQueryBuilder('o')
22
            ->addSelect('translation')
23
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
24
            ->setParameter('localeCode', $localeCode)
25
        ;
26
    }
27
28
    public function findByNamePart(string $phrase, ?string $locale = null): array
29
    {
30
        return $this->createTranslationBasedQueryBuilder($locale)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createTran...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
31
            ->andWhere('translation.name LIKE :name')
32
            ->setParameter('name', '%' . $phrase . '%')
33
            ->getQuery()
34
            ->getResult()
35
        ;
36
    }
37
38
    private function createTranslationBasedQueryBuilder(?string $locale = null): 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
    public function findOneByCode(string $code, ?string $localeCode): ?SectionInterface
56
    {
57
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return BitBag\SyliusCmsPlugin\E...y\SectionInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
58
            ->leftJoin('o.translations', 'translation')
59
            ->where('translation.locale = :localeCode')
60
            ->andWhere('o.code = :code')
61
            ->setParameter('code', $code)
62
            ->setParameter('localeCode', $localeCode)
63
            ->getQuery()
64
            ->getOneOrNullResult()
65
        ;
66
    }
67
68
    public function findByCodesAndLocale(string $codes, string $localeCode): array
69
    {
70
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
71
            ->leftJoin('o.translations', 'translation')
72
            ->where('translation.locale = :localeCode')
73
            ->andWhere('o.code IN(:codes)')
74
            ->setParameter('codes', explode(',', $codes))
75
            ->setParameter('localeCode', $localeCode)
76
            ->getQuery()
77
            ->getResult()
78
            ;
79
    }
80
}
81