NavigationController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 23.02 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 29
loc 126
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 4 1
A create() 0 4 1
A store() 15 15 1
A edit() 0 4 1
A update() 14 14 1
A destroy() 0 6 1
A publish() 0 9 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\NavigationDataTable;
6
use Yajra\CMS\Entities\Navigation;
7
use App\Http\Requests;
8
use Illuminate\Http\Request;
9
10
class NavigationController extends Controller
11
{
12
    /**
13
     * Controller specific permission ability map.
14
     *
15
     * @var array
16
     */
17
    protected $customPermissionMap = [
18
        'publish' => 'update',
19
    ];
20
21
    /**
22
     * NavigationController constructor.
23
     */
24
    public function __construct()
25
    {
26
        $this->authorizePermissionResource('navigation');
27
    }
28
29
    /**
30
     * Display list of navigation.
31
     *
32
     * @param \Yajra\CMS\DataTables\NavigationDataTable $dataTable
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
34
     */
35
    public function index(NavigationDataTable $dataTable)
36
    {
37
        return $dataTable->render('administrator.navigation.index');
38
    }
39
40
    /**
41
     * Show navigation form.
42
     *
43
     * @param \Yajra\CMS\Entities\Navigation $navigation
44
     * @return mixed
45
     */
46
    public function create(Navigation $navigation)
47
    {
48
        return view('administrator.navigation.create', compact('navigation'));
49
    }
50
51
    /**
52
     * Store a newly created navigation.
53
     *
54
     * @param \Illuminate\Http\Request $request
55
     * @return mixed
56
     */
57 View Code Duplication
    public function store(Request $request)
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...
58
    {
59
        $this->validate($request, [
60
            'title' => 'required|max:255',
61
            'type'  => 'required|max:255|alpha|unique:navigation,type',
62
        ]);
63
64
        $navigation = new Navigation;
65
        $navigation->fill($request->all());
66
        $navigation->published = $request->get('published', false);
67
        $navigation->save();
68
        flash()->success(trans('cms::navigation.store.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::navigation.store.success') 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...
69
70
        return redirect()->route('administrator.navigation.index');
71
    }
72
73
    /**
74
     * Show and edit selected navigation.
75
     *
76
     * @param \Yajra\CMS\Entities\Navigation $navigation
77
     * @return mixed
78
     */
79
    public function edit(Navigation $navigation)
80
    {
81
        return view('administrator.navigation.edit', compact('navigation'));
82
    }
83
84
    /**
85
     * Update selected navigation.
86
     *
87
     * @param \Yajra\CMS\Entities\Navigation $navigation
88
     * @param \Illuminate\Http\Request $request
89
     * @return mixed
90
     */
91 View Code Duplication
    public function update(Navigation $navigation, Request $request)
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...
92
    {
93
        $this->validate($request, [
94
            'title' => 'required|max:255',
95
            'type'  => 'required|max:255|alpha|unique:navigation,type,' . $navigation->id,
96
        ]);
97
98
        $navigation->fill($request->all());
99
        $navigation->published = $request->get('published', false);
100
        $navigation->save();
101
        flash()->success(trans('cms::navigation.update.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::navigation.update.success') 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...
102
103
        return redirect()->route('administrator.navigation.index');
104
    }
105
106
    /**
107
     * Remove selected navigation.
108
     *
109
     * @param \Yajra\CMS\Entities\Navigation $navigation
110
     * @return string
111
     * @throws \Exception
112
     */
113
    public function destroy(Navigation $navigation)
114
    {
115
        $navigation->delete();
116
117
        return $this->notifySuccess(trans('cms::navigation.destroy.success'));
118
    }
119
120
    /**
121
     * Publish/Unpublish a navigation.
122
     *
123
     * @param \Yajra\CMS\Entities\Navigation $navigation
124
     * @return \Illuminate\Http\JsonResponse
125
     */
126
    public function publish(Navigation $navigation)
127
    {
128
        $navigation->togglePublishedState();
129
130
        return $this->notifySuccess(sprintf(
131
            trans('cms::navigation.update.publish'),
132
            $navigation->published ? 'published' : 'unpublished'
133
        ));
134
    }
135
}
136