OrderSearchComponent   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 25
dl 0
loc 65
ccs 25
cts 26
cp 0.9615
rs 10
c 2
b 1
f 0
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getSearchConditions() 0 28 8
A getOrder() 0 20 4
1
<?php
2
3
/**
4
 * Order and search component handling generation of ordering and
5
 * searching conditions in loading data tables.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Controller\Component;
21
22
use Cake\Controller\Component;
23
use Cake\Http\ServerRequest;
24
25
use function array_keys;
26
use function count;
27
use function intval;
28
29
/**
30
 * Github api component handling comunication with github.
31
 */
32
class OrderSearchComponent extends Component
33
{
34
    /**
35
     * Indexes are +1'ed because first column is of checkboxes
36
     * and hence it should be ignored.
37
     *
38
     * @param string[] $aColumns The columns
39
     *
40
     * @return array
41
     */
42 14
    public function getSearchConditions(array $aColumns, ServerRequest $request): array
43
    {
44 14
        $searchConditions = ['OR' => []];
45 14
        $keys = array_keys($aColumns);
46 14
        $columnsCount = count($aColumns);
47
48 14
        $sSearch = $request->getQuery('sSearch');
49 14
        if ($sSearch !== '' && $sSearch !== null) {
50 7
            for ($i = 0; $i < $columnsCount; ++$i) {
51 7
                if ($request->getQuery('bSearchable_' . ($i + 1)) !== 'true') {
52 7
                    continue;
53
                }
54
55 7
                $searchConditions['OR'][] = [$aColumns[$keys[$i]] . ' LIKE' => '%' . $sSearch . '%'];
0 ignored issues
show
Bug introduced by
Are you sure $sSearch of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                $searchConditions['OR'][] = [$aColumns[$keys[$i]] . ' LIKE' => '%' . /** @scrutinizer ignore-type */ $sSearch . '%'];
Loading history...
56
            }
57
        }
58
59
        /* Individual column filtering */
60 14
        for ($i = 0; $i < $columnsCount; ++$i) {
61 14
            $searchTerm = $request->getQuery('sSearch_' . ($i + 1));
62 14
            if ($searchTerm === '' || $searchTerm === null) {
63 14
                continue;
64
            }
65
66 7
            $searchConditions[] = [$aColumns[$keys[$i]] . ' LIKE' => $searchTerm];
67
        }
68
69 14
        return $searchConditions;
70
    }
71
72
    /**
73
     * @param string[] $aColumns The columns
74
     *
75
     * @return array|null
76
     */
77 14
    public function getOrder(array $aColumns, ServerRequest $request): ?array
78
    {
79 14
        if ($request->getQuery('iSortCol_0') !== null) {
80 7
            $order = [];
81
            //Seems like we need to sort with only one column each time, so no need to loop
82 7
            $sort_column_index = intval($request->getQuery('iSortCol_0'));
83
84 7
            $keys = array_keys($aColumns);
85
86
            if (
87 7
                $sort_column_index > 0
88 7
                && $request->getQuery('bSortable_' . $sort_column_index) === 'true'
89
            ) {
90
                $order[$aColumns[$keys[$sort_column_index - 1]]] = $request->getQuery('sSortDir_0');
91
            }
92
93 7
            return $order;
94
        }
95
96 14
        return null;
97
    }
98
}
99