| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Xetaravel\Http\Controllers\Admin\Discuss; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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\DiscussCategory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Xetaravel\Models\Repositories\DiscussCategoryRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Xetaravel\Models\Validators\DiscussCategoryValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 12 |  | View Code Duplication | class CategoryController extends Controller | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->breadcrumbs->addCrumb('Discuss', route('admin.discuss.category.index')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Show all categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return \Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function index(): View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $categories = DiscussCategory::paginate(config('xetaravel.pagination.discuss.conversation_per_page')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->breadcrumbs->addCrumb('Manage Categories', route('admin.discuss.category.index')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'Admin::Discuss.category.index', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Show the caterory create form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return \Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function showCreateForm(): View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $breadcrumbs = $this->breadcrumbs | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->addCrumb('Manage Categories', route('admin.discuss.category.index')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->addCrumb("Create", route('admin.discuss.category.create')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return view('Admin::Discuss.category.create', ['breadcrumbs' => $this->breadcrumbs]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Handle a category create request for the application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param \Illuminate\Http\Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function create(Request $request): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         DiscussCategoryValidator::create($request->all())->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         DiscussCategoryRepository::create($request->all()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->route('admin.discuss.category.index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->with('success', 'Your category has been created successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Show the category update form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $slug The slug of the category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param int $id The id of the category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     public function showUpdateForm(string $slug, int $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $category = DiscussCategory::findOrFail($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $breadcrumbs = $this->breadcrumbs | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             ->addCrumb('Manage Categories', route('admin.discuss.category.index')) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             ->addCrumb( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 "Update : " . e(str_limit($category->title, 30)), | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 route( | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                     'admin.discuss.category.index', | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                     ['slug' => $category->slug, 'id' => $category->id] | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         return view('Admin::Discuss.category.update', compact('category', 'breadcrumbs')); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Handle an category update request for the application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param \Illuminate\Http\Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param int $id The id of the category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function update(Request $request, int $id): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $category = DiscussCategory::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         DiscussCategoryValidator::update($request->all(), $id)->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         DiscussCategoryRepository::update($request->all(), $category); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ->route('admin.discuss.category.index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ->with('success', 'Your category has been updated successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Handle the delete request for the category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param int $id The id of the category to delete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function delete(int $id): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $category = DiscussCategory::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         if ($category->delete()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             return redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 ->route('admin.discuss.category.index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 ->with('success', 'This category has been deleted successfully !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ->route('admin.discuss.category.index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ->with('danger', 'An error occurred while deleting this category !'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  | 
            
                        
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.