DateFilter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 49
rs 10
ccs 23
cts 24
cp 0.9583
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A range() 0 5 1
A isRange() 0 3 1
A render() 0 9 2
A apply() 0 21 3
1
<?php
2
3
namespace Yaro\Jarboe\Table\Filters;
4
5
class DateFilter extends AbstractFilter
6
{
7
    private $range = false;
8
9 2
    public function range(bool $enable = true)
10
    {
11 2
        $this->range = $enable;
12
13 2
        return $this;
14
    }
15
16 3
    public function isRange(): bool
17
    {
18 3
        return $this->range;
19
    }
20
21 1
    public function render()
22
    {
23 1
        $view = 'date';
24 1
        if ($this->isRange()) {
25
            $view .= '_range';
26
        }
27
28 1
        return view('jarboe::crud.filters.'. $view, [
29 1
            'filter' => $this,
30
        ]);
31
    }
32
33 2
    public function apply($model)
34
    {
35 2
        $value = $this->value();
36 2
        if (is_null($value)) {
37 1
            return;
38
        }
39
40 1
        if ($this->isRange()) {
41
            $model->when($value['from'] ?? null, function ($query, $value) {
42 1
                return $query->whereDate($this->field()->name(), '>=', $value);
43 1
            });
44
            $model->when($value['to'] ?? null, function ($query, $value) {
45 1
                return $query->whereDate($this->field()->name(), '<=', $value);
46 1
            });
47 1
            return;
48
        }
49
50 1
        $model->whereDate(
51 1
            $this->field()->name(),
52 1
            $this->sign,
53
            $value
54
        );
55 1
    }
56
}
57