| 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $permissions = Permission::paginate(10); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Permissions', route('admin.role.permission.index')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  | View Code Duplication |     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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function showUpdateForm(Request $request, int $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.