Test Setup Failed
Pull Request — master (#107)
by Tim
61:13 queued 52:59
created

StaffController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\Departments;
10
use App\Teams;
11
use App\Role;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Input;
14
use Illuminate\Support\Facades\Mail;
15
use Illuminate\Support\Facades\Redirect;
16
use Intervention\Image\Facades\Image;
17
use App\Permission;
18
use Bouncer;
19
use App\Http\Controllers\Controller;
20
21
class StaffController extends Controller
22
{
23
    /**
24
     * StaffController constructor.
25
     */ 
26
    public function __construct()
27
    {
28
        $this->middleware('auth');
29
        $this->middleware('lang');
30
    }
31
32
    /**
33
     * Display all users.
34
     *
35
     * @return mixed
36
     */
37
    public function index()
38
    {
39
        $data['users'] = User::orderBy('fname', 'asc')->paginate(10);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
40
41
        return view('staff/users', $data);
42
    }
43
44
    /**
45
     * Show the form for creating a new employee.
46
     *
47
     * @return mixed
48
     */
49
    public function create()
50
    {
51
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
52
53
        return view('staff/create_user', $data);
54
    }
55
56
    /**
57
     * Store a newly created employee in storage.
58
     *
59
     * @param Requests\StaffValidator|Request $request
60
     *
61
     * @return mixed
62
     */
63
    public function store(Requests\StaffValidator $request)
64
    {
65
        $user = new User();
66
        $user->fname = $request->get('fname');
67
        $user->name = $request->get('name');
68
        $user->address = $request->get('address');
69
        $user->postal_code = $request->get('postal_code');
70
        $user->city = $request->get('city');
71
        $user->email = $request->get('email');
72
        $user->password = bcrypt($request->get('password'));
73
        $user->save();
74
75
        $mailbox = env('MAIL_USERNAME');
76
        $mail_password = $request->get('password');
77
        session()->flash('message', 'New employee has been added to the application');
78
79
        $injectionData = ['user' => $user, 'password' => $mail_password];
80
81
        Mail::send('emails.new_user', $injectionData, function ($m) use ($user, $mailbox) {
82
            $m->from($mailbox);
83
            $m->to($user->email)->subject('Your user credentials!');
84
        });
85
86
        return redirect('staff');
87
    }
88
89
    /**
90
     * Update a user.
91
     *
92
     * @param Int,    $id
0 ignored issues
show
Documentation introduced by
The doc-type Int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
93
     * @param Request $request
94
     *
95
     * @return mixed
96
     */
97
    public function updateUser($id, Request $request)
98
    {
99
        $user = User::find($id);
100
        $user->fname = $request->get('fname');
101
        $user->name = $request->get('name');
102
        $user->address = $request->get('address');
103
        $user->postal_code = $request->get('postal_code');
104
        $user->city = $request->get('city');
105
        $user->email = $request->get('email');
106
        $user->assignRole($request->get('user_type'));
107
        $user->update();
108
109
        $user = User::find($id);
110
        Bouncer::assign($request->get('user_typ'))->to($user);
111
112
        session()->flash('message', 'User details have been updated');
113
114
        return \Redirect::back();
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function policies()
121
    {
122
        $data['roles'] = Role::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
124
        return view('staff/roles', $data);
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function addpolicies()
131
    {
132
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
133
134
        return view('staff/create_role', $data);
135
    }
136
137
    /**
138
     * @param Request $request
139
     *
140
     * @return string
141
     */
142
    public function addRole(Request $request)
143
    {
144
        $role = Role::create(['name' => $request->get('role_name')]);
145
146 View Code Duplication
        if ($role) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
147
            session()->flash('message', 'User role has been created');
148
            return redirect('staff/policies');
149
        } else {
150
            session()->flash('message', 'Could not create the role');
151
            return redirect()->back(302);
152
        }
153
    }
154
155
    /**
156
     * @param $id
157
     *
158
     * @return mixed
159
     */
160
    public function editpolicies($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...
161
    {
162
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
163
164
        return view('staff/edit_role', $data);
165
    }
166
167
    /**
168
     * Remove the user role.
169
     *
170
     * @param int, $id
0 ignored issues
show
Documentation introduced by
The doc-type int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
171
     *
172
     * @return redirect
173
     */
174
    public function destroyRole($id)
175
    {
176
        $role = Role::find($id);
177
        $role->delete();
178
        session()->flash('message', 'User role has been removed from the database');
179
180
        return redirect('staff/policies');
181
    }
182
183
    /**
184
     * Show all permission.
185
     */
186
    public function permissions()
187
    {
188
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
189
190
        return view('staff/permissions', $data);
191
    }
192
193
    /**
194
     * Show the form to create a new permission.
195
     */
196
    public function createPermission()
197
    {
198
        return view('staff/create_permission');
199
    }
200
201
    /**
202
     * Show the form to edit a permission.
203
     */
204
    public function EditPermission(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
205
    {
206
        $data['permission'] = Permission::find($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
207
        return view('staff/edit_permission', $data);
208
    }
209
210
    /**
211
     * Save the new permission.
212
     *
213
     * @param Request $request
214
     *
215
     * @return redirect
216
     */
217
    public function savePermission(Request $request)
218
    {
219
        Permission::create(['name' => $request->get('permission_name')]);
220
        session()->flash('message', 'The new permission has been added to the database');
221
222
        return redirect('staff/permissions');
223
    }
224
225
    /**
226
     * Delete a permission out off timecontrol.
227
     *
228
     * @param int, $id, the id off the permission.
0 ignored issues
show
Documentation introduced by
The doc-type int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $id,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
229
     * @return Redirect
230
     */
231
    public function destroyPermission($id)
232
    {
233
        $permission = Permission::find($id);
234
        $permission->delete();
235
236
        session()->flash('message', 'Permission has been removed from the database');
237
238
        return redirect('staff/permissions');
239
    }
240
241
    /**
242
     * Display the specified resource.
243
     *
244
     * @param int $id
245
     *
246
     * @return \Illuminate\Http\Response
247
     */
248
    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...
249
    {
250
        //
251
    }
252
253
    /**
254
     * Show the form for editing the specified resource.
255
     *
256
     * @param $id
257
     *
258
     * @return
259
     */
260
    public function edit($id)
261
    {
262
        $data['user']  = User::findOrFail($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
263
        $data['departments'] = Departments::all();
264
        $data['teams'] = Teams::all();
265
        $data['countries'] = Countries::all();
266
        $data['roles'] = Role::all();
267
268
        return view('staff/edit_user', $data);
269
    }
270
271
    /**
272
     * @return mixed
273
     */
274
    public function profile()
275
    {
276
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
277
278
        return view('staff/profile', $data);
279
    }
280
281
    /**
282
     * change a password.
283
     */
284
    public function chPass()
285
    {
286
287
    }
288
289
    /**
290
     * Update the specified resource in storage.
291
     *
292
     * @param Requests\accountManagementValidator|Request $request
293
     * @return \Illuminate\Http\Response
294
     */
295
    public function update(Requests\accountManagementValidator $request)
296
    {
297
        $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...
298
        $user->fname = $request->get('email');
299
        $user->name = $request->get('name');
300
        $user->email = $request->get('email');
301
        $user->address = $request->get('address');
302
        $user->email = $request->get('email');
303
304
        if (Input::file()) {
305
            $image = Input::file('avatar');
306
            $filename = time().'.'.$image->getClientOriginalExtension();
307
            $path = public_path('profilepics/'.$filename);
308
309
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
310
            $user->image = $filename;
311
        }
312
313
        $user->save();
314
315
        return redirect()->back();
316
    }
317
318
    /**
319
     * Remove the specified employee from the database.
320
     *
321
     * @param int $id
322
     *
323
     * @return \Illuminate\Http\Response
324
     */
325
    public function destroy($id)
326
    {
327
        if (!Auth::user()->is('Administrator')) {
328
            return Redirect::back();
329
        }
330
331
        User::Destroy($id);
332
        session()->flash('message', 'User has been removed from the database');
333
334
        return redirect('staff');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('staff'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by app\Http\Controllers\StaffController::destroy of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
335
    }
336
}
337