UsersController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 27.54 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 96.3%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
c 3
b 0
f 1
lcom 1
cbo 10
dl 38
loc 138
ccs 52
cts 54
cp 0.963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 7 1
A create() 0 8 1
A store() 17 17 2
B edit() 0 24 3
A update() 21 21 3
A delete() 0 14 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
/**
4
 * @package     Dashboard
5
 * @author      Ian Olson <[email protected]>
6
 * @license     MIT
7
 * @copyright   2015, Laraflock
8
 * @link        https://github.com/laraflock
9
 */
10
11
namespace Laraflock\Dashboard\Controllers;
12
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Response;
15
use Illuminate\Support\Collection;
16
use Laracasts\Flash\Flash;
17
use Laraflock\Dashboard\Exceptions\FormValidationException;
18
use Laraflock\Dashboard\Exceptions\UsersException;
19
20
class UsersController extends BaseDashboardController
21
{
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return Response
26
     */
27 1
    public function index()
28
    {
29 1
        $users = $this->userRepositoryInterface->getAllWith('roles');
30
31 1
        return $this->view('users.index')
32 1
                    ->with(['users' => $users]);
33
    }
34
35
    /**
36
     * Create a new resource.
37
     *
38
     * @return Response
39
     */
40 1
    public function create()
41
    {
42 1
        $roles = $this->roleRepositoryInterface->getAll()
43 1
                                               ->lists('name', 'slug');
44
45 1
        return $this->view('users.create')
46 1
                    ->with('roles', $roles);
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *
52
     * @param Request $request
53
     *
54
     * @return $this|\Illuminate\Http\RedirectResponse
55
     */
56 2 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...
57
    {
58
        try {
59 2
            $this->userRepositoryInterface->create($request->all());
60 2
        } catch (FormValidationException $e) {
61 1
            Flash::error($e->getMessage());
62
63 1
            return redirect()
64 1
              ->route('users.create')
65 1
              ->withErrors($e->getErrors())
66 1
              ->withInput();
67
        }
68
69 1
        Flash::success(trans('dashboard::dashboard.flash.user.create.success'));
70
71 1
        return redirect()->route('users.index');
72
    }
73
74
    /**
75
     * Show the form for editing the specified resource.
76
     *
77
     * @param int $id
78
     *
79
     * @return Response
80
     */
81 2
    public function edit($id)
82
    {
83 2
        if (!$user = $this->userRepositoryInterface->getByIdWith($id, 'roles')) {
84 1
            Flash::error(trans('dashboard::dashboard.errors.user.found'));
85
86 1
            return redirect()->route('users.index');
87
        }
88
89 1
        $currentRoles = $user->getRoles()
0 ignored issues
show
Bug introduced by
The method getRoles() does not seem to exist on object<Laraflock\Dashboa...serRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90 1
                             ->lists('name');
91
92 1
        if (empty($currentRoles)) {
93
            $currentRoles = new Collection(['name' => 'Not Available']);
94
        }
95
96 1
        $currentRoles->sortBy('name');
97 1
        $currentRoles = implode(', ', $currentRoles->toArray());
98
99 1
        $roles = $this->roleRepositoryInterface->getAll()
100 1
                                               ->lists('name', 'slug');
101
102 1
        return $this->view('users.edit')
103 1
                    ->with(['user' => $user, 'currentRoles' => $currentRoles, 'roles' => $roles]);
104
    }
105
106
    /**
107
     * Update the specified resource in storage.
108
     *
109
     * @param \Illuminate\Http\Request $request
110
     * @param int                      $id
111
     *
112
     * @return $this|\Illuminate\Http\RedirectResponse
113
     */
114 3 View Code Duplication
    public function update(Request $request, $id)
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...
115
    {
116
        try {
117 3
            $this->userRepositoryInterface->update($request->all(), $id);
118 3
        } catch (FormValidationException $e) {
119 1
            Flash::error($e->getMessage());
120
121 1
            return redirect()
122 1
              ->route('users.edit', ['id' => $id])
123 1
              ->withErrors($e->getErrors())
124 1
              ->withInput();
125 1
        } catch (UsersException $e) {
126 1
            Flash::error($e->getMessage());
127
128 1
            return redirect()->route('users.index');
129
        }
130
131 2
        Flash::success(trans('dashboard::dashboard.flash.user.edit.success'));
132
133 1
        return redirect()->route('users.edit', ['id' => $id]);
134
    }
135
136
    /**
137
     * Remove the specified resource from storage.
138
     *
139
     * @param int $id
140
     *
141
     * @return \Illuminate\Http\RedirectResponse
142
     */
143 2
    public function delete($id)
144
    {
145
        try {
146 2
            $this->userRepositoryInterface->delete($id);
147 2
        } catch (UsersException $e) {
148 1
            Flash::error($e->getMessage());
149
150 1
            return redirect()->route('users.index');
151
        }
152
153 1
        Flash::success(trans('dashboard::dashboard.flash.user.delete.success'));
154
155 1
        return redirect()->route('users.index');
156
    }
157
}