Completed
Push — develop ( 3c0bec...e16864 )
by Abdelrahman
01:47
created

AbilitiesController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 109
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A store() 0 4 1
A update() 0 4 1
A delete() 0 9 1
A form() 0 4 1
B process() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Backend;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Fort\Models\Ability;
9
use Cortex\Foundation\Http\Controllers\AuthorizedController;
10
11
class AbilitiesController extends AuthorizedController
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected $resource = 'abilities';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $resourceActionWhitelist = ['grant'];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceActionWhitelist exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
22
23
    /**
24
     * Display a listing of the resource.
25
     *
26
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28
    public function index()
29
    {
30
        $abilities = Ability::paginate(config('rinvex.fort.backend.items_per_page'));
31
32
        return view('cortex/fort::backend.abilities.index', compact('abilities'));
33
    }
34
35
    /**
36
     * Store a newly created resource in storage.
37
     *
38
     * @param \Illuminate\Http\Request $request
39
     *
40
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    public function store(Request $request)
43
    {
44
        return $this->process($request, new Ability());
45
    }
46
47
    /**
48
     * Update the given resource in storage.
49
     *
50
     * @param \Illuminate\Http\Request    $request
51
     * @param \Rinvex\Fort\Models\Ability $ability
52
     *
53
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
54
     */
55
    public function update(Request $request, Ability $ability)
56
    {
57
        return $this->process($request, $ability);
58
    }
59
60
    /**
61
     * Delete the given resource from storage.
62
     *
63
     * @param \Rinvex\Fort\Models\Ability $ability
64
     *
65
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    public function delete(Ability $ability)
68
    {
69
        $ability->delete();
70
71
        return intend([
72
            'url' => route('backend.abilities.index'),
73
            'with' => ['warning' => trans('cortex/fort::messages.ability.deleted', ['abilityId' => $ability->id])],
74
        ]);
75
    }
76
77
    /**
78
     * Show the form for create/update of the given resource.
79
     *
80
     * @param \Rinvex\Fort\Models\Ability $ability
81
     *
82
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
83
     */
84
    public function form(Ability $ability)
85
    {
86
        return view('cortex/fort::backend.abilities.form', compact('ability', 'resources'));
87
    }
88
89
    /**
90
     * Process the form for store/update of the given resource.
91
     *
92
     * @param \Illuminate\Http\Request    $request
93
     * @param \Rinvex\Fort\Models\Ability $ability
94
     *
95
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    protected function process(Request $request, Ability $ability)
98
    {
99
        // Prepare required input fields
100
        $input = $request->all();
101
102
        // Verify valid policy
103
        if (! empty($input['policy']) && (($class = mb_strstr($input['policy'], '@', true)) === false || ! method_exists($class, str_replace('@', '', mb_strstr($input['policy'], '@'))))) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 188 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104
            return intend([
105
                'back' => true,
106
                'withInput' => $request->all(),
107
                'withErrors' => ['policy' => trans('cortex/fort::messages.ability.invalid_policy')],
108
            ]);
109
        }
110
111
        // Save ability
112
        $ability->fill($input)->save();
113
114
        return intend([
115
            'url' => route('backend.abilities.index'),
116
            'with' => ['success' => trans('cortex/fort::messages.ability.saved', ['abilityId' => $ability->id])],
117
        ]);
118
    }
119
}
120