Passed
Push — master ( 1029e9...94b881 )
by
unknown
05:14
created

Resource::buildModelCriteria()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 47
rs 7.6666
cc 10
nc 32
nop 0

How to fix   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
namespace Thelia\Core\Template\Loop;
14
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use Thelia\Core\Security\AccessManager;
17
use Thelia\Core\Template\Element\BaseI18nLoop;
18
use Thelia\Core\Template\Element\LoopResult;
19
use Thelia\Core\Template\Element\LoopResultRow;
20
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
21
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
22
use Thelia\Core\Template\Loop\Argument\Argument;
23
use Thelia\Model\ResourceQuery;
24
use Thelia\Type;
25
use Thelia\Model\Resource as ResourceModel;
26
use Thelia\Type\TypeCollection;
27
28
/**
29
 *
30
 * Resource loop
31
 *
32
 * Class Resource
33
 *
34
 * @package Thelia\Core\Template\Loop
35
 * @author Etienne Roudeix <[email protected]>
36
 *
37
 * {@inheritdoc}
38
 * @method int getProfile()
39
 * @method string[] getCode()
40
 * @method string[] getOrder()
41
 */
42
class Resource extends BaseI18nLoop implements PropelSearchLoopInterface
43
{
44
    protected $timestampable = true;
45
46
    /**
47
     * @return ArgumentCollection
48
     */
49
    protected function getArgDefinitions()
50
    {
51
        return new ArgumentCollection(
52
            Argument::createIntTypeArgument('profile'),
53
            new Argument(
54
                'code',
55
                new Type\TypeCollection(
56
                    new Type\AlphaNumStringListType()
57
                )
58
            ),
59
            new Argument(
60
                'order',
61
                new TypeCollection(
62
                    new Type\EnumListType([
63
                        'id',
64
                        'id_reverse',
65
                        'code',
66
                        'code_reverse',
67
                        'title',
68
                        'title_reverse',
69
                    ])
70
                ),
71
                'id'
72
            )
73
        );
74
    }
75
76
    /**
77
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria|ResourceQuery
78
     * @throws \Propel\Runtime\Exception\PropelException
79
     */
80
    public function buildModelCriteria()
81
    {
82
        $search = ResourceQuery::create();
83
84
        /* manage translations */
85
        $this->configureI18nProcessing($search);
86
87
        $profile = $this->getProfile();
88
89
        if (null !== $profile) {
0 ignored issues
show
introduced by
The condition null !== $profile is always true.
Loading history...
90
            $search->leftJoinProfileResource('profile_resource')
91
                ->addJoinCondition('profile_resource', 'profile_resource.PROFILE_ID=?', $profile, null, \PDO::PARAM_INT)
92
                ->withColumn('profile_resource.access', 'access');
93
        }
94
95
        $code = $this->getCode();
96
97
        if (null !== $code) {
0 ignored issues
show
introduced by
The condition null !== $code is always true.
Loading history...
98
            $search->filterByCode($code, Criteria::IN);
99
        }
100
101
        $orders = $this->getOrder();
102
103
        foreach ($orders as $order) {
104
            switch ($order) {
105
                case "id":
106
                    $search->orderById(Criteria::ASC);
107
                    break;
108
                case "id_reverse":
109
                    $search->orderById(Criteria::DESC);
110
                    break;
111
                case "title":
112
                    $search->addAscendingOrderByColumn('i18n_TITLE');
113
                    break;
114
                case "title_reverse":
115
                    $search->addDescendingOrderByColumn('i18n_TITLE');
116
                    break;
117
                case "code":
118
                    $search->orderByCode(Criteria::ASC);
119
                    break;
120
                case "code_reverse":
121
                    $search->orderByCode(Criteria::DESC);
122
                    break;
123
            }
124
        }
125
126
        return $search;
127
    }
128
129
    /**
130
     * @param LoopResult $loopResult
131
     * @return LoopResult
132
     * @throws \Propel\Runtime\Exception\PropelException
133
     */
134
    public function parseResults(LoopResult $loopResult)
135
    {
136
        /** @var ResourceModel $resource */
137
        foreach ($loopResult->getResultDataCollection() as $resource) {
138
            $loopResultRow = new LoopResultRow($resource);
139
            $loopResultRow->set("ID", $resource->getId())
140
                ->set("IS_TRANSLATED", $resource->getVirtualColumn('IS_TRANSLATED'))
141
                ->set("LOCALE", $this->locale)
142
                ->set("CODE", $resource->getCode())
143
                ->set("TITLE", $resource->getVirtualColumn('i18n_TITLE'))
144
                ->set("CHAPO", $resource->getVirtualColumn('i18n_CHAPO'))
145
                ->set("DESCRIPTION", $resource->getVirtualColumn('i18n_DESCRIPTION'))
146
                ->set("POSTSCRIPTUM", $resource->getVirtualColumn('i18n_POSTSCRIPTUM'))
147
            ;
148
149
            if (null !== $this->getProfile()) {
150
                $accessValue = $resource->getVirtualColumn('access');
151
                $manager = new AccessManager($accessValue);
152
153
                $loopResultRow->set("VIEWABLE", $manager->can(AccessManager::VIEW)? 1 : 0)
154
                    ->set("CREATABLE", $manager->can(AccessManager::CREATE) ? 1 : 0)
155
                    ->set("UPDATABLE", $manager->can(AccessManager::UPDATE)? 1 : 0)
156
                    ->set("DELETABLE", $manager->can(AccessManager::DELETE)? 1 : 0);
157
            }
158
159
            $this->addOutputFields($loopResultRow, $resource);
160
161
            $loopResult->addRow($loopResultRow);
162
        }
163
164
        return $loopResult;
165
    }
166
}
167