Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

TaxonRepository::findChildren()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
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\Model\TaxonInterface;
16
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
17
18
/**
19
 * @author Aram Alipoor <[email protected]>
20
 */
21
class TaxonRepository extends EntityRepository implements TaxonRepositoryInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function findChildren(TaxonInterface $taxon)
27
    {
28
        $root = $taxon->isRoot() ? $taxon : $taxon->getRoot();
29
30
        $queryBuilder = $this->createQueryBuilder('o');
31
        $queryBuilder
32
            ->andWhere($queryBuilder->expr()->eq('o.root', ':root'))
33
            ->andWhere($queryBuilder->expr()->lt('o.right', ':right'))
34
            ->andWhere($queryBuilder->expr()->gt('o.left', ':left'))
35
            ->setParameter('root', $root)
36
            ->setParameter('left', $taxon->getLeft())
37
            ->setParameter('right', $taxon->getRight())
38
        ;
39
40
        return $queryBuilder->getQuery()->getResult();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function findChildrenAsTree(TaxonInterface $taxon)
47
    {
48
        $queryBuilder = $this->createQueryBuilder('o');
49
        $queryBuilder
50
            ->addSelect('translation')
51
            ->leftJoin('o.translations', 'translation')
52
            ->addSelect('children')
53
            ->leftJoin('o.children', 'children')
54
            ->andWhere('o.parent = :parent')
55
            ->addOrderBy('o.root')
56
            ->addOrderBy('o.position')
57
            ->setParameter('parent', $taxon)
58
        ;
59
60
        return $queryBuilder->getQuery()->getResult();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function findChildrenByRootCode($code)
67
    {
68
        /** @var TaxonInterface|null $root */
69
        $root = $this->findOneBy(['code' => $code]);
70
71
        if (null === $root) {
72
            return [];
73
        }
74
75
        return $this->findChildren($root);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function findChildrenAsTreeByRootCode($code)
82
    {
83
        /** @var TaxonInterface|null $root */
84
        $root = $this->findOneBy(['code' => $code]);
85
86
        if (null === $root) {
87
            return [];
88
        }
89
90
        return $this->findChildrenAsTree($root);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function findOneBySlug($slug)
97
    {
98
        return $this->createQueryBuilder('o')
99
            ->addSelect('translation')
100
            ->leftJoin('o.translations', 'translation')
101
            ->where('translation.slug = :slug')
102
            ->setParameter('slug', $slug)
103
            ->orderBy('o.position')
104
            ->getQuery()
105
            ->getOneOrNullResult()
106
        ;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function findByName($name, $locale)
113
    {
114
        return $this->createQueryBuilder('o')
115
            ->addSelect('translation')
116
            ->leftJoin('o.translations', 'translation')
117
            ->where('translation.name = :name')
118
            ->andWhere('translation.locale = :locale')
119
            ->setParameter('name', $name)
120
            ->setParameter('locale', $locale)
121
            ->getQuery()
122
            ->getResult()
123
        ;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function findRootNodes()
130
    {
131
        $queryBuilder = $this->createQueryBuilder('o');
132
        $queryBuilder
133
            ->andWhere($queryBuilder->expr()->isNull($this->getPropertyName('parent')))
134
            ->orderBy('o.position')
135
        ;
136
137
        return $queryBuilder->getQuery()->getResult();
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function findNodesTreeSorted()
144
    {
145
        $queryBuilder = $this->createQueryBuilder('o');
146
        $queryBuilder
147
            ->orderBy('o.root')
148
            ->addOrderBy('o.position')
149
        ;
150
    
151
        return $queryBuilder->getQuery()->getResult();
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function createListQueryBuilder()
158
    {
159
        return $this->createQueryBuilder('o')->leftJoin('o.translations', 'translation');
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getFormQueryBuilder()
166
    {
167
        return $this->createQueryBuilder('o');
168
    }
169
}
170