Completed
Pull Request — master (#90)
by Arnaud
05:26
created

LoadParameterExtractor::getPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Request;
4
5
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class LoadParameterExtractor
10
{
11
    /**
12
     * @var ActionConfiguration
13
     */
14
    private $configuration;
15
    
16
    /**
17
     * @var array
18
     */
19
    private $criteria = [];
20
    
21
    /**
22
     * @var array
23
     */
24
    private $order = [];
25
    
26
    /**
27
     * @var int
28
     */
29
    private $maxPerPage = 5000;
30
    
31
    /**
32
     * @var int
33
     */
34
    private $page = 1;
35
    
36
    /**
37
     * LoadParameterExtractor constructor.
38
     *
39
     * @param ActionConfiguration $configuration
40
     */
41 1
    public function __construct(ActionConfiguration $configuration)
42
    {
43 1
        $this->configuration = $configuration;
44 1
    }
45
    
46
    /**
47
     * @param Request $request
48
     */
49 1
    public function load(Request $request)
50
    {
51 1
        $this->loadCriteria($request);
52 1
        $this->loadOrder($request);
53 1
        $this->loadPagination($request);
54 1
    }
55
    
56
    /**
57
     * Get the criteria values.
58
     *
59
     * @param Request $request
60
     */
61 1
    private function loadCriteria(Request $request)
62
    {
63
        $criteriaConfiguration = $this
64 1
            ->configuration
65 1
            ->getParameter('criteria')
66
        ;
67
        
68 1 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...
69
            $value = $request->get($criterion);
70
            
71
            if (null !== $value) {
72
                $this->criteria[$criterion] = $value;
73
            }
74
        }
75 1
    }
76
    
77
    /**
78
     * @param Request $request
79
     */
80 1
    private function loadOrder(Request $request)
81
    {
82
        $sortable = $this
83 1
            ->configuration
84 1
            ->getParameter('sortable')
85
        ;
86
        
87
        // if the Action is not sortable, we do not load order parameters
88 1
        if (true !== $sortable) {
89 1
            return;
90
        }
91
        $this->order = [
92
            'sort' => $request->get('sort'),
93
            'order' => $request->get('order'),
94
        ];
95
    }
96
    
97
    /**
98
     * Get the pagination values.
99
     *
100
     * @param Request             $request
101
     */
102 1
    private function loadPagination(Request $request)
103
    {
104
        // the default value is the configured one
105 1
        $this->maxPerPage = $this->configuration->getParameter('max_per_page');
106
        
107 1
        if (false === $this->configuration->getParameter('pager')) {
108
            return;
109
        }
110
        // the pagination is required if the load strategy is multiple and the pagerfanta is configured
111
        $isPaginationRequired =
112 1
            AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->configuration->getParameter('load_strategy') &&
113 1
            'pagerfanta' === $this->configuration->getParameter('pager')
114
        ;
115
        
116 1
        if ($isPaginationRequired) {
117
            // retrieve the page parameter value
118
            $this->page = $request->get('page', 1);
119
        }
120
        
121 1
        if (null !== $request->get('maxPerPage')) {
122
            $this->maxPerPage = $request->get('maxPerPage');
123
        }
124 1
    }
125
    
126
    /**
127
     * @return array
128
     */
129 1
    public function getCriteria()
130
    {
131 1
        return $this->criteria;
132
    }
133
    
134
    /**
135
     * @return array
136
     */
137 1
    public function getOrder()
138
    {
139 1
        return $this->order;
140
    }
141
    
142
    /**
143
     * @return int
144
     */
145 1
    public function getMaxPerPage()
146
    {
147 1
        return $this->maxPerPage;
148
    }
149
    
150
    /**
151
     * @return int
152
     */
153 1
    public function getPage()
154
    {
155 1
        return $this->page;
156
    }
157
}
158