Users   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 135
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A listUser() 0 17 3
A addUser() 0 4 1
A createUser() 0 19 3
A editUser() 0 5 1
A updateUser() 0 17 2
A deleteUser() 0 5 1
1
<?php namespace Usman\Guardian\Http\Controllers;
2
3
use View;
4
use Input;
5
use Redirect;
6
use Request;
7
use Usman\Guardian\Validators\UserValidator;
8
use Usman\Guardian\Validators\Exceptions\ValidationException;
9
use Usman\Guardian\Repositories\Interfaces\UserRepositoryInterface;
10
11
class Users extends Base {
12
13
    /**
14
     * The repository instance
15
     * 
16
     * @var Usman\Guardian\Repositories\UserRepository
17
     */
18
    protected $user;
19
20
    /**
21
     * The validator instance
22
     * 
23
     * @var Usman\Guardian\Validators\UserValidator
24
     */
25
    protected $validator;
26
27
    /**
28
     * Creates a new User controller
29
     * 
30
     * @param UserRepositoryInterface $user      
31
     * @param UserValidator           $validator 
32
     */
33
    public function __construct(UserRepositoryInterface $user, UserValidator $validator)
34
    {
35
        $this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type object<Usman\Guardian\Re...serRepositoryInterface> is incompatible with the declared type object<Usman\Guardian\Ht...itories\UserRepository> of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Usman\Guardian\Validators\UserValidator> is incompatible with the declared type object<Usman\Guardian\Ht...lidators\UserValidator> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    /**
40
     * Lists ths users.
41
     * 
42
     * @return Response
43
     */
44
    public function listUser()
45
    { 
46
        //we will check if a form is submitted with either 
47
        //a role name or a username as a search term.
48
        if(Input::has('role') or Input::has('username'))
49
        {  
50
            extract(Input::only(['role','username']));
0 ignored issues
show
Bug introduced by
\Input::only(array('role', 'username')) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
51
            $users = $this->user->searchUserByRole($username,$role);
52
            $users->appends(compact('role','username'));
53
            return View::make('guardian::partials.user.list')->with('users',$users);
54
        }//otherwise we will display the default users list
55
        else
56
        {
57
            $users = $this->user->getByPageWith('roles');
58
            return View::make('guardian::partials.user.list',compact('users'));
59
        }
60
    }
61
62
    /**
63
     * Shows the add user form
64
     *
65
     * @return  Response
66
     */
67
    public function addUser()
68
    {   
69
        return View::make('guardian::partials.user.add');
70
    }
71
72
    /**
73
     * Saves the new user in the database.
74
     * 
75
     * @return Response
76
     */
77
    public function createUser()
78
    {
79
        try 
80
        {
81
            $this->validator->setFields(Input::all())->validate('create');
82
            $id = $this->user->create(Input::all());
83
            if(Input::has('roles'))
84
            {
85
                $this->user->attach($id, Input::get('roles'),'roles');
86
            }
87
            return Redirect::back()->withSuccess('User Created Successfully');
88
        }
89
        catch (ValidationException $e)
90
        {
91
            $errors = $e->getValidationErrors();
92
            return Redirect::back()->withErrors($errors)->withInput();
93
        }
94
        
95
    }
96
97
    /**
98
     * Shows the user edit form.
99
     * 
100
     * @param  int $id
101
     * @return Response
102
     */
103
    public function editUser($id)
104
    {
105
        $user = $this->user->findByIdWith($id,'roles');
106
        return View::make('guardian::partials.user.edit')->with('user',$user);
107
    }
108
    
109
    /**
110
     * Updates the database record for the user.
111
     * 
112
     * @param  int $id
113
     * @return Response
114
     */
115
    public function updateUser($id)
116
    {
117
        try
118
        {
119
            $this->validator->addRule('update','username','required|alpha_num|unique:users,username,'.$id);
120
            $this->validator->setFields(Input::all())->validate('update');
121
            $this->user->update($id, Input::all());
122
            $this->user->attach($id, Input::get('roles',[]),'roles');
123
            return Redirect::route('user.list',Input::get('ref'))->withSuccess('User Updated Successfully');
124
        }
125
        catch(ValidationException $e)
126
        {
127
            $errors = $e->getValidationErrors();
128
            return Redirect::back()->withErrors($errors)->withInput();
129
        }
130
131
    }
132
133
    /**
134
     * Deletes a user from the database.
135
     * 
136
     * @param  int $id
137
     * @return Response
138
     */
139
    public function deleteUser($id)
140
    {
141
        $this->user->deleteWith($id,['roles']);
142
        return Redirect::back()->withSuccess('Deleted Successfully!');
143
    }
144
145
}