Completed
Push — master ( 75f0c6...9ce00e )
by Renato
04:56
created

FilterSearch::filter()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7.0368

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 2
nop 3
dl 0
loc 29
ccs 20
cts 22
cp 0.9091
crap 7.0368
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace NwLaravel\Repositories\Criterias\Filters;
4
5
class FilterSearch implements FilterInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $nameSearchable;
11
12
    /**
13
     * @var array
14
     */
15
    protected $searchables;
16
17
    /**
18
     * @var string
19
     */
20
    protected $table = '';
21
22
    /**
23
     * Construct
24
     *
25
     * @param string $nameSearchable
26
     * @param array  $searchables
27
     * @param string $table
28
     */
29 16
    public function __construct($nameSearchable, $searchables, $table = '')
30
    {
31 16
        $this->nameSearchable = $nameSearchable;
32 16
        $this->searchables = $searchables;
33 16
        $this->table = $table;
34 16
    }
35
36
    /**
37
     * Filter
38
     *
39
     * @param Query\Builder $query
40
     * @param int|string    $key
41
     * @param mixed         $value
42
     *
43
     * @return boolean
44
     */
45 15
    public function filter($query, $key, $value)
46
    {
47 15
        if ($key === $this->nameSearchable) {
48 3
            $fieldsSearchable = $this->searchables;
49 3
            $query = $query->where(function ($query) use ($fieldsSearchable, $value) {
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50 3
                foreach ($fieldsSearchable as $field => $condition) {
51 3
                    if (is_numeric($field)) {
52 3
                        $field = $condition;
53 3
                        $condition = "=";
54 3
                    }
55
56 3
                    $condition  = trim(strtolower($condition));
57
58 3
                    if (!empty($value)) {
59 2
                        $search = in_array($condition, ["like", "ilike"]) ? "%{$value}%" : $value;
60 2
                        if ($field == 'id') {
61
                            $search = intval($search);
62
                        }
63 2
                        $query->orWhere($this->table.'.'.$field, $condition, $search);
64 2
                    }
65 3
                }
66 3
                return $query;
67 3
            });
68
69 3
            return true;
70
        }
71
72 13
        return false;
73
    }
74
}
75