Completed
Pull Request — master (#1852)
by Gilles
26:45 queued 12:41
created

CategoryTree::buildCategoryTree()   D

Complexity

Conditions 15
Paths 289

Size

Total Lines 68
Code Lines 46

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 68
rs 4.2779
cc 15
eloc 46
nc 289
nop 7

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Core\Template\Loop;
14
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use Thelia\Core\Template\Element\ArraySearchLoopInterface;
17
use Thelia\Core\Template\Element\LoopResult;
18
use Thelia\Core\Template\Element\LoopResultRow;
19
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
20
use Thelia\Core\Template\Loop\Argument\Argument;
21
use Thelia\Model\CategoryQuery;
22
use Thelia\Type\TypeCollection;
23
use Thelia\Type;
24
use Thelia\Type\BooleanOrBothType;
25
use Thelia\Core\Template\Element\BaseI18nLoop;
26
27
/**
28
 *
29
 * Category tree loop, to get a category tree from a given category to a given depth.
30
 *
31
 * - category is the category id
32
 * - depth is the maximum depth to go, default unlimited
33
 * - visible if true or missing, only visible categories will be displayed. If false, all categories (visible or not) are returned.
34
 *
35
 * @package Thelia\Core\Template\Loop
36
 * @author Franck Allimant <[email protected]>
37
 *
38
 * {@inheritdoc}
39
 * @method int getCategory()
40
 * @method int getDepth()
41
 * @method bool getNeedCountChild()
42
 * @method bool|string getVisible()
43
 * @method int[] getExclude()
44
 * @method string[] getOrder()
45
 */
46
class CategoryTree extends BaseI18nLoop implements ArraySearchLoopInterface
47
{
48
    /**
49
     * @return ArgumentCollection
50
     */
51
    protected function getArgDefinitions()
52
    {
53
        return new ArgumentCollection(
54
            Argument::createIntTypeArgument('category', null, true),
55
            Argument::createIntTypeArgument('depth', PHP_INT_MAX),
56
            Argument::createBooleanTypeArgument('need_count_child', false),
57
            Argument::createBooleanOrBothTypeArgument('visible', true, false),
58
            Argument::createIntListTypeArgument('exclude', array()),
59
            new Argument(
60
                'order',
61
                new TypeCollection(
62
                    new Type\EnumListType(array('position', 'position_reverse', 'id', 'id_reverse', 'alpha', 'alpha_reverse'))
63
                ),
64
                'position'
65
            )
66
        );
67
    }
68
69
    // changement de rubrique
70
    protected function buildCategoryTree($parent, $visible, $level, $previousLevel, $maxLevel, $exclude, &$resultsList)
71
    {
72
        if ($level > $maxLevel) {
73
            return;
74
        }
75
76
        $search = CategoryQuery::create();
77
        $this->configureI18nProcessing($search, array('TITLE'));
78
79
        $search->filterByParent($parent);
80
81
        if ($visible !== BooleanOrBothType::ANY) {
82
            $search->filterByVisible($visible);
83
        }
84
85
        if ($exclude != null) {
86
            $search->filterById($exclude, Criteria::NOT_IN);
87
        }
88
89
        $orders  = $this->getOrder();
90
91
        foreach ($orders as $order) {
92
            switch ($order) {
93
                case "position":
94
                    $search->orderByPosition(Criteria::ASC);
95
                    break;
96
                case "position_reverse":
97
                    $search->orderByPosition(Criteria::DESC);
98
                    break;
99
                case "id":
100
                    $search->orderById(Criteria::ASC);
101
                    break;
102
                case "id_reverse":
103
                    $search->orderById(Criteria::DESC);
104
                    break;
105
                case "alpha":
106
                    $search->addAscendingOrderByColumn('i18n_TITLE');
107
                    break;
108
                case "alpha_reverse":
109
                    $search->addDescendingOrderByColumn('i18n_TITLE');
110
                    break;
111
            }
112
        }
113
114
        $results = $search->find();
115
116
        $needCountChild = $this->getNeedCountChild();
117
118
        foreach ($results as $result) {
119
            $row = array(
120
                "ID" => $result->getId(),
121
                "TITLE" => $result->getVirtualColumn('i18n_TITLE'),
122
                "PARENT" => $result->getParent(),
123
                "URL" => $this->getReturnUrl() ? $result->getUrl($this->locale) : null,
124
                "VISIBLE" => $result->getVisible() ? "1" : "0",
125
                "LEVEL" => $level,
126
                'PREV_LEVEL' => $previousLevel,
127
            );
128
129
            if ($needCountChild) {
130
                $row['CHILD_COUNT'] = $result->countChild();
131
            }
132
133
            $resultsList[] = $row;
134
135
            $this->buildCategoryTree($result->getId(), $visible, 1 + $level, $level, $maxLevel, $exclude, $resultsList);
136
        }
137
    }
138
139
    public function parseResults(LoopResult $loopResult)
140
    {
141
        foreach ($loopResult->getResultDataCollection() as $result) {
142
            $loopResultRow = new LoopResultRow($result);
143
            foreach ($result as $output => $outputValue) {
144
                $loopResultRow->set($output, $outputValue);
145
            }
146
            $loopResult->addRow($loopResultRow);
147
        }
148
149
        return $loopResult;
150
    }
151
152
    public function buildArray()
153
    {
154
        $id = $this->getCategory();
155
        $depth = $this->getDepth();
156
        $visible = $this->getVisible();
157
        $exclude = $this->getExclude();
158
159
        $resultsList = array();
160
161
        $this->buildCategoryTree($id, $visible, 0, 0, $depth, $exclude, $resultsList);
162
163
        return $resultsList;
164
    }
165
}
166