Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetQuestionStatsRequest
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
0.00% covered (danger)
0.00%
0 / 1
 rules
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2
3namespace App\Http\Requests\Api\V2;
4
5use App\Http\Requests\Api\V2\BaseApiRequest;
6use Exception;
7use Illuminate\Support\Carbon;
8use Illuminate\Validation\ValidationException;
9
10class GetQuestionStatsRequest extends BaseApiRequest
11{
12    /**
13     * Get the validation rules that apply to the request.
14     *
15     * @return array
16     * @throws \Illuminate\Validation\ValidationException If given dates are unparsable of period
17     * is bigger than 3 months.
18     * @throws \Exception If carbon date processing failed.
19     */
20    public function rules(): array
21    {
22        try {
23            if (Carbon::parse($this->end)->floatDiffInMonths(Carbon::parse($this->start)) > 3) {
24                throw ValidationException::withMessages([
25                    'Vous devez sélectionner une période de maximum 3 mois'
26                ]);
27            }
28        } catch (Exception $e) {
29            if (\get_class($e) !== ValidationException::class) {
30                throw ValidationException::withMessages([
31                    'Les dates sélectionnées ne sembles pas être valides'
32                ]);
33            }
34            throw $e;
35        }
36        return [
37            'start' => 'required|date|date_format:Y-m-d',
38            'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
39        ];
40    }
41}