Completed
Push — master ( e71542...a026a7 )
by Kamil
16s
created

TaxonRepository::findByNamePart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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
namespace Sylius\Bundle\TaxonomyBundle\Doctrine\ORM;
13
14
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
15
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
16
17
/**
18
 * @author Aram Alipoor <[email protected]>
19
 */
20
class TaxonRepository extends EntityRepository implements TaxonRepositoryInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function findChildren($parentCode, $locale)
26
    {
27
        return $this->createQueryBuilder('o')
28
            ->addSelect('translation')
29
            ->addSelect('child')
30
            ->innerJoin('o.parent', 'parent')
31
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
32
            ->leftJoin('o.children', 'child')
33
            ->andWhere('parent.code = :parentCode')
34
            ->addOrderBy('o.position')
35
            ->setParameter('parentCode', $parentCode)
36
            ->setParameter('locale', $locale)
37
            ->getQuery()
38
            ->getResult()
39
        ;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function findOneBySlug($slug, $locale)
46
    {
47
        return $this->createQueryBuilder('o')
48
            ->addSelect('translation')
49
            ->innerJoin('o.translations', 'translation')
50
            ->andWhere('translation.slug = :slug')
51
            ->andWhere('translation.locale = :locale')
52
            ->setParameter('slug', $slug)
53
            ->setParameter('locale', $locale)
54
            ->getQuery()
55
            ->getOneOrNullResult()
56
        ;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function findByName($name, $locale)
63
    {
64
        return $this->createQueryBuilder('o')
65
            ->addSelect('translation')
66
            ->innerJoin('o.translations', 'translation')
67
            ->andWhere('translation.name = :name')
68
            ->andWhere('translation.locale = :locale')
69
            ->setParameter('name', $name)
70
            ->setParameter('locale', $locale)
71
            ->getQuery()
72
            ->getResult()
73
        ;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function findRootNodes()
80
    {
81
        return $this->createQueryBuilder('o')
82
            ->andWhere('o.parent IS NULL')
83
            ->addOrderBy('o.position')
84
            ->getQuery()
85
            ->getResult()
86
        ;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function findNodesTreeSorted($rootCode = null)
93
    {
94
        return $this->createQueryBuilder('o')
95
            ->addOrderBy('o.root')
96
            ->addOrderBy('o.left')
97
            ->addOrderBy('o.position')
98
            ->getQuery()
99
            ->getResult()
100
        ;
101
102
        if (null !== $rootCode) {
103
            $queryBuilder
104
                ->join('o.root', 'root')
105
                ->andWhere('root.code = :rootCode')
106
                ->setParameter('rootCode', $rootCode)
107
            ;
108
        }
109
110
        return $queryBuilder->getQuery()->getResult();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function findByNamePart($phrase, $locale)
117
    {
118
        return $this->createQueryBuilder('o')
119
            ->addSelect('translation')
120
            ->innerJoin('o.translations', 'translation')
121
            ->andWhere('translation.name LIKE :name')
122
            ->andWhere('translation.locale = :locale')
123
            ->setParameter('name', '%'.$phrase.'%')
124
            ->setParameter('locale', $locale)
125
            ->getQuery()
126
            ->getResult()
127
        ;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function createListQueryBuilder()
134
    {
135
        return $this->createQueryBuilder('o')->leftJoin('o.translations', 'translation');
136
    }
137
}
138