UsersController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
crap 1
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
}