Completed
Push — master ( 48f2f1...3f90d9 )
by Antony
02:39
created

SchedulesLoop::buildModelCriteria()   C

Complexity

Conditions 13
Paths 192

Size

Total Lines 53
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 53
rs 5.7588
cc 13
eloc 38
nc 192
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
            ], 'id')
104
105
        );
106
    }
107
108
    /**
109
     * this method returns a Propel ModelCriteria
110
     *
111
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria
112
     */
113
    public function buildModelCriteria()
114
    {
115
        $query = DealerShedulesQuery::create();
116
117
        if ($id = $this->getId()) {
118
            $query->filterById($id);
119
        }
120
121
        if ($day = $this->getDay()) {
122
            $query->filterByDay($day);
123
        }
124
125
        if ($dealer_id = $this->getDealerId()) {
126
            $query->filterByDealerId($dealer_id);
127
        }
128
129
        if ($this->getDefaultPeriod()) {
130
            $query->filterByPeriodNull();
131
        } else {
132
            $query->filterByPeriodNotNull();
133
            if($this->getHidePast())
134
            $query->filterByPeriodEnd(new \DateTime(),Criteria::GREATER_THAN);
135
        }
136
137
        $query->filterByClosed($this->getClosed());
138
139
        foreach ($this->getOrder() as $order) {
140
            switch ($order) {
141
                case 'id':
142
                    $query->orderById();
143
                    break;
144
                case 'id-reverse':
145
                    $query->orderById(Criteria::DESC);
146
                    break;
147
                case 'day':
148
                    $query->orderByDay();
149
                    break;
150
                case 'day-reverse':
151
                    $query->orderByDay(Criteria::DESC);
152
                    break;
153
                case 'begin':
154
                    $query->orderByBegin();
155
                    break;
156
                case 'begin-reverse':
157
                    $query->orderByBegin(Criteria::DESC);
158
                    break;
159
                default:
160
                    break;
161
            }
162
        }
163
164
        return $query;
165
    }
166
167
    protected function getDayLabel($int = 0)
168
    {
169
        return [
170
            $this->translator->trans("Monday", [], Dealer::MESSAGE_DOMAIN),
171
            $this->translator->trans("Tuesday", [], Dealer::MESSAGE_DOMAIN),
172
            $this->translator->trans("Wednesday", [], Dealer::MESSAGE_DOMAIN),
173
            $this->translator->trans("Thursday", [], Dealer::MESSAGE_DOMAIN),
174
            $this->translator->trans("Friday", [], Dealer::MESSAGE_DOMAIN),
175
            $this->translator->trans("Saturday", [], Dealer::MESSAGE_DOMAIN),
176
            $this->translator->trans("Sunday", [], Dealer::MESSAGE_DOMAIN)
177
        ][$int];
178
    }
179
}