Completed
Push — master ( 395774...b3be9a )
by
unknown
16s queued 12s
created

DealerLoop::parseResults()   C

Complexity

Conditions 12
Paths 65

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 6.5333
c 0
b 0
f 0
cc 12
nc 65
nop 1

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
14
namespace Dealer\Loop;
15
16
use Dealer\Model\Dealer;
17
use Dealer\Model\DealerQuery;
18
use Dealer\Model\Map\DealerBrandTableMap;
19
use Dealer\Model\Map\DealerContentTableMap;
20
use Dealer\Model\Map\DealerFolderTableMap;
21
use Dealer\Model\Map\DealerProductTableMap;
22
use Dealer\Model\Map\DealerTableMap;
23
use Propel\Runtime\ActiveQuery\Criteria;
24
use Propel\Runtime\ActiveQuery\Join;
25
use Thelia\Core\Template\Element\BaseI18nLoop;
26
use Thelia\Core\Template\Element\LoopResult;
27
use Thelia\Core\Template\Element\LoopResultRow;
28
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
29
use Thelia\Core\Template\Loop\Argument\Argument;
30
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
31
32
/**
33
 * Class DealerLoop
34
 * @package Dealer\Loop
35
 */
36
class DealerLoop extends BaseI18nLoop implements PropelSearchLoopInterface
37
{
38
39
    /**
40
     * @param LoopResult $loopResult
41
     *
42
     * @return LoopResult
43
     */
44
    public function parseResults(LoopResult $loopResult)
45
    {
46
        /** @var Dealer $dealer */
47
        foreach ($loopResult->getResultDataCollection() as $dealer) {
48
            $loopResultRow = new LoopResultRow($dealer);
49
50
            $loopResultRow
51
                ->set('ID', $dealer->getId())
52
                ->set("ADDRESS1", $dealer->getAddress1())
53
                ->set("ADDRESS2", $dealer->getAddress2())
54
                ->set("ADDRESS3", $dealer->getAddress3())
55
                ->set("ZIPCODE", $dealer->getZipcode())
56
                ->set("CITY", $dealer->getCity())
57
                ->set("COUNTRY_ID", $dealer->getCountryId())
58
                ->set("LAT", $dealer->getLatitude())
59
                ->set("LON", $dealer->getLongitude())
60
                ->set("CREATE_DATE", $dealer->getCreatedAt())
61
                ->set("UPDATE_DATE", $dealer->getUpdatedAt())
62
                ->set("VISIBLE", $dealer->getVisible())
63
            ;
64
65
            if ($dealer->hasVirtualColumn('i18n_TITLE')) {
66
                $loopResultRow->set("TITLE", $dealer->getVirtualColumn('i18n_TITLE'));
67
            }
68
69
            if ($dealer->hasVirtualColumn('i18n_ACCESS')) {
70
                $loopResultRow->set("ACCESS", $dealer->getVirtualColumn('i18n_ACCESS'));
71
            }
72
73
            if ($dealer->hasVirtualColumn('i18n_DESCRIPTION')) {
74
                $loopResultRow->set("DESCRIPTION", $dealer->getVirtualColumn('i18n_DESCRIPTION'));
75
            }
76
77
            if ($dealer->hasVirtualColumn('i18n_BIG_DESCRIPTION')) {
78
                $loopResultRow->set("BIG_DESCRIPTION", $dealer->getVirtualColumn('i18n_BIG_DESCRIPTION'));
79
            }
80
81
            if ($dealer->hasVirtualColumn('i18n_HARD_OPEN_HOUR')) {
82
                $loopResultRow->set("HARD_OPEN_HOUR", $dealer->getVirtualColumn('i18n_HARD_OPEN_HOUR'));
83
            }
84
85
            if ($this->getWithPrevNextInfo()) {
86
                $previous = $this->getPrevious($dealer);
87
                $next = $this->getNext($dealer);
88
                $loopResultRow
89
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
90
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
91
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
92
                    ->set("NEXT", $next != null ? $next->getId() : -1);
93
            }
94
95
            $loopResult->addRow($loopResultRow);
96
        }
97
98
        return $loopResult;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    protected function getArgDefinitions()
105
    {
106
        return new ArgumentCollection(
107
            Argument::createIntListTypeArgument('id'),
108
            Argument::createIntListTypeArgument('country_id'),
109
            Argument::createIntListTypeArgument('content_id'),
110
            Argument::createIntListTypeArgument('folder_id'),
111
            Argument::createIntListTypeArgument('brand_id'),
112
            Argument::createIntListTypeArgument('product_id'),
113
            Argument::createBooleanTypeArgument('visible'),
114
            Argument::createAnyListTypeArgument('city'),
115
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
116
            Argument::createEnumListTypeArgument('order', [
117
                'id',
118
                'id-reverse',
119
                'date',
120
                'date-reverse',
121
                'title',
122
                'title-reverse',
123
            ], 'id')
124
        );
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function buildModelCriteria()
131
    {
132
        $query = DealerQuery::create();
133
134
        // manage translations
135
        $this->configureI18nProcessing(
136
            $query,
137
            [
138
                'TITLE',
139
                'DESCRIPTION',
140
                'ACCESS',
141
                'BIG_DESCRIPTION',
142
                'HARD_OPEN_HOUR'
143
            ],
144
            null,
145
            'ID',
146
            $this->getForceReturn()
147
        );
148
149
        if (null != $id = $this->getId()) {
150
            $query->filterById($id);
151
        }
152
153
        if (null != $country_id = $this->getCountryId()) {
154
            $query->filterByCountryId($country_id);
155
        }
156
157
        if (null != $city = $this->getCity()) {
158
            $query->filterByCity($city);
159
        }
160
161
        if (null != $visible = $this->getVisible()) {
162
            $query->filterByVisible($visible);
163
        }
164
165
        if ($content = $this->getContentId()) {
166
            if (is_array($content)) {
167
                $content = implode(",", $content);
168
            }
169
            $contentJoin = new Join(DealerTableMap::ID, DealerContentTableMap::DEALER_ID, Criteria::LEFT_JOIN);
170
            $query
171
                ->addJoinObject($contentJoin)
172
                ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")");
173
            ;
174
        }
175
176
        if ($folder = $this->getFolderId()) {
177
            if (is_array($folder)) {
178
                $folder = implode(",", $folder);
179
            }
180
            $contentJoin = new Join(DealerTableMap::ID, DealerFolderTableMap::DEALER_ID, Criteria::LEFT_JOIN);
181
            $query
182
                ->addJoinObject($contentJoin)
183
                ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")");
184
            ;
185
        }
186
187
        if ($brand = $this->getBrandId()) {
188
            if (is_array($brand)) {
189
                $brand = implode(",", $brand);
190
            }
191
            $contentJoin = new Join(DealerTableMap::ID, DealerBrandTableMap::DEALER_ID, Criteria::LEFT_JOIN);
192
            $query
193
                ->addJoinObject($contentJoin)
194
                ->where(DealerBrandTableMap::BRAND_ID." ".Criteria::IN." (".$brand.")");
195
            ;
196
        }
197
198
        if ($product = $this->getProductId()) {
199
            if (is_array($product)) {
200
                $product = implode(",", $product);
201
            }
202
            $contentJoin = new Join(DealerTableMap::ID, DealerProductTableMap::DEALER_ID, Criteria::LEFT_JOIN);
203
            $query
204
                ->addJoinObject($contentJoin)
205
                ->where(DealerProductTableMap::PRODUCT_ID." ".Criteria::IN." (".$product.")");
206
            ;
207
        }
208
209
        if ($this->getBackendContext() === true) {
210
            $query =$this->getAdminDealer($query);
211
        }
212
213
        foreach ($this->getOrder() as $order) {
214
            switch ($order) {
215
                case 'id' :
216
                    $query->orderById();
217
                    break;
218
                case 'id-reverse' :
219
                    $query->orderById(Criteria::DESC);
220
                    break;
221
                case 'date' :
222
                    $query->orderByCreatedAt();
223
                    break;
224
                case 'date-reverse' :
225
                    $query->orderByCreatedAt(Criteria::DESC);
226
                    break;
227
                case 'title' :
228
                    $query->useDealerI18nQuery()->orderByTitle()->endUse();
229
                    break;
230
                case 'title-reverse' :
231
                    $query->useDealerI18nQuery()->orderByTitle(Criteria::DESC)->endUse();
232
                    break;
233
                default:
234
                    break;
235
            }
236
        }
237
238
        return $query;
239
    }
240
241
    /**
242
     * @param Dealer $dealer
243
     * @return Dealer
244
     */
245
    protected function getPrevious($dealer)
246
    {
247
        $query = DealerQuery::create();
248
249
        foreach ($this->getOrder() as $order) {
250
            switch ($order) {
251
                case 'id' :
252
                    $query->orderById(Criteria::DESC);
253
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
254
                    break;
255
                case 'id-reverse' :
256
                    $query->orderById();
257
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
258
                    break;
259
                default:
260
                    $query->orderById(Criteria::DESC);
261
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
262
                    break;
263
            }
264
        }
265
266
        return $query->findOne();
267
    }
268
269
    /**
270
     * @param Dealer $dealer
271
     * @return Dealer
272
     */
273
    protected function getNext($dealer)
274
    {
275
        $query = DealerQuery::create();
276
277
        foreach ($this->getOrder() as $order) {
278
            switch ($order) {
279
                case 'id' :
280
                    $query->orderById();
281
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
282
                    break;
283
                case 'id-reverse' :
284
                    $query->orderById(Criteria::DESC);
285
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
286
                    break;
287
                default:
288
                    $query->orderById();
289
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
290
                    break;
291
            }
292
        }
293
294
        return $query->findOne();
295
    }
296
297
    /**
298
     * @param DealerQuery $query
299
     * @return DealerQuery
300
     */
301
    protected function getAdminDealer($query)
302
    {
303
        /** @var \Thelia\Model\Admin $admin */
304
        $admin = $this->securityContext->getAdminUser();
305
    
306
        if ($admin === null) {
307
            return $query;
308
        }
309
310
        if ($admin->getProfileId() === null) {
311
            return $query;
312
        }
313
314
        // If the current admin has an allowed profile_id, let him see all the dealers
315
        if (null != $configProfileIds = \Dealer\Dealer::getConfigValue(\Dealer\Dealer::CONFIG_ALLOW_PROFILE_ID)) {
316
            $profileIds = explode(',', $configProfileIds);
317
318
            if (in_array($admin->getProfileId(), $profileIds)) {
319
                return $query;
320
            }
321
        }
322
323
        return $query->useDealerAdminQuery()->filterByAdminId($admin->getId())->endUse();
324
    }
325
}
326