Completed
Pull Request — master (#22)
by Fèvre
05:46 queued 02:55
created

PermissionController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Role;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Xetaravel\Http\Controllers\Admin\Controller;
8
use Xetaravel\Models\Permission;
9
use Xetaravel\Models\Repositories\PermissionRepository;
10
use Xetaravel\Models\Validators\PermissionValidator;
11
12
class PermissionController extends Controller
13
{
14
    /**
15
     * Show all the permissions.
16
     *
17
     * @return \Illuminate\View\View
18
     */
19 View Code Duplication
    public function index(): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $permissions = Permission::paginate(10);
22
23
        $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Permissions', route('admin.role.permission.index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
        return view('Admin::Role.permission.index', compact('permissions', 'breadcrumbs'));
26
    }
27
28
    /**
29
     * Show the permission create form.
30
     *
31
     * @return \Illuminate\View\View
32
     */
33
    public function showCreateForm(): View
34
    {
35
        $breadcrumbs = $this->breadcrumbs
36
            ->addCrumb('Manage Permissions', route('admin.role.permission.index'))
37
            ->addCrumb("Create", route('admin.role.permission.create'));
38
39
        return view('Admin::Role.permission.create', compact('breadcrumbs'));
40
    }
41
42
    /**
43
     * Handle a permission create request for the application.
44
     *
45
     * @param \Illuminate\Http\Request $request
46
     *
47
     * @return \Illuminate\Http\RedirectResponse
48
     */
49
    public function create(Request $request): RedirectResponse
50
    {
51
        PermissionValidator::create($request->all())->validate();
52
        PermissionRepository::create($request->all());
53
54
        return redirect()
55
            ->route('admin.role.permission.index')
56
            ->with('success', 'This permission has been created successfully !');
57
    }
58
59
    /**
60
     * Show the update form.
61
     *
62
     * @param \Illuminate\Http\Request $request
63
     * @param int $id The id of the permission.
64
     *
65
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be View|\Illuminate\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...
66
     */
67
    public function showUpdateForm(Request $request, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        $permission = Permission::findOrFail($id);
70
71
        $breadcrumbs = $this->breadcrumbs
72
            ->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
73
            ->addCrumb('Manage Permissions', route('admin.role.permission.index'))
74
            ->addCrumb(
75
                'Update ' . e($permission->name),
76
                route('admin.role.permission.update', $permission->slug, $permission->id)
77
            );
78
79
        return view(
80
            'Admin::Role.permission.update',
81
            compact('permission', 'breadcrumbs')
82
        );
83
    }
84
85
    /**
86
     * Handle an permission update request for the application.
87
     *
88
     * @param \Illuminate\Http\Request $request
89
     * @param int $id The id of the permission to update.
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93 View Code Duplication
    public function update(Request $request, int $id): RedirectResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $permission = Permission::findOrFail($id);
96
97
        PermissionValidator::update($request->all(), $permission->id)->validate();
98
        PermissionRepository::update($request->all(), $permission);
99
100
        return redirect()
101
            ->route('admin.role.permission.index')
102
            ->with('success', 'This permission has been updated successfully !');
103
    }
104
105
    /**
106
     * Handle the delete request for the permission.
107
     *
108
     * @param int $id The id of the permission to delete.
109
     *
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function delete(int $id): RedirectResponse
113
    {
114
        $permission = Permission::findOrFail($id);
115
116
        if (!$permission->is_deletable) {
117
            return redirect()
118
                ->route('admin.role.permission.index')
119
                ->with('danger', 'You can not delete this permission !');
120
        }
121
122
        if ($permission->delete()) {
123
            return redirect()
124
                ->route('admin.role.permission.index')
125
                ->with('success', 'This permission has been deleted successfully !');
126
        }
127
128
        return redirect()
129
            ->route('admin.role.permission.index')
130
            ->with('danger', 'An error occurred while deleting this permission !');
131
    }
132
}
133