CategoriesController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 140
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 4 1
A create() 0 6 1
A store() 10 10 1
A edit() 0 8 2
A update() 10 10 1
A destroy() 0 14 3
A publish() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\DataTables\CategoriesDataTable;
6
use Yajra\CMS\Entities\Category;
7
use Yajra\CMS\Http\Requests\CategoriesFormRequest;
8
9
class CategoriesController extends Controller
10
{
11
    /**
12
     * Controller specific permission ability map.
13
     *
14
     * @var array
15
     */
16
    protected $customPermissionMap = [
17
        'publish' => 'update',
18
    ];
19
20
    /**
21
     * CategoriesController constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->authorizePermissionResource('category');
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @param  \Yajra\CMS\DataTables\CategoriesDataTable $dataTable
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function index(CategoriesDataTable $dataTable)
35
    {
36
        return $dataTable->render('administrator.categories.index');
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @param  \Yajra\CMS\Entities\Category $category
43
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
44
     */
45
    public function create(Category $category)
46
    {
47
        $category->published = true;
48
49
        return view('administrator.categories.create', compact('category'));
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param  \Yajra\CMS\Http\Requests\CategoriesFormRequest $request
56
     * @param  \Yajra\CMS\Entities\Category $category
57
     * @return \Illuminate\Http\Response
58
     */
59 View Code Duplication
    public function store(CategoriesFormRequest $request, Category $category)
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...
60
    {
61
        $category->fill($request->all());
62
        $category->published     = $request->get('published', false);
63
        $category->authenticated = $request->get('authenticated', false);
64
        $category->save();
65
        flash()->success(trans('cms::categories.alert.success', ['stat' => 'Created']));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::categories.a...y('stat' => 'Created')) targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
67
        return redirect()->route('administrator.categories.index');
68
    }
69
70
    /**
71
     * Show the form for editing the specified resource.
72
     *
73
     * @param  \Yajra\CMS\Entities\Category $category
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function edit(Category $category)
77
    {
78
        if ($category->isRoot()) {
79
            abort(404);
80
        }
81
82
        return view('administrator.categories.edit', compact('category'));
83
    }
84
85
    /**
86
     * Update the specified resource in storage.
87
     *
88
     * @param  CategoriesFormRequest $request
89
     * @param  \Yajra\CMS\Entities\Category $category
90
     * @return \Illuminate\Http\Response
91
     */
92 View Code Duplication
    public function update(CategoriesFormRequest $request, Category $category)
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...
93
    {
94
        $category->fill($request->all());
95
        $category->published     = $request->get('published', false);
96
        $category->authenticated = $request->get('authenticated', false);
97
        $category->save();
98
        flash()->success(trans('cms::categories.alert.success', ['stat' => 'Updated']));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::categories.a...y('stat' => 'Updated')) targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
100
        return redirect()->route('administrator.categories.index');
101
    }
102
103
    /**
104
     * Remove the specified resource from storage.
105
     *
106
     * @param  \Yajra\CMS\Entities\Category $category
107
     * @return \Illuminate\Http\JsonResponse
108
     */
109
    public function destroy(Category $category)
110
    {
111
        if ($category->isRoot()) {
112
            abort(404);
113
        }
114
115
        if ($category->has('articles')) {
116
            return $this->notifyError(trans('cms::categories.destroy.error'));
117
        }
118
119
        $category->delete();
120
121
        return $this->notifySuccess(trans('cms::categories.destroy.success'));
122
    }
123
124
    /**
125
     * Button Response from DataTable request to publish status.
126
     *
127
     * @param  \Yajra\CMS\Entities\Category $category
128
     * @return \Illuminate\Http\JsonResponse
129
     */
130
    public function publish(Category $category)
131
    {
132
        $category->togglePublishedState();
133
134
        $category->getDescendants()->each(
135
            function (Category $cat) use ($category) {
136
                $cat->published = $category->published;
137
                $cat->save();
138
            }
139
        );
140
141
        return $this->notifySuccess(
142
            sprintf(
143
                'Category successfully %s!',
144
                $category->published ? 'published' : 'unpublished'
145
            )
146
        );
147
    }
148
}
149