Completed
Push — component-bundle ( 0f1f3e )
by Kamil
35:06
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
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
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) {
0 ignored issues
show
Unused Code introduced by
if (null !== $rootCode) ...ootCode', $rootCode); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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