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

SchedulesLoop::buildModelCriteria()   D

Complexity

Conditions 15
Paths 240

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 4.5833
c 0
b 0
f 0
cc 15
nc 240
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\Dealer;
17
use Dealer\Model\DealerShedules;
18
use Dealer\Model\DealerShedulesQuery;
19
use Propel\Runtime\ActiveQuery\Criteria;
20
use Thelia\Core\Template\Element\BaseLoop;
21
use Thelia\Core\Template\Element\LoopResult;
22
use Thelia\Core\Template\Element\LoopResultRow;
23
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
24
use Thelia\Core\Template\Loop\Argument\Argument;
25
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
26
27
/**
28
 * Class ShedulesLoop
29
 * @package Dealer\Loop
30
 */
31
class SchedulesLoop extends BaseLoop implements PropelSearchLoopInterface
32
{
33
34
    /**
35
     * @param LoopResult $loopResult
36
     *
37
     * @return LoopResult
38
     */
39
    public function parseResults(LoopResult $loopResult)
40
    {
41
        /** @var DealerShedules $schedules */
42
        foreach ($loopResult->getResultDataCollection() as $schedules) {
43
            $loopResultRow = new LoopResultRow($schedules);
44
45
            $loopResultRow
46
                ->set('ID', $schedules->getId())
47
                ->set('DEALER_ID', $schedules->getDealerId())
48
                ->set('DAY', $schedules->getDay())
49
                ->set('DAY_LABEL', $this->getDayLabel($schedules->getDay()))
50
                ->set('BEGIN', $schedules->getBegin())
51
                ->set('END', $schedules->getEnd())
52
                ->set('PERIOD_BEGIN', $schedules->getPeriodBegin())
53
                ->set('PERIOD_END', $schedules->getPeriodEnd());
54
55
56
            $loopResult->addRow($loopResultRow);
57
        }
58
59
        return $loopResult;
60
    }
61
62
    /**
63
     * Definition of loop arguments
64
     *
65
     * example :
66
     *
67
     * public function getArgDefinitions()
68
     * {
69
     *  return new ArgumentCollection(
70
     *
71
     *       Argument::createIntListTypeArgument('id'),
72
     *           new Argument(
73
     *           'ref',
74
     *           new TypeCollection(
75
     *               new Type\AlphaNumStringListType()
76
     *           )
77
     *       ),
78
     *       Argument::createIntListTypeArgument('category'),
79
     *       Argument::createBooleanTypeArgument('new'),
80
     *       ...
81
     *   );
82
     * }
83
     *
84
     * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
85
     */
86
    protected function getArgDefinitions()
87
    {
88
        return new ArgumentCollection(
89
90
            Argument::createIntListTypeArgument('id'),
91
            Argument::createIntListTypeArgument('dealer_id'),
92
            Argument::createBooleanTypeArgument('default_period'),
93
            Argument::createBooleanTypeArgument('hide_past', false),
94
            Argument::createBooleanTypeArgument('closed', false),
95
            Argument::createIntListTypeArgument('day'),
96
            Argument::createEnumListTypeArgument('order', [
97
                'id',
98
                'id-reverse',
99
                'day',
100
                'day-reverse',
101
                'begin',
102
                'begin-reverse',
103
                'period-begin',
104
                'period-begin-reverse'
105
            ], 'id')
106
107
        );
108
    }
109
110
    /**
111
     * this method returns a Propel ModelCriteria
112
     *
113
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria
114
     */
115
    public function buildModelCriteria()
116
    {
117
        $query = DealerShedulesQuery::create();
118
119
        if ($id = $this->getId()) {
120
            $query->filterById($id);
121
        }
122
123
        if ($day = $this->getDay()) {
124
            $query->filterByDay($day);
125
        }
126
127
        if ($dealer_id = $this->getDealerId()) {
128
            $query->filterByDealerId($dealer_id);
129
        }
130
131
        if ($this->getDefaultPeriod()) {
132
            $query->filterByPeriodNull();
133
        } else {
134
            $query->filterByPeriodNotNull();
135
            if ($this->getHidePast()) {
136
                $query->filterByPeriodEnd(new \DateTime(), Criteria::GREATER_THAN);
137
            }
138
        }
139
140
        $query->filterByClosed($this->getClosed());
141
142
        foreach ($this->getOrder() as $order) {
143
            switch ($order) {
144
                case 'id':
145
                    $query->orderById();
146
                    break;
147
                case 'id-reverse':
148
                    $query->orderById(Criteria::DESC);
149
                    break;
150
                case 'day':
151
                    $query->orderByDay();
152
                    break;
153
                case 'day-reverse':
154
                    $query->orderByDay(Criteria::DESC);
155
                    break;
156
                case 'begin':
157
                    $query->orderByBegin();
158
                    break;
159
                case 'begin-reverse':
160
                    $query->orderByBegin(Criteria::DESC);
161
                    break;
162
                case 'period-begin':
163
                    $query->orderByPeriodBegin();
164
                    break;
165
                case 'period-begin-reverse':
166
                    $query->orderByPeriodBegin(Criteria::DESC);
167
                    break;
168
                default:
169
                    break;
170
            }
171
        }
172
173
        return $query;
174
    }
175
176
    protected function getDayLabel($int = 0)
177
    {
178
        return [
179
            $this->translator->trans("Monday", [], Dealer::MESSAGE_DOMAIN),
180
            $this->translator->trans("Tuesday", [], Dealer::MESSAGE_DOMAIN),
181
            $this->translator->trans("Wednesday", [], Dealer::MESSAGE_DOMAIN),
182
            $this->translator->trans("Thursday", [], Dealer::MESSAGE_DOMAIN),
183
            $this->translator->trans("Friday", [], Dealer::MESSAGE_DOMAIN),
184
            $this->translator->trans("Saturday", [], Dealer::MESSAGE_DOMAIN),
185
            $this->translator->trans("Sunday", [], Dealer::MESSAGE_DOMAIN)
186
        ][$int];
187
    }
188
}
189