Completed
Push — master ( 0d066b...b259c2 )
by Fèvre
13s
created

CategoryController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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
class CategoryController extends Controller
13
{
14
    /**
15
     * Show all categories.
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
        $categories = Category::paginate(config('xetaravel.pagination.blog.article_per_page'));
22
23
        $this->breadcrumbs->addCrumb('Manage Categories', route('admin.blog.category.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::Blog.category.index', ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]);
26
    }
27
28
    /**
29
     * Show the caterory create form.
30
     *
31
     * @return \Illuminate\View\View
32
     */
33
    public function showCreateForm(): View
34
    {
35
        $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...
36
            ->addCrumb('Manage Categories', route('admin.blog.article.index'))
37
            ->addCrumb("Create", route('admin.blog.article.create'));
38
39
        return view('Admin::Blog.category.create', ['breadcrumbs' => $this->breadcrumbs]);
40
    }
41
42
    /**
43
     * Handle a category create request for the application.
44
     *
45
     * @param \Illuminate\Http\Request $request
46
     *
47
     * @return \Illuminate\Http\RedirectResponse
48
     */
49 View Code Duplication
    public function create(Request $request): 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...
50
    {
51
        CategoryValidator::create($request->all())->validate();
52
53
        if (CategoryRepository::create($request->all())) {
54
            return redirect()
55
                ->route('admin.blog.category.index')
56
                ->with('success', 'Your category has been created successfully !');
57
        }
58
59
        return redirect()
60
            ->route('admin.blog.category.index')
61
            ->with('danger', 'An error occurred while creating your category !');
62
    }
63
64
    /**
65
     * Show the category update form.
66
     *
67
     * @param string $slug The slug of the category.
68
     * @param int $id The id of the category.
69
     *
70
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be RedirectResponse|View|\I...\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...
71
     */
72
    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...
73
    {
74
        $category = Category::find($id);
75
76
        if (is_null($category)) {
77
            return redirect()
78
                ->route('admin.blog.category.index')
79
                ->with('danger', 'This category doesn\'t exist or has been deleted !');
80
        }
81
82
        $breadcrumbs = $this->breadcrumbs
83
            ->addCrumb('Manage Categories', route('admin.blog.category.index'))
84
            ->addCrumb(
85
                "Update : " . e(str_limit($category->title, 30)),
86
                route(
87
                    'admin.blog.category.index',
88
                    ['slug' => $category->slug, 'id' => $category->id]
89
                )
90
            );
91
92
        return view('Admin::Blog.category.update', compact('category', 'breadcrumbs'));
93
    }
94
95
    /**
96
     * Handle an category update request for the application.
97
     *
98
     * @param \Illuminate\Http\Request $request
99
     * @param int $id The id of the category.
100
     *
101
     * @return \Illuminate\Http\RedirectResponse
102
     */
103 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...
104
    {
105
        CategoryValidator::update($request->all(), $id)->validate();
106
107
        $category = Category::find($id);
108
109
        if (CategoryRepository::update($request->all(), $category)) {
110
            return redirect()
111
                ->route('admin.blog.category.index')
112
                ->with('success', 'Your category has been updated successfully !');
113
        }
114
115
        return redirect()
116
            ->route('admin.blog.category.index')
117
            ->with('danger', 'An error occurred while updating your category !');
118
    }
119
120
    /**
121
     * Handle the delete request for the category.
122
     *
123
     * @param int $id The id of the category to delete.
124
     *
125
     * @return \Illuminate\Http\RedirectResponse
126
     */
127 View Code Duplication
    public function delete(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...
128
    {
129
        $category = Category::find($id);
130
131
        if (is_null($category)) {
132
            return redirect()
133
                ->route('admin.blog.category.index')
134
                ->with('danger', 'This category doesn\'t exist or has already been deleted !');
135
        }
136
137
        if ($category->delete()) {
138
            return redirect()
139
                ->route('admin.blog.category.index')
140
                ->with('success', 'This category has been deleted successfully !');
141
        }
142
143
        return redirect()
144
            ->route('admin.blog.category.index')
145
            ->with('danger', 'An error occurred while deleting this category !');
146
    }
147
}
148