Completed
Pull Request — master (#90)
by Arnaud
17:45
created

LoadParameterExtractor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 176
Duplicated Lines 3.98 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 7
loc 176
ccs 36
cts 57
cp 0.6316
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 5
A load() 0 6 1
A loadCriteria() 7 15 3
A loadOrder() 0 22 3
B loadPagination() 0 23 5
A getCriteria() 0 4 1
A getOrder() 0 4 1
A getMaxPerPage() 0 4 1
A getPage() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 4
    public function __construct(ActionConfiguration $configuration, array $filters = [])
46
    {
47 4
        $this->configuration = $configuration;
48
    
49 4
        $configuredFilters = $this->configuration->getParameter('filters');
50
        
51 4
        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 4
    }
67
    
68
    /**
69
     * @param Request $request
70
     */
71 4
    public function load(Request $request)
72
    {
73 4
        $this->loadCriteria($request);
74 4
        $this->loadOrder($request);
75 4
        $this->loadPagination($request);
76 4
    }
77
    
78
    /**
79
     * Get the criteria values.
80
     *
81
     * @param Request $request
82
     */
83 4
    private function loadCriteria(Request $request)
84
    {
85
        $criteriaConfiguration = $this
86 4
            ->configuration
87 4
            ->getParameter('criteria')
88
        ;
89
        
90 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...
91
            $value = $request->get($criterion);
92
            
93
            if (null !== $value) {
94
                $this->criteria[$criterion] = $value;
95
            }
96
        }
97 4
    }
98
    
99
    /**
100
     * @param Request $request
101
     */
102 4
    private function loadOrder(Request $request)
103
    {
104
        $sortable = $this
105 4
            ->configuration
106 4
            ->getParameter('sortable')
107
        ;
108
        
109
        // if the Action is not sortable, we do not load order parameters
110 4
        if (true !== $sortable) {
111 4
            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 4
    private function loadPagination(Request $request)
131
    {
132
        // the default value is the configured one
133 4
        $this->maxPerPage = $this->configuration->getParameter('max_per_page');
134
        
135 4
        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 4
            AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->configuration->getParameter('load_strategy') &&
141 4
            'pagerfanta' === $this->configuration->getParameter('pager')
142
        ;
143
        
144 4
        if ($isPaginationRequired) {
145
            // retrieve the page parameter value
146
            $this->page = (int)$request->get('page', 1);
147
        }
148
        
149 4
        if (null !== $request->get('maxPerPage')) {
150
            $this->maxPerPage = $request->get('maxPerPage');
151
        }
152 4
    }
153
    
154
    /**
155
     * @return array
156
     */
157 4
    public function getCriteria()
158
    {
159 4
        return $this->criteria;
160
    }
161
    
162
    /**
163
     * @return array
164
     */
165 4
    public function getOrder()
166
    {
167 4
        return $this->order;
168
    }
169
    
170
    /**
171
     * @return int
172
     */
173 4
    public function getMaxPerPage()
174
    {
175 4
        return $this->maxPerPage;
176
    }
177
    
178
    /**
179
     * @return int
180
     */
181 4
    public function getPage()
182
    {
183 4
        return $this->page;
184
    }
185
}
186