Completed
Push — master ( 4d4a30...6bef1f )
by Antony
03:37
created

DealerLoop::buildModelCriteria()   F

Complexity

Conditions 13
Paths 432

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 73
rs 3.917
cc 13
eloc 46
nc 432
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
14
namespace Dealer\Loop;
15
16
use Dealer\Model\Dealer;
17
use Dealer\Model\DealerContent;
18
use Dealer\Model\DealerQuery;
19
use Dealer\Model\Map\DealerContentTableMap;
20
use Dealer\Model\Map\DealerFolderTableMap;
21
use Dealer\Model\Map\DealerTableMap;
22
use Propel\Runtime\ActiveQuery\Criteria;
23
use Propel\Runtime\ActiveQuery\Join;
24
use Thelia\Core\Template\Element\BaseI18nLoop;
25
use Thelia\Core\Template\Element\LoopResult;
26
use Thelia\Core\Template\Element\LoopResultRow;
27
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
28
use Thelia\Core\Template\Loop\Argument\Argument;
29
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
30
31
/**
32
 * Class DealerLoop
33
 * @package Dealer\Loop
34
 */
35
class DealerLoop extends BaseI18nLoop implements PropelSearchLoopInterface
36
{
37
38
    /**
39
     * @param LoopResult $loopResult
40
     *
41
     * @return LoopResult
42
     */
43
    public function parseResults(LoopResult $loopResult)
44
    {
45
        /** @var Dealer $dealer */
46
        foreach ($loopResult->getResultDataCollection() as $dealer) {
47
            $loopResultRow = new LoopResultRow($dealer);
48
49
            $loopResultRow
50
                ->set('ID', $dealer->getId())
51
                ->set("ADDRESS1", $dealer->getAddress1())
52
                ->set("ADDRESS2", $dealer->getAddress2())
53
                ->set("ADDRESS3", $dealer->getAddress3())
54
                ->set("ZIPCODE", $dealer->getZipcode())
55
                ->set("CITY", $dealer->getCity())
56
                ->set("COUNTRY_ID", $dealer->getCountryId())
57
                ->set("LAT", $dealer->getLatitude())
58
                ->set("LON", $dealer->getLongitude())
59
                ->set("CREATE_DATE", $dealer->getCreatedAt())
60
                ->set("UPDATE_DATE", $dealer->getUpdatedAt());
61
62
            if ($dealer->hasVirtualColumn('i18n_TITLE')) {
63
                $loopResultRow->set("TITLE", $dealer->getVirtualColumn('i18n_TITLE'));
64
            }
65
66
            if ($dealer->hasVirtualColumn('i18n_DESCRIPTION')) {
67
                $loopResultRow->set("DESCRIPTION", $dealer->getVirtualColumn('i18n_DESCRIPTION'));
68
            }
69
70
            if ($this->getWithPrevNextInfo()) {
71
72
                $previous = $this->getPrevious($dealer);
73
                $next = $this->getNext($dealer);
74
                $loopResultRow
75
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
76
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
77
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
78
                    ->set("NEXT", $next != null ? $next->getId() : -1);
79
            }
80
81
            $loopResult->addRow($loopResultRow);
82
        }
83
84
        return $loopResult;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    protected function getArgDefinitions()
91
    {
92
        return new ArgumentCollection(
93
            Argument::createIntListTypeArgument('id'),
94
            Argument::createIntListTypeArgument('country_id'),
95
            Argument::createIntListTypeArgument('content_id'),
96
            Argument::createIntListTypeArgument('folder_id'),
97
            Argument::createAnyListTypeArgument('city'),
98
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
99
            Argument::createEnumListTypeArgument('order', [
100
                'id',
101
                'id-reverse',
102
                'date',
103
                'date-reverse'
104
            ], 'id')
105
        );
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function buildModelCriteria()
112
    {
113
        $query = DealerQuery::create();
114
115
        // manage translations
116
        $this->configureI18nProcessing(
117
            $query,
118
            [
119
                'TITLE',
120
                'DESCRIPTION'
121
            ],
122
            null,
123
            'ID',
124
            $this->getForceReturn()
125
        );
126
127
        if ($id = $this->getId()) {
128
            $query->filterById($id);
129
        }
130
131
        if ($country_id = $this->getCountryId()) {
132
            $query->filterByCountryId($country_id);
133
        }
134
135
        if ($city = $this->getCity()) {
136
            $query->filterByCity($city);
137
        }
138
139
        if($content = $this->getContentId()){
140
            if(is_array($content)){
141
                $content = implode(",", $content);
142
            }
143
            $contentJoin = new Join(DealerTableMap::ID,DealerContentTableMap::DEALER_ID,Criteria::LEFT_JOIN);
144
            $query
145
                ->addJoinObject($contentJoin)
146
                ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")");
147
            ;
148
149
        }
150
151
        if($folder = $this->getFolderId()){
152
            if(is_array($folder)){
153
                $folder = implode(",", $folder);
154
            }
155
            $contentJoin = new Join(DealerTableMap::ID,DealerFolderTableMap::DEALER_ID,Criteria::LEFT_JOIN);
156
            $query
157
                ->addJoinObject($contentJoin)
158
                ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")");
159
            ;
160
161
        }
162
163
        foreach ($this->getOrder() as $order) {
164
            switch ($order) {
165
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
166
                    $query->orderById();
167
                    break;
168
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
169
                    $query->orderById(Criteria::DESC);
170
                    break;
171
                case 'date' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
172
                    $query->orderByCreatedAt();
173
                    break;
174
                case 'date-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
175
                    $query->orderByCreatedAt(Criteria::DESC);
176
                    break;
177
                default:
178
                    break;
179
            }
180
        }
181
182
        return $query;
183
    }
184
185
    /**
186
     * @param Dealer $dealer
187
     * @return Dealer
188
     */
189
    protected function getPrevious($dealer)
190
    {
191
        $query = DealerQuery::create();
192
193
        foreach ($this->getOrder() as $order) {
194
            switch ($order) {
195
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
196
                    $query->orderById(Criteria::DESC);
197
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
198
                    break;
199
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
200
                    $query->orderById();
201
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
202
                    break;
203
                default:
204
                    $query->orderById(Criteria::DESC);
205
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
206
                    break;
207
            }
208
        }
209
210
        return $query->findOne();
211
    }
212
213
    /**
214
     * @param Dealer $dealer
215
     * @return Dealer
216
     */
217
    protected function getNext($dealer)
218
    {
219
        $query = DealerQuery::create();
220
221
        foreach ($this->getOrder() as $order) {
222
            switch ($order) {
223
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
224
                    $query->orderById();
225
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
226
                    break;
227
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
228
                    $query->orderById(Criteria::DESC);
229
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
230
                    break;
231
                default:
232
                    $query->orderById();
233
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
234
                    break;
235
            }
236
        }
237
238
        return $query->findOne();
239
    }
240
}