Completed
Pull Request — master (#90)
by Arnaud
02:00
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 4
CRAP Score 4.125

Importance

Changes 0
Metric Value
dl 7
loc 15
ccs 4
cts 8
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 4.125
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 4
     * @param array               $filters
42
     *
43 4
     * @throws Exception
44 4
     */
45
    public function __construct(ActionConfiguration $configuration, array $filters = [])
46
    {
47
        $this->configuration = $configuration;
48
    
49 4
        $configuredFilters = $this->configuration->getParameter('filters');
50
        
51 4
        foreach ($filters as $filter => $value) {
52 4
    
53 4
            if (!key_exists($filter, $configuredFilters)) {
54 4
                throw new Exception('The filter "'.$filter.'" is not configured');
55
            }
56
            
57
            if (null !== $value) {
58
                if ('string' === $configuredFilters[$filter]['type']) {
59
                    $value = '%'.$value.'%';
60
                }
61 4
                $this->criteria[$filter] = $value;
62
            }
63
        }
64 4
    }
65 4
    
66
    /**
67
     * @param Request $request
68 4
     */
69
    public function load(Request $request)
70
    {
71
        $this->loadCriteria($request);
72
        $this->loadOrder($request);
73
        $this->loadPagination($request);
74
    }
75 4
    
76
    /**
77
     * Get the criteria values.
78
     *
79
     * @param Request $request
80 4
     */
81
    private function loadCriteria(Request $request)
82
    {
83 4
        $criteriaConfiguration = $this
84 4
            ->configuration
85
            ->getParameter('criteria')
86
        ;
87
        
88 4 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...
89 4
            $value = $request->get($criterion);
90
            
91
            if (null !== $value) {
92
                $this->criteria[$criterion] = $value;
93
            }
94
        }
95
    }
96
    
97
    /**
98
     * @param Request $request
99
     */
100
    private function loadOrder(Request $request)
101
    {
102 4
        $sortable = $this
103
            ->configuration
104
            ->getParameter('sortable')
105 4
        ;
106
        
107 4
        // if the Action is not sortable, we do not load order parameters
108
        if (true !== $sortable) {
109
            return;
110
        }
111
        $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...
112 4
        $sort = $request->get('sort');
113 4
        $order = $request->get('order', 'ASC');
114
    
115
        if ($sort) {
116 4
            $this->order = [
117
                $sort => $order,
118
            ];
119
        }
120
    
121 4
    }
122
    
123
    /**
124 4
     * Get the pagination values.
125
     *
126
     * @param Request             $request
127
     */
128
    private function loadPagination(Request $request)
129 4
    {
130
        // the default value is the configured one
131 4
        $this->maxPerPage = $this->configuration->getParameter('max_per_page');
132
        
133
        if (false === $this->configuration->getParameter('pager')) {
134
            return;
135
        }
136
        // the pagination is required if the load strategy is multiple and the pagerfanta is configured
137 4
        $isPaginationRequired =
138
            AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->configuration->getParameter('load_strategy') &&
139 4
            'pagerfanta' === $this->configuration->getParameter('pager')
140
        ;
141
        
142
        if ($isPaginationRequired) {
143
            // retrieve the page parameter value
144
            $this->page = (int)$request->get('page', 1);
145 4
        }
146
        
147 4
        if (null !== $request->get('maxPerPage')) {
148
            $this->maxPerPage = $request->get('maxPerPage');
149
        }
150
    }
151
    
152
    /**
153 4
     * @return array
154
     */
155 4
    public function getCriteria()
156
    {
157
        return $this->criteria;
158
    }
159
    
160
    /**
161
     * @return array
162
     */
163
    public function getOrder()
164
    {
165
        return $this->order;
166
    }
167
    
168
    /**
169
     * @return int
170
     */
171
    public function getMaxPerPage()
172
    {
173
        return $this->maxPerPage;
174
    }
175
    
176
    /**
177
     * @return int
178
     */
179
    public function getPage()
180
    {
181
        return $this->page;
182
    }
183
}
184