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