CommentRequest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 20 3
A rules() 0 4 1
1
<?php
2
3
namespace FaithGen\SDK\Http\Requests;
4
5
use FaithGen\SDK\Models\Ministry;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Illuminate\Support\Str;
8
9
class CommentRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        if (auth()->user() instanceof Ministry) {
19
            $routeModel = collect($this->route()->parameters());
20
21
            $modelName = $routeModel->keys()->first();
22
23
            $binding = collect(app()->getBindings())
24
                ->filter(function ($binding, $key) use ($modelName) {
25
                    return Str::contains($key, '\\'.ucfirst($modelName)) && Str::endsWith($key, 'Service');
26
                })
27
                ->keys()
28
                ->first();
29
30
            $modelMethod = 'get'.ucwords($modelName);
31
32
            return $this->user()->can('view', app($binding)->$modelMethod());
33
        }
34
35
        return true;
36
    }
37
38
    /**
39
     * Get the validation rules that apply to the request.
40
     *
41
     * @return array
42
     */
43
    public function rules()
44
    {
45
        return [
46
            'comment' => 'required',
47
        ];
48
    }
49
}
50