Passed
Push — develop ( b4818b...dcecff )
by Septianata
16:14
created

SearchRequest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 11
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validationData() 0 7 1
A parseDaterange() 0 5 1
A rules() 0 5 1
1
<?php
2
3
namespace App\Http\Requests\Report\Order;
4
5
use App\Infrastructure\Foundation\Http\FormRequest;
6
use Illuminate\Support\Carbon;
7
8
class SearchRequest extends FormRequest
9
{
10
    /**
11
     * Request value of "start_date".
12
     *
13
     * @var \Illuminate\Support\Carbon
14
     */
15
    public Carbon $start_date;
16
17
    /**
18
     * Request value of "end_date".
19
     *
20
     * @var \Illuminate\Support\Carbon
21
     */
22
    public Carbon $end_date;
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function validationData()
28
    {
29
        $this->parseDaterange($this->input('daterange'));
30
31
        return [
32
            'start_date' => $this->start_date->format('Y-m-d'),
33
            'end_date' => $this->end_date->format('Y-m-d'),
34
        ];
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function rules()
41
    {
42
        return [
43
            'start_date' => 'required|date_format:Y-m-d|after_or_equal:' . Carbon::today()->startOfMonth()->format('Y-m-d'),
44
            'end_date' => 'required|date_format:Y-m-d|before_or_equal:' . Carbon::today()->endOfMonth()->format('Y-m-d'),
45
        ];
46
    }
47
48
    /**
49
     * Parse the given daterange value into start_date and end_date format.
50
     *
51
     * @param  string  $value
52
     * @return array
53
     */
54
    protected function parseDaterange(string $value): array
55
    {
56
        return tap(array_pad(explode(' - ', $value), 2, null), function (array $daterange) {
57
            $this->start_date = Carbon::parse($daterange[0]);
58
            $this->end_date = Carbon::parse($daterange[1]);
59
        });
60
    }
61
}
62