Completed
Pull Request — master (#142)
by
unknown
07:20
created

SectionRepository::findOneByCodeAndChannelCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 3
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
    /**
22
     * {@inheritdoc}
23
     */
24
    public function createListQueryBuilder(): QueryBuilder
25
    {
26
        return $this->createQueryBuilder('o')
27
            ->leftJoin('o.translations', 'translation')
28
        ;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function findByNamePart(string $phrase, ?string $locale = null): array
35
    {
36
        return $this->createTranslationBasedQueryBuilder($locale)
37
            ->andWhere('translation.name LIKE :name')
38
            ->setParameter('name', '%' . $phrase . '%')
39
            ->getQuery()
40
            ->getResult()
41
        ;
42
    }
43
44
    /**
45
     * @param string|null $locale
46
     *
47
     * @return QueryBuilder
48
     */
49
    private function createTranslationBasedQueryBuilder(?string $locale = null): QueryBuilder
50
    {
51
        $queryBuilder = $this->createQueryBuilder('o')
52
            ->addSelect('translation')
53
            ->leftJoin('o.translations', 'translation')
54
        ;
55
56
        if (null !== $locale) {
57
            $queryBuilder
58
                ->andWhere('translation.locale = :locale')
59
                ->setParameter('locale', $locale)
60
            ;
61
        }
62
63
        return $queryBuilder;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function findOneByCodeAndChannelCode(string $code, ?string $localeCode, string $channelCode): ?SectionInterface
70
    {
71
        return $this->createQueryBuilder('o')
72
            ->leftJoin('o.translations', 'translation')
73
            ->innerJoin('o.channels', 'channels')
74
            ->where('translation.locale = :localeCode')
75
            ->andWhere('o.code = :code')
76
            ->andWhere('channels.code = :channelCode')
77
            ->setParameter('code', $code)
78
            ->setParameter('localeCode', $localeCode)
79
            ->setParameter('channelCode', $channelCode)
80
            ->getQuery()
81
            ->getOneOrNullResult()
82
        ;
83
    }
84
}
85