Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php namespace Modules\Recipe\Http\Controllers\Admin; |
||
| 10 | class RecipeController extends AdminBaseController |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var RecipeRepository |
||
| 14 | */ |
||
| 15 | private $recipe; |
||
| 16 | /** |
||
| 17 | * @var FileRepository |
||
| 18 | */ |
||
| 19 | private $file; |
||
| 20 | |||
| 21 | public function __construct(RecipeRepository $recipe, FileRepository $file) |
||
| 22 | { |
||
| 23 | parent::__construct(); |
||
| 24 | |||
| 25 | $this->recipe = $recipe; |
||
| 26 | $this->file = $file; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Display a listing of the resource. |
||
| 31 | * |
||
| 32 | * @return Response |
||
| 33 | */ |
||
| 34 | public function index() |
||
| 35 | { |
||
| 36 | $recipes = $this->recipe->all(); |
||
| 37 | |||
| 38 | return view('recipe::admin.recipes.index', compact('recipes')); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Show the form for creating a new resource. |
||
| 43 | * |
||
| 44 | * @return Response |
||
| 45 | */ |
||
| 46 | public function create() |
||
| 47 | { |
||
| 48 | return view('recipe::admin.recipes.create'); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Store a newly created resource in storage. |
||
| 53 | * |
||
| 54 | * @param Request $request |
||
| 55 | * @return Response |
||
| 56 | */ |
||
| 57 | public function store(Request $request) |
||
| 58 | { |
||
| 59 | $this->recipe->create($request->all()); |
||
| 60 | |||
| 61 | flash()->success(trans('core::core.messages.resource created', ['name' => trans('recipe::recipes.title.recipes')])); |
||
| 62 | |||
| 63 | return redirect()->route('admin.recipe.recipe.index'); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Show the form for editing the specified resource. |
||
| 68 | * |
||
| 69 | * @param Recipe $recipe |
||
| 70 | * @return Response |
||
| 71 | */ |
||
| 72 | public function edit(Recipe $recipe) |
||
| 73 | { |
||
| 74 | $galleryFiles = $this->file->findMultipleFilesByZoneForEntity('gallery', $recipe); |
||
| 75 | $featured_image = $this->file->findFileByZoneForEntity('featured_image', $recipe); |
||
| 76 | |||
| 77 | return view('recipe::admin.recipes.edit', compact('recipe', 'galleryFiles', 'featured_image')); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Update the specified resource in storage. |
||
| 82 | * |
||
| 83 | * @param Recipe $recipe |
||
| 84 | * @param Request $request |
||
| 85 | * @return Response |
||
| 86 | */ |
||
| 87 | public function update(Recipe $recipe, Request $request) |
||
| 88 | { |
||
| 89 | $this->recipe->update($recipe, $request->all()); |
||
| 90 | |||
| 91 | flash()->success(trans('core::core.messages.resource updated', ['name' => trans('recipe::recipes.title.recipes')])); |
||
| 92 | |||
| 93 | return redirect()->route('admin.recipe.recipe.index'); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Remove the specified resource from storage. |
||
| 98 | * |
||
| 99 | * @param Recipe $recipe |
||
| 100 | * @return Response |
||
| 101 | */ |
||
| 102 | public function destroy(Recipe $recipe) |
||
| 103 | { |
||
| 104 | $this->recipe->destroy($recipe); |
||
| 105 | |||
| 106 | flash()->success(trans('core::core.messages.resource deleted', ['name' => trans('recipe::recipes.title.recipes')])); |
||
| 107 | |||
| 108 | return redirect()->route('admin.recipe.recipe.index'); |
||
| 109 | } |
||
| 110 | } |
||
| 111 |