Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
77.78% covered (warning)
77.78%
7 / 9
GetQuestionStatsRequest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
4.18
77.78% covered (warning)
77.78%
7 / 9
 rules
0.00% covered (danger)
0.00%
0 / 1
4.18
77.78% covered (warning)
77.78%
7 / 9
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 ValidationException If given dates are unparsable of period is bigger than 3 months.
17     * @throws \Exception If carbon date processing failed.
18     */
19    public function rules(): array
20    {
21        try {
22            if (Carbon::parse($this->end)->diffInMonths(Carbon::parse($this->start)->addDay()) >= 3) {
23                throw ValidationException::withMessages([
24                    'Vous devez sélectionner une période de maximum 3 mois'
25                ]);
26            }
27        } catch (Exception $e) {
28            if (\get_class($e) !== ValidationException::class) {
29                throw ValidationException::withMessages([
30                    'Les dates sélectionnées ne sembles pas être valides'
31                ]);
32            }
33            throw $e;
34        }
35        return [
36            'start' => 'required|date|date_format:Y-m-d',
37            'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
38        ];
39    }
40}