Completed
Push — master ( 52cd2c...047880 )
by Kamil
47:26 queued 26:32
created

TaxonRepository::findChildrenByChannelMenuTaxon()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\TaxonomyBundle\Doctrine\ORM;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
use Sylius\Component\Taxonomy\Model\TaxonInterface;
19
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
20
21
class TaxonRepository extends EntityRepository implements TaxonRepositoryInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function findChildren(string $parentCode, ?string $locale = null): array
27
    {
28
        return $this->createTranslationBasedQueryBuilder($locale)
29
            ->addSelect('child')
30
            ->innerJoin('o.parent', 'parent')
31
            ->leftJoin('o.children', 'child')
32
            ->andWhere('parent.code = :parentCode')
33
            ->addOrderBy('o.position')
34
            ->setParameter('parentCode', $parentCode)
35
            ->getQuery()
36
            ->getResult()
37
        ;
38
    }
39
40
    public function findChildrenByChannelMenuTaxon(?TaxonInterface $menuTaxon = null, ?string $locale = null): array
41
    {
42
        return $this->createTranslationBasedQueryBuilder($locale)
43
            ->addSelect('child')
44
            ->innerJoin('o.parent', 'parent')
45
            ->leftJoin('o.children', 'child')
46
            ->andWhere('parent.code = :parentCode')
47
            ->addOrderBy('o.position')
48
            ->setParameter('parentCode', ($menuTaxon !== null) ? $menuTaxon->getCode() : 'category')
49
            ->getQuery()
50
            ->getResult()
51
        ;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function findOneBySlug(string $slug, string $locale): ?TaxonInterface
58
    {
59
        return $this->createQueryBuilder('o')
60
            ->addSelect('translation')
61
            ->innerJoin('o.translations', 'translation')
62
            ->andWhere('translation.slug = :slug')
63
            ->andWhere('translation.locale = :locale')
64
            ->setParameter('slug', $slug)
65
            ->setParameter('locale', $locale)
66
            ->getQuery()
67
            ->getOneOrNullResult()
68
        ;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function findByName(string $name, string $locale): array
75
    {
76
        return $this->createQueryBuilder('o')
77
            ->addSelect('translation')
78
            ->innerJoin('o.translations', 'translation')
79
            ->andWhere('translation.name = :name')
80
            ->andWhere('translation.locale = :locale')
81
            ->setParameter('name', $name)
82
            ->setParameter('locale', $locale)
83
            ->getQuery()
84
            ->getResult()
85
        ;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function findRootNodes(): array
92
    {
93
        return $this->createQueryBuilder('o')
94
            ->andWhere('o.parent IS NULL')
95
            ->addOrderBy('o.position')
96
            ->getQuery()
97
            ->getResult()
98
        ;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function findByNamePart(string $phrase, ?string $locale = null, ?int $limit = null): array
105
    {
106
        return $this->createTranslationBasedQueryBuilder($locale)
107
            ->andWhere('translation.name LIKE :name')
108
            ->setParameter('name', '%' . $phrase . '%')
109
            ->setMaxResults($limit)
110
            ->getQuery()
111
            ->getResult()
112
        ;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function createListQueryBuilder(): QueryBuilder
119
    {
120
        return $this->createQueryBuilder('o')->leftJoin('o.translations', 'translation');
121
    }
122
123
    protected function createTranslationBasedQueryBuilder(?string $locale): QueryBuilder
124
    {
125
        $queryBuilder = $this->createQueryBuilder('o')
126
            ->addSelect('translation')
127
            ->leftJoin('o.translations', 'translation')
128
        ;
129
130
        if (null !== $locale) {
131
            $queryBuilder
132
                ->andWhere('translation.locale = :locale')
133
                ->setParameter('locale', $locale)
134
            ;
135
        }
136
137
        return $queryBuilder;
138
    }
139
}
140