Completed
Pull Request — master (#90)
by Arnaud
02:15
created

LoadParameterExtractor::loadCriteria()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 7
Ratio 46.67 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 7
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Request;
4
5
use Exception;
6
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class LoadParameterExtractor
11
{
12
    /**
13
     * @var ActionConfiguration
14
     */
15
    private $configuration;
16
    
17
    /**
18
     * @var array
19
     */
20
    private $criteria = [];
21
    
22
    /**
23
     * @var array
24
     */
25
    private $order = [];
26
    
27
    /**
28
     * @var int
29
     */
30
    private $maxPerPage = 5000;
31
    
32
    /**
33
     * @var int
34
     */
35
    private $page = 1;
36
    
37
    /**
38
     * LoadParameterExtractor constructor.
39
     *
40
     * @param ActionConfiguration $configuration
41
     * @param array               $filters
42
     *
43
     * @throws Exception
44
     */
45
    public function __construct(ActionConfiguration $configuration, array $filters = [])
46
    {
47
        $this->configuration = $configuration;
48
    
49
        $configuredFilters = $this->configuration->getParameter('filters');
50
        
51
        foreach ($filters as $filter => $value) {
52
    
53
            if (!key_exists($filter, $configuredFilters)) {
54
                throw new Exception('The filter "'.$filter.'" is not configured');
55
            }
56
            
57
            if (null !== $value) {
58
                dump($configuredFilters);
59
                dump($filter);
60
                if ('string' === $configuredFilters[$filter]['type']) {
61
                    $value = '%'.$value.'%';
62
                }
63
                $this->criteria[$filter] = $value;
64
            }
65
        }
66
    }
67
    
68
    /**
69
     * @param Request $request
70
     */
71
    public function load(Request $request)
72
    {
73
        $this->loadCriteria($request);
74
        $this->loadOrder($request);
75
        $this->loadPagination($request);
76
    }
77
    
78
    /**
79
     * Get the criteria values.
80
     *
81
     * @param Request $request
82
     */
83
    private function loadCriteria(Request $request)
84
    {
85
        $criteriaConfiguration = $this
86
            ->configuration
87
            ->getParameter('criteria')
88
        ;
89
        
90 View Code Duplication
        foreach ($criteriaConfiguration as $criterion) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
            $value = $request->get($criterion);
92
            
93
            if (null !== $value) {
94
                $this->criteria[$criterion] = $value;
95
            }
96
        }
97
    }
98
    
99
    /**
100
     * @param Request $request
101
     */
102
    private function loadOrder(Request $request)
103
    {
104
        $sortable = $this
105
            ->configuration
106
            ->getParameter('sortable')
107
        ;
108
        
109
        // if the Action is not sortable, we do not load order parameters
110
        if (true !== $sortable) {
111
            return;
112
        }
113
        $this->order = $this->configuration->getParameter('order');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->configuration->getParameter('order') of type * is incompatible with the declared type array of property $order.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
114
        $sort = $request->get('sort');
115
        $order = $request->get('order', 'ASC');
116
    
117
        if ($sort) {
118
            $this->order = [
119
                $sort => $order,
120
            ];
121
        }
122
    
123
    }
124
    
125
    /**
126
     * Get the pagination values.
127
     *
128
     * @param Request             $request
129
     */
130
    private function loadPagination(Request $request)
131
    {
132
        // the default value is the configured one
133
        $this->maxPerPage = $this->configuration->getParameter('max_per_page');
134
        
135
        if (false === $this->configuration->getParameter('pager')) {
136
            return;
137
        }
138
        // the pagination is required if the load strategy is multiple and the pagerfanta is configured
139
        $isPaginationRequired =
140
            AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->configuration->getParameter('load_strategy') &&
141
            'pagerfanta' === $this->configuration->getParameter('pager')
142
        ;
143
        
144
        if ($isPaginationRequired) {
145
            // retrieve the page parameter value
146
            $this->page = (int)$request->get('page', 1);
147
        }
148
        
149
        if (null !== $request->get('maxPerPage')) {
150
            $this->maxPerPage = $request->get('maxPerPage');
151
        }
152
    }
153
    
154
    /**
155
     * @return array
156
     */
157
    public function getCriteria()
158
    {
159
        return $this->criteria;
160
    }
161
    
162
    /**
163
     * @return array
164
     */
165
    public function getOrder()
166
    {
167
        return $this->order;
168
    }
169
    
170
    /**
171
     * @return int
172
     */
173
    public function getMaxPerPage()
174
    {
175
        return $this->maxPerPage;
176
    }
177
    
178
    /**
179
     * @return int
180
     */
181
    public function getPage()
182
    {
183
        return $this->page;
184
    }
185
}
186