Completed
Pull Request — master (#45)
by Fèvre
32:37
created

CategoryController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Blog;
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\Category;
9
use Xetaravel\Models\Repositories\CategoryRepository;
10
use Xetaravel\Models\Validators\CategoryValidator;
11
12 View Code Duplication
class CategoryController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Constructor.
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct();
20
21
        $this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
22
    }
23
24
    /**
25
     * Show all categories.
26
     *
27
     * @return \Illuminate\View\View
28
     */
29
    public function index(): View
30
    {
31
        $categories = Category::paginate(config('xetaravel.pagination.blog.article_per_page'));
32
33
        $this->breadcrumbs->addCrumb('Manage Categories', route('admin.blog.category.index'));
34
35
        return view('Admin::Blog.category.index', ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]);
36
    }
37
38
    /**
39
     * Show the caterory create form.
40
     *
41
     * @return \Illuminate\View\View
42
     */
43
    public function showCreateForm(): View
44
    {
45
        $breadcrumbs = $this->breadcrumbs
0 ignored issues
show
Unused Code introduced by
$breadcrumbs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
            ->addCrumb('Manage Categories', route('admin.blog.category.index'))
47
            ->addCrumb("Create", route('admin.blog.category.create'));
48
49
        return view('Admin::Blog.category.create', ['breadcrumbs' => $this->breadcrumbs]);
50
    }
51
52
    /**
53
     * Handle a category create request for the application.
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     *
57
     * @return \Illuminate\Http\RedirectResponse
58
     */
59
    public function create(Request $request): RedirectResponse
60
    {
61
        CategoryValidator::create($request->all())->validate();
62
        CategoryRepository::create($request->all());
63
64
        return redirect()
65
            ->route('admin.blog.category.index')
66
            ->with('success', 'Your category has been created successfully !');
67
    }
68
69
    /**
70
     * Show the category update form.
71
     *
72
     * @param string $slug The slug of the category.
73
     * @param int $id The id of the category.
74
     *
75
     * @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...
76
     */
77
    public function showUpdateForm(string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $slug 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...
78
    {
79
        $category = Category::findOrFail($id);
80
81
        $breadcrumbs = $this->breadcrumbs
82
            ->addCrumb('Manage Categories', route('admin.blog.category.index'))
83
            ->addCrumb(
84
                "Update : " . e(str_limit($category->title, 30)),
85
                route(
86
                    'admin.blog.category.index',
87
                    ['slug' => $category->slug, 'id' => $category->id]
88
                )
89
            );
90
91
        return view('Admin::Blog.category.update', compact('category', 'breadcrumbs'));
92
    }
93
94
    /**
95
     * Handle an category update request for the application.
96
     *
97
     * @param \Illuminate\Http\Request $request
98
     * @param int $id The id of the category.
99
     *
100
     * @return \Illuminate\Http\RedirectResponse
101
     */
102
    public function update(Request $request, int $id): RedirectResponse
103
    {
104
        $category = Category::findOrFail($id);
105
106
        CategoryValidator::update($request->all(), $id)->validate();
107
        CategoryRepository::update($request->all(), $category);
108
109
        return redirect()
110
            ->route('admin.blog.category.index')
111
            ->with('success', 'Your category has been updated successfully !');
112
    }
113
114
    /**
115
     * Handle the delete request for the category.
116
     *
117
     * @param int $id The id of the category to delete.
118
     *
119
     * @return \Illuminate\Http\RedirectResponse
120
     */
121
    public function delete(int $id): RedirectResponse
122
    {
123
        $category = Category::findOrFail($id);
124
125
        if ($category->delete()) {
126
            return redirect()
127
                ->route('admin.blog.category.index')
128
                ->with('success', 'This category has been deleted successfully !');
129
        }
130
131
        return redirect()
132
            ->route('admin.blog.category.index')
133
            ->with('danger', 'An error occurred while deleting this category !');
134
    }
135
}
136