Completed
Pull Request — master (#107)
by Glenn
10:27 queued 05:05
created

StaffController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 24
c 8
b 3
f 1
lcom 0
cbo 10
dl 0
loc 242
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 5 1
A create() 0 5 1
A store() 0 21 1
A show() 0 4 1
A chPass() 0 4 1
A update() 0 22 2
A destroy() 0 10 2
A addpolicies() 0 5 1
A permissions() 0 5 1
A edit() 0 8 1
A updateUser() 0 15 1
A editpolicies() 0 7 1
A destroyRole() 0 7 1
A create_permission() 0 4 1
A profile() 0 5 1
A policies() 0 5 1
A addRole() 0 14 3
A save_permission() 0 7 1
A destroy_permission() 0 7 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
use App\User;
8
use App\Countries;
9
use App\Teams;
10
use App\Role;
11
use Illuminate\Support\Facades\Input;
12
use Intervention\Image\Facades\Image;
13
use App\Permission;
14
15
class StaffController extends Controller
16
{
17
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
23
24
    /**
25
     * Display all users.
26
     */
27
    public function index()
28
    {
29
        $users = User::orderBy('fname', 'asc')->paginate(10);
30
        return view('staff/users', ['users' => $users]);
31
    }
32
33
    /**
34
     * Show the form for creating a new employee.
35
     *
36
     */
37
    public function create()
38
    {
39
        $countries = Countries::all();
40
        return view('staff/create_user', ['countries' => $countries]);
41
    }
42
43
    /**
44
     * Store a newly created employee in storage.
45
     *
46
     */
47
    public function store(Request $request)
48
    {
49
        $user = new User;
50
        $user->fname = $request->get('fname');
0 ignored issues
show
Documentation introduced by
The property fname 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...
51
        $user->name = $request->get('name');
0 ignored issues
show
Documentation introduced by
The property name 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...
52
        $user->address = $request->get('address');
0 ignored issues
show
Documentation introduced by
The property address 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...
53
        $user->postal_code = $request->get('postal_code');
0 ignored issues
show
Documentation introduced by
The property postal_code 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...
54
        $user->city = $request->get('city');
0 ignored issues
show
Documentation introduced by
The property city 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...
55
        $user->email = $request->get('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...
56
        $user->password = bcrypt($request->get('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...
57
        $user->save();
58
59
        $mailbox = env('MAIL_USERNAME');
60
        $mail_password = $request->get('password');
61
        \Session::flash('message', "New employee has been added to the application");
62
        \Mail::send('emails.new_user', ['user' => $user, 'password' => $mail_password], function ($m) use ($user, $mailbox) {
63
                  $m->from($mailbox);
64
                  $m->to($user->email)->subject('Your user credentials!');
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...
65
      });
66
        return redirect('staff');
67
    }
68
69
    public function updateUser($id, Request $request)
70
    {
71
        $user = User::find($id);
72
        $user->fname = $request->get('fname');
73
        $user->name = $request->get('name');
74
        $user->address = $request->get('address');
75
        $user->postal_code = $request->get('postal_code');
76
        $user->city = $request->get('city');
77
        $user->email = $request->get('email');
78
        $user->assignRole($request->get('user_type'));
79
        $user->update();
80
81
        \Session::flash('message', "User details have been updated");
82
        return \Redirect::back();
83
    }
84
85
86
    public function policies()
87
    {
88
        $roles = Role::all();
89
        return view('staff/roles', ['roles' => $roles]);
90
    }
91
92
    public function addpolicies()
93
    {
94
        $permissions = Permission::all();
95
        return view('staff/create_role', ['permissions' => $permissions]);
96
    }
97
98
99
    public function addRole(Request $request)
100
    {
101
        $role = Role::create(['name' => $request->get('role_name')]);
102
        foreach ($request->get('permissions') as $permission) {
103
            $role->givePermissionTo($permission);
0 ignored issues
show
Documentation Bug introduced by
The method givePermissionTo does not exist on object<App\Role>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
        }
105
        if($assign_role)
0 ignored issues
show
Bug introduced by
The variable $assign_role does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
106
        {
107
        \Session::flash('message', 'New user role has been created');
108
        return redirect('staff/policies');
109
      }else {
110
        return "Mislukt";
111
      }
112
    }
113
114
    public function editpolicies($id)
115
    {
116
        $role = Role::find($id);
0 ignored issues
show
Unused Code introduced by
$role is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
117
        $permissions = Permission::all();
118
119
        return view('staff/edit_role', ['permissions' => $permissions]);
120
    }
121
122
    /**
123
     *
124
     * Remove the user role.
125
     *
126
     */
127
    public function destroyRole($id)
128
    {
129
        $role = Role::find($id);
130
        $role->delete();
131
        \Session::flash('message', "User role has been removed from the database");
132
        return redirect('staff/policies');
133
    }
134
135
136
    /**
137
     *
138
     * Show all permission.
139
     *
140
     */
141
    public function permissions()
142
    {
143
        $permissions = Permission::all();
144
        return view('staff/permissions', ['permissions' => $permissions]);
145
    }
146
147
    /**
148
     *
149
     * Show the form to create a new permission.
150
     *
151
     */
152
    public function create_permission()
153
    {
154
        return view('staff/create_permission');
155
    }
156
157
    /**
158
     *
159
     * Save the new permission.
160
     *
161
     */
162
    public function save_permission(Request $request)
163
    {
164
        // $permission_name = $request->get('permission_name');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
     $permission = Permission::create(['name' => $request->get('permission_name')]);
0 ignored issues
show
Unused Code introduced by
$permission is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
        \Session::flash('message', "The new permission has been added to the database");
167
        return redirect('staff/permissions');
168
    }
169
170
    public function destroy_permission($id)
171
    {
172
        $permission = Permission::find($id);
173
        $permission->delete();
174
        \Session::flash('message', "Permission has been removed from the database");
175
        return redirect('staff/permissions');
176
    }
177
178
    /**
179
     * Display the specified resource.
180
     *
181
     * @param  int  $id
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
    {
186
        //
187
    }
188
189
    /**
190
     * Show the form for editing the specified resource.
191
     */
192
    public function edit($id)
193
    {
194
        $user = User::findOrFail($id);
195
        $teams = Teams::all();
196
        $countries = Countries::all();
197
        $roles = Role::all();
198
        return view("staff/edit_user", ['user' => $user, 'teams' => $teams, 'countries' => $countries, 'roles' => $roles]);
199
    }
200
201
202
    public function profile()
203
    {
204
        $countries = Countries::all();
205
        return view("staff/profile", ['countries' => $countries]);
206
    }
207
208
    public function chPass()
209
    {
210
211
    }
212
213
    /**
214
     * Update the specified resource in storage.
215
     *
216
     * @param  \Illuminate\Http\Request $request
217
     * @return \Illuminate\Http\Response
218
     */
219
    public function update(Request $request)
220
    {
221
        $user = User::findOrFail(auth()->user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
222
        $user->fname   = $request->get('email');
223
        $user->name    = $request->get('name');
224
        $user->email   = $request->get('email');
225
        $user->address = $request->get('address');
226
        $user->email   = '[email protected]';
227
228
        if(Input::file()) {
229
            $image = Input::file('avatar');
230
            $filename  = time() . '.' . $image->getClientOriginalExtension();
231
            $path = public_path('profilepics/' . $filename);
232
233
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
234
            $user->image = $filename;
235
        }
236
237
        $user->save();
238
239
        return redirect()->back();
240
    }
241
242
    /**
243
     * Remove the specified employee from the database.
244
     *
245
     */
246
    public function destroy($id)
247
    {
248
        if (! Auth::user()->is('Administrator')) {
249
            return Redorect::back();
250
        }
251
252
        User::Destroy($id);
253
        session()->flash('message', "User has been removed from the database");
254
        return redirect('staff');
255
    }
256
}
257