AdminController   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 206
Duplicated Lines 24.76 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 6
dl 51
loc 206
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newUser() 0 7 1
B updateUser() 0 47 8

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 App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\User;
7
use App\Group;
8
use Auth;
9
10
class AdminController extends Controller
11
{
12
    use SearchTrait;
13
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
    }
23
24 View Code Duplication
    public function showUsers(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...
25
    {
26
        $users = User::orderBy('firstname', 'asc')->orderBy('lastname', 'asc');
27
28
        $results = $this->search($users, $request->input('type'), $request->input('search'));
29
30
        if (false === is_null($results)) {
31
            $request->session()->flash(
32
                'results',
33
                trans_choice(
34
                    'users.message.search',
35
                    $results,
36
                    ['number' => $results]
37
                )
38
            );
39
            $request->session()->flash('search', $request->input('search'));
40
            $request->session()->flash('type', $request->input('type'));
41
        }
42
43
        return view('user/users', ['users' => $users->paginate(20)]);
44
    }
45
46
    /**
47
     * Edit the given user
48
     *
49
     * @param  int  $id
50
     * @return Response
51
     */
52
    public function editUser($id)
53
    {
54
        $user = User::findOrFail($id);
55
        $groups = Group::orderBy('name', 'asc')->get();
56
        return view('user/user',
57
            ['user' => $user, 'groups' => $groups]
58
        );
59
    }
60
61
    /**
62
     * Open a new user form.
63
     *
64
     * @return Response
65
     */
66
    public function newUser()
67
    {
68
        $groups = Group::orderBy('name', 'asc')->get();
69
        return view('user/user',
70
            ['user' => new User, 'groups' => $groups]
71
        );
72
    }
73
74
    /**
75
     * Add a new user.
76
     *
77
     * @return Response
78
     */
79
    public function addUser(Request $request)
80
    {
81
        $this->validate($request, [
82
            'firstname' => 'required|alpha_num|max:100',
83
            'lastname' => 'required|alpha_num|max:100',
84
            'email' => 'required|email|unique:users,email',
85
            'password' => 'required|alpha_dash|min:8|confirmed',
86
            'password_confirmation' => 'required|min:8',
87
            'level' => 'required',
88
            'status' => 'required|boolean',
89
            'group' => 'required_if:level,1'
90
        ]);
91
92
        $user = new User;
93
        $user->email = strtolower($request->email);
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
        $user->password = bcrypt($request->password);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
        $user->firstname = ucwords($request->firstname);
0 ignored issues
show
Documentation introduced by
The property firstname does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
        $user->lastname = ucwords($request->lastname);
0 ignored issues
show
Documentation introduced by
The property lastname does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
97
        $user->level = $request->level;
0 ignored issues
show
Documentation introduced by
The property level does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
        $user->status = $request->status;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
        if (false === empty($request->group)) {
100
            $user->group_id = $request->group;
0 ignored issues
show
Documentation introduced by
The property group_id does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
        }
102
        $user->created_by = $request->user()->id;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
        $user->save();
104
        return redirect('administrators')->with(
105
            'status',
106
            trans('users.message.add', ['user' => $user->email])
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
        );
108
    }
109
110
    /**
111
     * Update an user.
112
     *
113
     * @param  Request  $request
114
     * @param  int  $id
115
     * @return Response
116
     */
117
    public function updateUser(Request $request, $id = 0)
118
    {
119
        $this->validate($request, [
120
            'password' => 'alpha_dash|min:8|confirmed',
121
            'password_confirmation' => 'required_with:password|min:8',
122
            'status' => 'boolean',
123
            'group' => 'exists:groups,id'
124
        ]);
125
126
        if ($request->is('profile')) {
127
            $id = Auth::user()->id;
128
        }
129
130
        $user = User::findOrFail($id);
131
132
        if (false === empty($request->password)) {
133
            $user->password = bcrypt($request->password);
134
        }
135
        if (false === empty($request->level)) {
136
            $user->level = $request->level;
137
        }
138
        if (false === empty($request->group)) {
139
            $user->group_id = $request->group;
140
        }
141
142
        // if not admin local then no group
143
        if ($user->level !== USER::USER_LEVEL_LOCAL) {
144
            $user->group_id = 0;
145
        }
146
147
        if ($request->has('status')) {
148
            $user->status = $request->status;
149
        }
150
151
        $user->update();
152
153
        if ($request->is('profile')) {
154
            $redirect = 'profile';
155
        } else {
156
            $redirect = 'administrators';
157
        }
158
159
        return redirect($redirect)->with(
160
            'status',
161
            trans('users.message.update', ['user' => $user->email])
162
        );
163
    }
164
165
    /**
166
     * Enable an user.
167
     *
168
     * @param  int  $id
169
     * @return Response
170
     */
171 View Code Duplication
    public function enableUser($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...
172
    {
173
        $user = User::findOrFail($id);
174
        $user->status = User::USER_ENABLED;
175
        $user->update();
176
        return redirect()->back()->with(
177
            'status',
178
            trans('users.message.enable', ['user' => $user->email])
179
        );
180
    }
181
182
    /**
183
     * Disable an user.
184
     *
185
     * @param  int  $id
186
     * @return Response
187
     */
188 View Code Duplication
    public function disableUser($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...
189
    {
190
        $user = User::findOrFail($id);
191
        $user->status = User::USER_DISABLED;
192
        $user->update();
193
        return redirect()->back()->with(
194
            'status',
195
            trans('users.message.disable', ['user' => $user->email])
196
        );
197
    }
198
199
    /**
200
     * Remove an user.
201
     *
202
     * @param  int  $id
203
     * @return Response
204
     */
205 View Code Duplication
    public function removeUser($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...
206
    {
207
        $user = User::findOrFail($id);
208
        $email = $user->email;
209
        $user->delete();
210
        return redirect()->back()->with(
211
            'status',
212
            trans('users.message.delete', ['user' => $email])
213
        );
214
    }
215
}
216