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