UserController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 8.42 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 9
c 7
b 2
f 1
lcom 1
cbo 7
dl 8
loc 95
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A index() 0 5 1
A new_user() 0 5 1
A create() 0 21 3
A update() 0 10 1
A delete() 8 8 1
A edit() 0 7 1

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 WITR\Http\Controllers\Admin;
4
5
use WITR\Http\Requests\Admin\User as Requests;
6
use WITR\Http\Controllers\Controller;
7
use WITR\User;
8
use WITR\Role;
9
use Input;
10
use File;
11
use Hash;
12
13
use Illuminate\Http\Request;
14
use Illuminate\Contracts\Auth\PasswordBroker;
15
16
class UserController extends Controller {
17
18
	public function __construct(PasswordBroker $passwords)
19
	{
20
		$this->middleware('auth');
21
		$this->middleware('admin');
22
23
		$this->passwords = $passwords;
0 ignored issues
show
Bug introduced by
The property passwords does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
	}
25
26
	/**
27
	 * Display a listing of the Users.
28
	 *
29
	 * @return Response
30
	 */
31
	public function index()
32
	{
33
		$users = User::orderBy('name', 'asc')->get();
34
		return view('admin.users.index', ['users' => $users]);
35
	}
36
37
	public function new_user()
38
	{
39
		$roles = Role::lists('name', 'id');
40
		return view('admin.users.create', ['roles' => $roles]); 
41
	}
42
43
	/**
44
	 * Show the form for creating a new resource.
45
	 *
46
	 * @return Response
47
	 */
48
	public function create(Requests\CreateRequest $request)
49
	{
50
		$user = new User($request->all());
51
		$user->username = $request['email'];
0 ignored issues
show
Documentation introduced by
The property username does not exist on object<WITR\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...
52
		$user->password = '';
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<WITR\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...
53
		$user->save();
54
55
        $response = $this->passwords->sendResetLink($request->only('email'), function($m)
56
		{
57
			$m->subject('Welcome to WITR!');
58
		});
59
60
		switch ($response)
61
		{
62
			case PasswordBroker::RESET_LINK_SENT:
63
				return redirect()->route('admin.users.index')
64
					->with('success', 'User Created!');
65
			case PasswordBroker::INVALID_USER:
66
				return redirect()->back()->with('error', trans($response));
67
		}
68
	}
69
70
	/**
71
	 * Show the form for editing the specified resource.
72
	 *
73
	 * @param  int  $id
74
	 * @return Response
75
	 */
76
	public function edit($id)
77
	{
78
		$roles = Role::lists('name', 'id');
79
		$user = User::findOrFail($id);
80
81
		return view('admin.users.edit', ['roles' => $roles, 'user' => $user]);
82
	}
83
84
	public function update(Requests\UpdateRequest $request, $id)
85
	{
86
		$user = User::findOrFail($id);
87
		$user->fill($request->all());
88
		$user->username = $request['email'];
89
		$user->save();
90
91
		return redirect()->route('admin.users.index')
92
			->with('success', 'User Saved!');
93
	}
94
95
	/**
96
	 * Remove the specified resource from storage.
97
	 *
98
	 * @param  int  $id
99
	 * @return Response
100
	 */
101 View Code Duplication
	public function delete($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...
102
	{
103
		$user = User::findOrFail($id);
104
		File::delete(public_path().'/img/djs/'.$user->picture);
105
		User::destroy($id);
106
		return redirect()->route('admin.users.index')
107
			->with('success', 'User Deleted!');
108
	}
109
110
}
111