Completed
Push — master ( 239572...90ee88 )
by
unknown
03:26
created

Datatable110QueryParser   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 26
c 3
b 0
f 2
lcom 1
cbo 6
dl 0
loc 175
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A canParse() 0 4 1
A parse() 0 21 1
A isEmpty() 0 4 1
A isArrayAndHasKey() 0 12 3
A getDrawCall() 0 6 2
A getStart() 0 6 2
A getLength() 0 6 2
A getSearch() 0 8 2
B getSearchColumns() 0 21 5
A getRegex() 0 8 2
B getOrder() 0 15 5
1
<?php
2
3
namespace OpenSkill\Datatable\Queries\Parser;
4
5
6
use OpenSkill\Datatable\Columns\ColumnConfiguration;
7
use OpenSkill\Datatable\Queries\QueryConfiguration;
8
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\ParameterBag;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class Datatable110QueryParser extends QueryParser
14
{
15
16
    /**
17
     * Method to determine if this parser can handle the query parameters. If so then the parser should return true
18
     * and be able to return a DTQueryConfiguration
19
     *
20
     * @param Request $request The current request, that should be investigated
21
     * @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration
22
     */
23
    public function canParse(Request $request)
24
    {
25
        return $request->query->has("draw");
26
    }
27
28
    /**
29
     * Method that should parse the request and return a DTQueryConfiguration
30
     *
31
     * @param Request $request The current request that should be investigated
32
     * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns
33
     * @return QueryConfiguration the configuration the provider can use to prepare the data
34
     */
35
    public function parse(Request $request, array $columnConfiguration)
36
    {
37
        $query = $request->query;
38
        $builder = QueryConfigurationBuilder::create();
39
40
        $this->getDrawCall($query, $builder);
41
42
        $this->getStart($query, $builder);
43
44
        $this->getLength($query, $builder);
45
46
        $this->getSearch($query, $builder);
47
48
        $this->getRegex($query, $builder);
49
50
        $this->getOrder($query, $builder, $columnConfiguration);
51
52
        $this->getSearchColumns($query, $builder, $columnConfiguration);
53
54
        return $builder->build();
55
    }
56
57
    /**
58
     * Helper function that will check if a variable is empty
59
     * @param mixed $string
60
     * @return bool true if empty, false otherwise
61
     */
62
    private function isEmpty($string)
63
    {
64
        return empty($string);
65
    }
66
67
    /**
68
     * Helper function that will check if an array key exists
69
     * @param mixed $array
70
     * @param string $key key to check
71
     * @return bool true if array & exists, false otherwise
72
     */
73
    private function isArrayAndHasKey($array, $key)
74
    {
75
        if (!is_array($array)) {
76
            return false;
77
        }
78
79
        if (array_key_exists($key, $array)) {
80
            return true;
81
        }
82
83
        return false;
84
    }
85
86
    /**
87
     * @param ParameterBag $query
88
     * @param QueryConfigurationBuilder $builder
89
     */
90
    public function getDrawCall($query, $builder)
91
    {
92
        if ($query->has('draw')) {
93
            $builder->drawCall($query->get('draw'));
94
        }
95
    }
96
97
    /**
98
     * @param ParameterBag $query
99
     * @param QueryConfigurationBuilder $builder
100
     */
101
    public function getStart($query, $builder)
102
    {
103
        if ($query->has('start')) {
104
            $builder->start($query->get('start'));
105
        }
106
    }
107
108
    /**
109
     * @param ParameterBag $query
110
     * @param QueryConfigurationBuilder $builder
111
     */
112
    public function getLength($query, $builder)
113
    {
114
        if ($query->has('length')) {
115
            $builder->length($query->get('length'));
116
        }
117
    }
118
119
    /**
120
     * @param ParameterBag $query
121
     * @param QueryConfigurationBuilder $builder
122
     */
123
    public function getSearch($query, $builder)
124
    {
125
        $search = $query->get('search');
126
127
        if ($this->isArrayAndHasKey($search, 'value')) {
128
            $builder->searchValue($search['value'], null, true);
0 ignored issues
show
Unused Code introduced by
The call to QueryConfigurationBuilder::searchValue() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
        }
130
    }
131
132
    public function getSearchColumns($query, $builder, array $columnConfiguration)
133
    {
134
        // for each column we need to see if there is a search value
135
        $columns = $query->get('columns');
136
137
        foreach ($columnConfiguration as $i => $c) {
138
            // check if there is something search related
139
            if (!isset($columns[$i])) {
140
                continue;
141
            }
142
143
            if ($c->getSearch()->isSearchable()) {
144
                // search for this column is available
145
                $value = $columns[$i]['search']['value'];
146
147
                if (!$this->isEmpty($value)) {
148
                    $builder->columnSearch($c->getName(), $columns[$i]['search']['value'], null, true);
149
                }
150
            }
151
        }
152
    }
153
154
    /**
155
     * @param ParameterBag $query
156
     * @param QueryConfigurationBuilder $builder
157
     */
158
    public function getRegex($query, $builder)
159
    {
160
        $search = $query->get('search');
161
162
        if ($this->isArrayAndHasKey($search, 'regex')) {
163
            $builder->searchRegex($search['regex'], null, true);
0 ignored issues
show
Unused Code introduced by
The call to QueryConfigurationBuilder::searchRegex() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
164
        }
165
    }
166
167
    /**
168
     * @param ParameterBag $query
169
     * @param QueryConfigurationBuilder $builder
170
     * @param ColumnConfiguration[] $columnConfiguration
171
     */
172
    private function getOrder(ParameterBag $query, QueryConfigurationBuilder $builder, array $columnConfiguration)
173
    {
174
        //loop over the order
175
        if(($query->has('order'))) {
176
            $order = $query->get('order');
177
            foreach($order as $i => $config) {
178
                if(array_key_exists($config['column'], $columnConfiguration)) {
179
                    $column = $columnConfiguration[$config['column']];
180
                    if($column->getOrder()->isOrderable()) {
181
                        $builder->columnOrder($column->getName(), $config['dir']);
182
                    }
183
                }
184
            }
185
        }
186
    }
187
}