Completed
Pull Request — 2.2 (#2130)
by Gilles
47:47 queued 35:49
created

Category::buildModelCriteria()   F

Complexity

Conditions 22
Paths 4752

Size

Total Lines 97
Code Lines 63

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 97
rs 2
cc 22
eloc 63
nc 4752
nop 0

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\BaseI18nLoop;
17
use Thelia\Core\Template\Element\LoopResult;
18
use Thelia\Core\Template\Element\LoopResultRow;
19
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
20
use Thelia\Core\Template\Element\SearchLoopInterface;
21
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
22
use Thelia\Core\Template\Loop\Argument\Argument;
23
use Thelia\Model\CategoryQuery;
24
use Thelia\Type\TypeCollection;
25
use Thelia\Type;
26
use Thelia\Type\BooleanOrBothType;
27
use Thelia\Model\ProductQuery;
28
29
/**
30
 *
31
 * Category loop, all params available :
32
 *
33
 * - id : can be an id (eq : 3) or a "string list" (eg: 3, 4, 5)
34
 * - parent : categories having this parent id
35
 * - current : current id is used if you are on a category page
36
 * - not_empty : if value is 1, category and subcategories must have at least 1 product
37
 * - visible : default 1, if you want category not visible put 0
38
 * - order : all value available :  'alpha', 'alpha_reverse', 'manual' (default), 'manual_reverse', 'random'
39
 * - exclude : all category id you want to exclude (as for id, an integer or a "string list" can be used)
40
 *
41
 * example :
42
 *
43
 * <THELIA_cat type="category" parent="3" limit="4">
44
 *      #TITLE : #ID
45
 * </THELIA_cat>
46
 *
47
 *
48
 * Class Category
49
 * @package Thelia\Core\Template\Loop
50
 * @author Manuel Raynaud <[email protected]>
51
 * @author Etienne Roudeix <[email protected]>
52
 */
53
class Category extends BaseI18nLoop implements PropelSearchLoopInterface, SearchLoopInterface
54
{
55
    protected $timestampable = true;
56
    protected $versionable = true;
57
58
    /**
59
     * @return ArgumentCollection
60
     */
61
    protected function getArgDefinitions()
62
    {
63
        return new ArgumentCollection(
64
            Argument::createIntListTypeArgument('id'),
65
            Argument::createIntTypeArgument('parent'),
66
            Argument::createIntTypeArgument('product'),
67
            Argument::createIntTypeArgument('exclude_product'),
68
            Argument::createBooleanTypeArgument('current'),
69
            Argument::createBooleanTypeArgument('not_empty', 0),
70
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
71
            Argument::createBooleanTypeArgument('need_count_child', false),
72
            Argument::createBooleanTypeArgument('need_product_count', false),
73
            Argument::createBooleanOrBothTypeArgument('visible', 1),
74
            new Argument(
75
                'order',
76
                new TypeCollection(
77
                    new Type\EnumListType(array('id', 'id_reverse', 'alpha', 'alpha_reverse', 'manual', 'manual_reverse', 'visible', 'visible_reverse', 'random'))
78
                ),
79
                'manual'
80
            ),
81
            Argument::createIntListTypeArgument('exclude')
82
        );
83
    }
84
85
    /**
86
     * @return array of available field to search in
87
     */
88
    public function getSearchIn()
89
    {
90
        return [
91
            "title"
92
        ];
93
    }
94
95
    public function doSearch(&$search, $searchTerm, $searchIn, $searchCriteria)
96
    {
97
        $search->_and();
98
99
        $search->where("CASE WHEN NOT ISNULL(`requested_locale_i18n`.ID) THEN `requested_locale_i18n`.`TITLE` ELSE `default_locale_i18n`.`TITLE` END ".$searchCriteria." ?", $searchTerm, \PDO::PARAM_STR);
100
    }
101
102
    public function buildModelCriteria()
103
    {
104
        $search = CategoryQuery::create();
105
106
        /* manage translations */
107
        $this->configureI18nProcessing($search, array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM', 'META_TITLE', 'META_DESCRIPTION', 'META_KEYWORDS'));
108
109
        $id = $this->getId();
0 ignored issues
show
Documentation Bug introduced by
The method getId does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
111
        if (!is_null($id)) {
112
            $search->filterById($id, Criteria::IN);
113
        }
114
115
        $parent = $this->getParent();
0 ignored issues
show
Documentation Bug introduced by
The method getParent does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
116
117
        if (!is_null($parent)) {
118
            $search->filterByParent($parent);
119
        }
120
121
        $current = $this->getCurrent();
0 ignored issues
show
Documentation Bug introduced by
The method getCurrent does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
122
123
        if ($current === true) {
124
            $search->filterById($this->request->get("category_id"));
125
        } elseif ($current === false) {
126
            $search->filterById($this->request->get("category_id"), Criteria::NOT_IN);
127
        }
128
129
        $exclude = $this->getExclude();
0 ignored issues
show
Documentation Bug introduced by
The method getExclude does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
130
131
        if (!is_null($exclude)) {
132
            $search->filterById($exclude, Criteria::NOT_IN);
133
        }
134
135
        $visible = $this->getVisible();
0 ignored issues
show
Documentation Bug introduced by
The method getVisible does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
136
137
        if ($visible !== BooleanOrBothType::ANY) {
138
            $search->filterByVisible($visible ? 1 : 0);
139
        }
140
141
        $product = $this->getProduct();
0 ignored issues
show
Documentation Bug introduced by
The method getProduct does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
142
143
        if ($product != null) {
144
            $obj = ProductQuery::create()->findPk($product);
145
146
            if ($obj != null) {
147
                $search->filterByProduct($obj, Criteria::IN);
148
            }
149
        }
150
151
        $exclude_product = $this->getExclude_product();
0 ignored issues
show
Documentation Bug introduced by
The method getExclude_product does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
152
153
        if ($exclude_product != null) {
154
            $obj = ProductQuery::create()->findPk($exclude_product);
155
156
            if ($obj != null) {
157
                $search->filterByProduct($obj, Criteria::NOT_IN);
158
            }
159
        }
160
161
        $orders  = $this->getOrder();
0 ignored issues
show
Documentation Bug introduced by
The method getOrder does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
162
163
        foreach ($orders as $order) {
164
            switch ($order) {
165
                case "id":
166
                    $search->orderById(Criteria::ASC);
167
                    break;
168
                case "id_reverse":
169
                    $search->orderById(Criteria::DESC);
170
                    break;
171
                case "alpha":
172
                    $search->addAscendingOrderByColumn('i18n_TITLE');
173
                    break;
174
                case "alpha_reverse":
175
                    $search->addDescendingOrderByColumn('i18n_TITLE');
176
                    break;
177
                case "manual_reverse":
178
                    $search->orderByPosition(Criteria::DESC);
179
                    break;
180
                case "manual":
181
                    $search->orderByPosition(Criteria::ASC);
182
                    break;
183
                case "visible":
184
                    $search->orderByVisible(Criteria::ASC);
185
                    break;
186
                case "visible_reverse":
187
                    $search->orderByVisible(Criteria::DESC);
188
                    break;
189
                case "random":
190
                    $search->clearOrderByColumns();
191
                    $search->addAscendingOrderByColumn('RAND()');
192
                    break(2);
193
                    break;
0 ignored issues
show
Unused Code introduced by
break; 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...
194
            }
195
        }
196
197
        return $search;
198
    }
199
200
    public function parseResults(LoopResult $loopResult)
201
    {
202
        foreach ($loopResult->getResultDataCollection() as $category) {
203
            /*
204
             * no cause pagination lost :
205
             * if ($this->getNotEmpty() && $category->countAllProducts() == 0) continue;
206
             */
207
208
            $loopResultRow = new LoopResultRow($category);
209
210
            $loopResultRow
211
                ->set("ID", $category->getId())
212
                ->set("IS_TRANSLATED", $category->getVirtualColumn('IS_TRANSLATED'))
213
                ->set("LOCALE", $this->locale)
214
                ->set("TITLE", $category->getVirtualColumn('i18n_TITLE'))
215
                ->set("CHAPO", $category->getVirtualColumn('i18n_CHAPO'))
216
                ->set("DESCRIPTION", $category->getVirtualColumn('i18n_DESCRIPTION'))
217
                ->set("POSTSCRIPTUM", $category->getVirtualColumn('i18n_POSTSCRIPTUM'))
218
                ->set("PARENT", $category->getParent())
219
                ->set("ROOT", $category->getRoot($category->getId()))
220
                ->set("URL", $category->getUrl($this->locale))
221
                ->set("META_TITLE", $category->getVirtualColumn('i18n_META_TITLE'))
222
                ->set("META_DESCRIPTION", $category->getVirtualColumn('i18n_META_DESCRIPTION'))
223
                ->set("META_KEYWORDS", $category->getVirtualColumn('i18n_META_KEYWORDS'))
224
                ->set("VISIBLE", $category->getVisible() ? "1" : "0")
225
                ->set("POSITION", $category->getPosition())
226
                ->set("TEMPLATE", $category->getDefaultTemplateId())
227
228
            ;
229
230
            if ($this->getNeedCountChild()) {
0 ignored issues
show
Documentation Bug introduced by
The method getNeedCountChild does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
231
                $loopResultRow->set("CHILD_COUNT", $category->countChild());
232
            }
233
234
            if ($this->getNeedProductCount()) {
0 ignored issues
show
Documentation Bug introduced by
The method getNeedProductCount does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
235
                $loopResultRow->set("PRODUCT_COUNT", $category->countAllProducts());
236
            }
237
238
            if ($this->getBackend_context() || $this->getWithPrevNextInfo()) {
0 ignored issues
show
Documentation Bug introduced by
The method getBackend_context does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getWithPrevNextInfo does not exist on object<Thelia\Core\Template\Loop\Category>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
239
                // Find previous and next category
240
                $previous = CategoryQuery::create()
241
                    ->filterByParent($category->getParent())
242
                    ->filterByPosition($category->getPosition(), Criteria::LESS_THAN)
243
                    ->orderByPosition(Criteria::DESC)
244
                    ->findOne()
245
                ;
246
247
                $next = CategoryQuery::create()
248
                    ->filterByParent($category->getParent())
249
                    ->filterByPosition($category->getPosition(), Criteria::GREATER_THAN)
250
                    ->orderByPosition(Criteria::ASC)
251
                    ->findOne()
252
                ;
253
254
                $loopResultRow
255
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
256
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
257
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
258
                    ->set("NEXT", $next != null ? $next->getId() : -1)
259
                ;
260
            }
261
262
            $this->addOutputFields($loopResultRow, $category);
263
264
            $loopResult->addRow($loopResultRow);
265
        }
266
267
        return $loopResult;
268
    }
269
}
270