Completed
Push — master ( cdfd93...b7c857 )
by
unknown
06:47
created

FeatureAvailability::buildModelCriteria()   C

Complexity

Conditions 12
Paths 72

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 41
c 3
b 1
f 0
dl 0
loc 64
rs 6.9666
cc 12
nc 72
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\Loop\Argument\ArgumentCollection;
21
use Thelia\Core\Template\Loop\Argument\Argument;
22
use Thelia\Model\FeatureAv;
23
use Thelia\Model\FeatureAvQuery;
24
use Thelia\Model\FeatureProductQuery;
25
use Thelia\Model\Map\FeatureAvTableMap;
0 ignored issues
show
Bug introduced by
The type Thelia\Model\Map\FeatureAvTableMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Thelia\Model\Map\FeatureProductTableMap;
0 ignored issues
show
Bug introduced by
The type Thelia\Model\Map\FeatureProductTableMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Thelia\Type\TypeCollection;
28
use Thelia\Type;
29
30
/**
31
 * FeatureAvailability loop
32
 *
33
 *
34
 * Class FeatureAvailability
35
 * @package Thelia\Core\Template\Loop
36
 * @author Etienne Roudeix <[email protected]>
37
 *
38
 * {@inheritdoc}
39
 * @method int[] getId()
40
 * @method int[] getFeature()
41
 * @method int[] getExclude()
42
 * @method string[] getOrder()
43
 */
44
class FeatureAvailability extends BaseI18nLoop implements PropelSearchLoopInterface
45
{
46
    protected $timestampable = true;
47
48
    /**
49
     * @return ArgumentCollection
50
     */
51
    protected function getArgDefinitions()
52
    {
53
        return new ArgumentCollection(
54
            Argument::createIntListTypeArgument('id'),
55
            Argument::createIntListTypeArgument('feature'),
56
            Argument::createIntListTypeArgument('exclude'),
57
            new Argument(
58
                'order',
59
                new TypeCollection(
60
                    new Type\EnumListType(array('id', 'id_reverse', 'alpha', 'alpha-reverse', 'alpha_reverse', 'manual', 'manual_reverse'))
61
                ),
62
                'manual'
63
            )
64
        );
65
    }
66
67
    public function buildModelCriteria()
68
    {
69
        $search = FeatureAvQuery::create();
70
71
        /* manage translations */
72
        $this->configureI18nProcessing($search);
73
74
        $id = $this->getId();
75
76
        if (null !== $id) {
0 ignored issues
show
introduced by
The condition null !== $id is always true.
Loading history...
77
            $search->filterById($id, Criteria::IN);
78
        }
79
80
        $exclude = $this->getExclude();
81
82
        if (null !== $exclude) {
0 ignored issues
show
introduced by
The condition null !== $exclude is always true.
Loading history...
83
            $search->filterById($exclude, Criteria::NOT_IN);
84
        }
85
86
        $feature = $this->getFeature();
87
88
        if (null !== $feature) {
0 ignored issues
show
introduced by
The condition null !== $feature is always true.
Loading history...
89
            $search->filterByFeatureId($feature, Criteria::IN);
90
        }
91
92
        $orders = $this->getOrder();
93
94
        foreach ($orders as $order) {
95
            switch ($order) {
96
                case 'id':
97
                    $search->orderById(Criteria::ASC);
98
                    break;
99
                case 'id_reverse':
100
                    $search->orderById(Criteria::DESC);
101
                    break;
102
                case "alpha":
103
                    $search->addAscendingOrderByColumn('i18n_TITLE');
104
                    break;
105
                case "alpha_reverse":
106
                case "alpha-reverse":
107
                    $search->addDescendingOrderByColumn('i18n_TITLE');
108
                    break;
109
                case "manual":
110
                    $search->orderByPosition(Criteria::ASC);
111
                    break;
112
                case "manual_reverse":
113
                    $search->orderByPosition(Criteria::DESC);
114
                    break;
115
            }
116
        }
117
118
        // Search only non-freetext feature values.
119
        $search
120
            ->useFeatureProductQuery()
121
                ->filterByIsFreeText(false)
122
                ->_or()
123
                ->filterByIsFreeText(null) //does not belong to any product
124
            ->endUse()
125
        ;
126
127
        // Joining with FeatureProduct may result in multiple occurences of the same FeatureAv. Juste get one.
128
        $search->distinct();
129
130
        return $search;
131
    }
132
133
    public function parseResults(LoopResult $loopResult)
134
    {
135
        /** @var FeatureAv $featureAv */
136
        foreach ($loopResult->getResultDataCollection() as $featureAv) {
137
            $loopResultRow = new LoopResultRow($featureAv);
138
            $loopResultRow->set("ID", $featureAv->getId())
139
                ->set("IS_TRANSLATED", $featureAv->getVirtualColumn('IS_TRANSLATED'))
140
                ->set("LOCALE", $this->locale)
141
                ->set("FEATURE_ID", $featureAv->getFeatureId())
142
                ->set("TITLE", $featureAv->getVirtualColumn('i18n_TITLE'))
143
                ->set("CHAPO", $featureAv->getVirtualColumn('i18n_CHAPO'))
144
                ->set("DESCRIPTION", $featureAv->getVirtualColumn('i18n_DESCRIPTION'))
145
                ->set("POSTSCRIPTUM", $featureAv->getVirtualColumn('i18n_POSTSCRIPTUM'))
146
                ->set("POSITION", $featureAv->getPosition());
147
            $this->addOutputFields($loopResultRow, $featureAv);
148
149
            $loopResult->addRow($loopResultRow);
150
        }
151
152
        return $loopResult;
153
    }
154
}
155