Completed
Push — master ( a098b4...5a38a9 )
by Renato
05:12
created

FilterDate::filter()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 8
nop 3
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 8
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace NwLaravel\Repositories\Criterias\Filters;
4
5
use Datetime;
6
7
class FilterDate implements FilterInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $operator;
13
14
    /**
15
     * @var mixed
16
     */
17
    private $grammar;
18
19
    /**
20
     * Construct
21
     *
22
     * @param string $operator
23
     */
24 16
    public function __construct($operator = '=')
25
    {
26 16
        $this->operator = $operator;
27 16
    }
28
29
    /**
30
     * Filter
31
     *
32
     * @param Query\Builder $query
33
     * @param int|string    $key
34
     * @param mixed         $value
35
     *
36
     * @return boolean
37
     */
38 15
    public function filter($query, $key, $value)
39
    {
40 15
        if (is_string($value)) {
41 8
            $value = str_replace("_", '', $value); // Caso vier com a mascara __/__/____
42 8
        }
43
44 15
        if (!empty($value) || is_null($value)) {
45 14
            $date = asDateTime($value);
46
47 14
            if ($date instanceof Datetime && !is_null($value)) {
48 11
                $query = $query->whereRaw(
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...
49 11
                    $this->dateFormatDb($query, $key, $this->operator),
50 11
                    [$date->format('Y-m-d')]
51 11
                );
52 11
            } else {
53 4
                if ($this->operator == '!=' || $this->operator == '<>') {
54 2
                    $query = $query->whereNotNull($key);
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...
55 2
                } else {
56 3
                    $query = $query->whereNull($key);
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...
57
                }
58
            }
59
60 14
            return true;
61
        }
62
63 1
        return false;
64
    }
65
66
    /**
67
     * Date Formate Database
68
     *
69
     * @param Builder  $query    Builder
70
     * @param string   $key      Column
71
     * @param string   $operator String Operator
72
     *
73
     * @return string
74
     */
75 11
    private function dateFormatDb($query, $key, $operator)
76
    {
77 11
        if (!$this->grammar) {
78 11
            $this->grammar = $query->getQuery()->getGrammar();
79 11
        }
80
81 11
        $key = $this->grammar->wrap($key);
82
83 11
        $formatDb = sprintf("%s %s ?", $key, $operator);
84
85 11
        switch (true) {
86 11
            case $this->grammar instanceof \Illuminate\Database\Query\Grammars\MySqlGrammar:
87 5
                $formatDb = sprintf("DATE(%s) %s ?", $key, $operator);
88 5
                break;
89
90 6
            case $this->grammar instanceof \Illuminate\Database\Query\Grammars\PostgresGrammar:
91 2
                $formatDb = sprintf("DATE_TRUNC('day', %s) %s ?", $key, $operator);
92 2
                break;
93
94 4
            case $this->grammar instanceof \Illuminate\Database\Query\Grammars\SQLiteGrammar:
95 2
                $formatDb = sprintf("strftime('%%Y-%%m-%%d', %s) %s ?", $key, $operator);
96 2
                break;
97
98 2
            case $this->grammar instanceof \Illuminate\Database\Query\Grammars\SqlServerGrammar:
99 2
                $formatDb = sprintf("CAST(%s AS DATE) %s ?", $key, $operator);
100 2
        }
101
102 11
        return $formatDb;
103
    }
104
}
105