Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
16 / 20
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CopyTemplateRequest
80.00% covered (warning)
80.00%
16 / 20
66.67% covered (warning)
66.67%
2 / 3
6.29
0.00% covered (danger)
0.00%
0 / 1
 authorize
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 failedAuthorization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rules
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
4.30
1<?php
2
3namespace App\Http\Requests;
4
5use Illuminate\Auth\Access\AuthorizationException;
6use Illuminate\Foundation\Http\FormRequest;
7use Illuminate\Support\Facades\DB;
8
9class CopyTemplateRequest extends FormRequest
10{
11    /**
12     * Determine if the user is authorized to make this request.
13     */
14    public function authorize(): bool
15    {
16        return DB::table('shortcuts')->where([
17            ['template_id', $this->template_id],
18            ['user_id', request()->user()->getKey()],
19        ])->doesntExist();
20    }
21
22    /**
23     * Handle a failed authorization attempt.
24     *
25     *
26     * @throws \Illuminate\Auth\Access\AuthorizationException
27     */
28    protected function failedAuthorization(): void
29    {
30        throw new AuthorizationException('This flyplate has been added to your flycuts already');
31    }
32
33    /**
34     * Get the validation rules that apply to the request.
35     */
36    public function rules(): array
37    {
38        $rules = [
39            'template_id' => 'required|string|exists:templates,_id',
40            'category_id' => ['required'],
41            'type' => 'required|string|in:category,sub_categories_lv1,sub_categories_lv2',
42        ];
43
44        switch ($this->type) {
45            case 'sub_categories_lv2':
46                array_push($rules['category_id'], 'exists:shortcut_sub_categories_lv2,_id');
47                break;
48            case 'sub_categories_lv1':
49                array_push($rules['category_id'], 'exists:shortcut_sub_categories_lv1,_id');
50                break;
51
52            default:
53                array_push($rules['category_id'], 'exists:shortcut_categories,_id');
54                break;
55        }
56
57        return $rules;
58    }
59}