buildModelCriteria()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0
cc 3
nc 4
nop 0

How to fix   Long Method   

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 module AttributeType                                */
4
/*                                                                                   */
5
/*      For the full copyright and license information, please view the LICENSE.txt  */
6
/*      file that was distributed with this source code.                             */
7
/*************************************************************************************/
8
9
namespace AttributeType\Loop;
10
11
use AttributeType\Model\AttributeTypeAvMeta;
12
use AttributeType\Model\AttributeTypeAvMetaQuery;
13
use AttributeType\Model\Map\AttributeAttributeTypeTableMap;
14
use AttributeType\Model\Map\AttributeTypeAvMetaTableMap;
15
use AttributeType\Model\Map\AttributeTypeTableMap;
16
use Propel\Runtime\ActiveQuery\Criteria;
17
use Propel\Runtime\ActiveQuery\Join;
18
use Thelia\Core\Template\Element\LoopResult;
19
use Thelia\Core\Template\Element\LoopResultRow;
20
use Thelia\Core\Template\Loop\Argument\Argument;
21
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
22
use Thelia\Core\Template\Loop\AttributeAvailability;
23
use Thelia\Model\AttributeAv;
24
use Thelia\Model\Map\AttributeAvTableMap;
25
26
/**
27
 * Class AttributeAvailabilityExtendLoop
28
 * @package AttributeType\Loop
29
 * @author Gilles Bourgeat <[email protected]>
30
 *
31
 * @method string getAttributeTypeId()
32
 * @method int[] getAttributeTypeSlug()
33
 */
34
class AttributeAvailabilityExtendLoop extends AttributeAvailability implements PropelSearchLoopInterface
35
{
36
    /**
37
     * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
38
     */
39
    protected function getArgDefinitions()
40
    {
41
        return parent::getArgDefinitions()->addArguments(array(
42
            Argument::createIntListTypeArgument("attribute_type_id"),
43
            Argument::createAnyTypeArgument("attribute_type_slug")
44
        ));
45
    }
46
47
    /**
48
     * this method returns a Propel ModelCriteria
49
     *
50
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria
51
     */
52
    public function buildModelCriteria()
53
    {
54
        $query = parent::buildModelCriteria();
55
56
        if (null !== $attributeTypeSlug = $this->getAttributeTypeSlug()) {
57
            $attributeTypeSlug = array_map(function($value) {
58
                return "'" . addslashes($value) . "'";
59
            }, explode(',', $attributeTypeSlug));
60
61
            $join = new Join();
62
63
            $join->addExplicitCondition(
64
                AttributeAvTableMap::TABLE_NAME,
65
                'ATTRIBUTE_ID',
66
                null,
67
                AttributeAttributeTypeTableMap::TABLE_NAME,
68
                'ATTRIBUTE_ID',
69
                null
70
            );
71
72
            $join2 = new Join();
73
74
            $join2->addExplicitCondition(
75
                AttributeAttributeTypeTableMap::TABLE_NAME,
76
                'ATTRIBUTE_TYPE_ID',
77
                null,
78
                AttributeTypeTableMap::TABLE_NAME,
79
                'ID',
80
                null
81
            );
82
83
            $join->setJoinType(Criteria::JOIN);
84
            $join2->setJoinType(Criteria::JOIN);
85
86
            $query
87
                ->addJoinObject($join, 'attribute_attribute_type_join')
88
                ->addJoinObject($join2, 'attribute_type_join')
89
                ->addJoinCondition(
90
                    'attribute_type_join',
91
                    '`attribute_type`.`slug` IN ('.implode(',', $attributeTypeSlug).')'
92
                );
93
        }
94
95
        if (null !== $attributeTypeId = $this->getAttributeTypeId()) {
96
            $join = new Join();
97
98
            $join->addExplicitCondition(
99
                AttributeAvTableMap::TABLE_NAME,
100
                'ATTRIBUTE_ID',
101
                null,
102
                AttributeAttributeTypeTableMap::TABLE_NAME,
103
                'ATTRIBUTE_ID',
104
                null
105
            );
106
107
            $join->setJoinType(Criteria::JOIN);
108
109
            $query
110
                ->addJoinObject($join, 'attribute_type_join')
111
                ->addJoinCondition(
112
                    'attribute_type_join',
113
                    '`attribute_attribute_type`.`attribute_type_id` IN (?)',
114
                    implode(',', $attributeTypeId),
115
                    null,
116
                    \PDO::PARAM_INT
117
                );
118
        }
119
120
        return $query;
121
    }
122
123
    /**
124
     * @param LoopResult $loopResult
125
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
126
     */
127
    protected function getAttributesMeta(LoopResult $loopResult)
128
    {
129
        $attributeAvIds = array();
130
131
        /** @var AttributeAV $attributeAv */
132
        foreach ($loopResult->getResultDataCollection() as $attributeAv) {
133
            $attributeAvIds[] = $attributeAv->getId();
134
        }
135
136
        $joinAttributeAttributeType = new Join();
137
138
        $joinAttributeAttributeType->addExplicitCondition(
139
            AttributeTypeAvMetaTableMap::TABLE_NAME,
140
            'ATTRIBUTE_ATTRIBUTE_TYPE_ID',
141
            null,
142
            AttributeAttributeTypeTableMap::TABLE_NAME,
143
            'ID',
144
            null
145
        );
146
147
        $joinAttributeAttributeType->setJoinType(Criteria::INNER_JOIN);
148
149
        $joinAttributeType = new Join();
150
151
        $joinAttributeType->addExplicitCondition(
152
            AttributeAttributeTypeTableMap::TABLE_NAME,
153
            'ATTRIBUTE_TYPE_ID',
154
            null,
155
            AttributeTypeTableMap::TABLE_NAME,
156
            'ID',
157
            null
158
        );
159
160
        $joinAttributeType->setJoinType(Criteria::INNER_JOIN);
161
162
        $query = AttributeTypeAvMetaQuery::create()
163
            ->filterByLocale($this->locale)
164
            ->filterByAttributeAvId($attributeAvIds, Criteria::IN)
165
            ->addJoinObject($joinAttributeAttributeType)
166
            ->addJoinObject($joinAttributeType);
167
168
        $query->withColumn('`attribute_type`.`SLUG`', 'SLUG');
169
170
        return $query->find();
171
    }
172
173
    /**
174
     * @param string $slug
175
     * @return string
176
     */
177
    protected function formatSlug($slug)
178
    {
179
        return strtoupper(str_replace('-', '_', $slug));
180
    }
181
182
    /**
183
     * @param LoopResult $loopResult
184
     * @return LoopResult
185
     * @throws \Propel\Runtime\Exception\PropelException
186
     */
187
    public function parseResults(LoopResult $loopResult)
188
    {
189
        $attributesMeta = $this->getAttributesMeta($loopResult);
190
191
        $slugs = array();
192
193
        /** @var AttributeTypeAvMeta $attributeMeta */
194
        foreach ($attributesMeta as $attributeMeta) {
195
            $slugs[$attributeMeta->getVirtualColumn('SLUG')] = true;
196
        }
197
198
        /** @var AttributeAv $attributeAv */
199
        foreach ($loopResult->getResultDataCollection() as $attributeAv) {
200
            $loopResultRow = new LoopResultRow($attributeAv);
201
            $loopResultRow
202
                ->set("ID", $attributeAv->getId())
203
                ->set("ATTRIBUTE_ID", $attributeAv->getAttributeId())
204
                ->set("IS_TRANSLATED", $attributeAv->getVirtualColumn('IS_TRANSLATED'))
205
                ->set("LOCALE", $this->locale)
206
                ->set("TITLE", $attributeAv->getVirtualColumn('i18n_TITLE'))
207
                ->set("CHAPO", $attributeAv->getVirtualColumn('i18n_CHAPO'))
208
                ->set("DESCRIPTION", $attributeAv->getVirtualColumn('i18n_DESCRIPTION'))
209
                ->set("POSTSCRIPTUM", $attributeAv->getVirtualColumn('i18n_POSTSCRIPTUM'))
210
                ->set("POSITION", $attributeAv->getPosition())
211
            ;
212
213
            // init slug variable
214
            foreach ($slugs as $slug => $bool) {
215
                $loopResultRow->set(
216
                    $this->formatSlug(
217
                        $slug
218
                    ),
219
                    null
220
                );
221
            }
222
223
            /** @var AttributeTypeAvMeta $attributeMeta */
224
            foreach ($attributesMeta as $attributeMeta) {
225
                if ($attributeMeta->getAttributeAvId() === $attributeAv->getId()) {
226
                    $loopResultRow->set(
227
                        $this->formatSlug(
228
                            $attributeMeta->getVirtualColumn('SLUG')
229
                        ),
230
                        $attributeMeta->getValue()
231
                    );
232
                }
233
            }
234
235
            $this->addOutputFields($loopResultRow, $attributeAv);
236
237
            $loopResult->addRow($loopResultRow);
238
        }
239
240
        return $loopResult;
241
    }
242
}
243