Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StatsByBrandRequest
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 authorize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 rules
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Http\Requests\Bo;
4
5use Illuminate\Foundation\Http\FormRequest;
6use Illuminate\Support\Carbon;
7use Illuminate\Validation\ValidationException;
8
9class StatsByBrandRequest extends FormRequest
10{
11    /**
12     * Determine if the user is authorized to make this request.
13     *
14     * @return boolean
15     */
16    public function authorize(): bool
17    {
18        return auth('backend')->check();
19    }
20
21    /**
22     * Get the validation rules that apply to the request.
23     *
24     * @return array
25     * @throws \Illuminate\Validation\ValidationException If given dates are unparsable of period
26     *                                                    is bigger than 1 year.
27     * @throws \Exception If carbon date processing failed.
28     */
29    public function rules(): array
30    {
31        if ($this->post()) {
32            try {
33                if ((Carbon::parse($this->end)->diffInDays(Carbon::parse($this->start)) + 1) > 365) {
34                    throw ValidationException::withMessages([
35                        'Vous devez sélectionner une période de maximum 1 ans'
36                    ]);
37                }
38            } catch (\Exception $e) {
39                if (\get_class($e) !== ValidationException::class) {
40                    throw ValidationException::withMessages([
41                        'Les dates séléectionnées ne sembles pas être valides'
42                    ]);
43                }
44                throw $e;
45            }
46            return [
47                'brand' => 'required|exists:brands,id',
48                'start' => 'required|date|date_format:Y-m-d',
49                'end'   => 'required|date|date_format:Y-m-d|after_or_equal:start',
50            ];
51        } //end if
52        return [];
53    }
54}