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 ValidationException If given dates are unparsable of period is bigger than 1 year.
30     * @throws \Exception If carbon date processing failed.
31     */
32    public function rules()
33    {
34        if ($this->post()) {
35            try {
36                if ((Carbon::parse($this->end)->diffInDays(Carbon::parse($this->start)) + 1) > 365) {
37                    throw ValidationException::withMessages([
38                        'Vous devez sélectionner une période de maximum 1 ans'
39                    ]);
40                }
41            } catch (Exception $e) {
42                if (\get_class($e) !== ValidationException::class) {
43                    throw ValidationException::withMessages([
44                        'Les dates séléectionnées ne sembles pas être valides'
45                    ]);
46                }
47                throw $e;
48            }
49            return [
50                'question' => 'required|exists:questions,id',
51                'start' => 'required|date|date_format:Y-m-d',
52                'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
53            ];
54        }//end if
55        return [];
56    }
57}