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\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
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('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
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...
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
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...
79
     */
80
    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...
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