Completed
Pull Request — master (#107)
by Glenn
15:22 queued 07:05
created

StaffController::destroy_permission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 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\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
/**
22
 * Class StaffController
23
 * @package app\Http\Controllers
24
 */
25
class StaffController extends Controller
26
{
27
    /**
28
     * StaffController constructor.
29
     */ 
30
    public function __construct()
31
    {
32
        $this->middleware('auth');
33
        $this->middleware('lang');
34
    }
35
36
    /**
37
     * Display all users.
38
     *
39
     * @return mixed
40
     */
41
    public function index()
42
    {
43
        $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...
44
45
        return view('staff/users', $data);
46
    }
47
48
    /**
49
     * Show the form for creating a new employee.
50
     *
51
     * @return mixed
52
     */
53
    public function create()
54
    {
55
        $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...
56
57
        return view('staff/create_user', $data);
58
    }
59
60
    /**
61
     * Store a newly created employee in storage.
62
     *
63
     * @param Requests\StaffValidator|Request $request
64
     *
65
     * @return mixed
66
     */
67
    public function store(Requests\StaffValidator $request)
68
    {
69
        $user = new User();
70
        $user->fname = $request->get('fname');
71
        $user->name = $request->get('name');
72
        $user->address = $request->get('address');
73
        $user->postal_code = $request->get('postal_code');
74
        $user->city = $request->get('city');
75
        $user->email = $request->get('email');
76
        $user->password = bcrypt($request->get('password'));
77
        $user->save();
78
79
        $mailbox = env('MAIL_USERNAME');
80
        $mail_password = $request->get('password');
81
        session()->flash('message', trans('FlashSession.newEmployee'));
82
83
        $injectionData = ['user' => $user, 'password' => $mail_password];
84
85
        Mail::send('emails.new_user', $injectionData, function ($m) use ($user, $mailbox) {
86
            $m->from($mailbox);
87
            $m->to($user->email)->subject('Your user credentials!');
88
        });
89
90
        return redirect('staff');
91
    }
92
93
    /**
94
     * Update a user.
95
     *
96
     * @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...
97
     * @param Request $request
98
     *
99
     * @return mixed
100
     */
101
    public function updateUser($id, Request $request)
102
    {
103
        $user = User::find($id);
104
        $user->fname = $request->get('fname');
105
        $user->name = $request->get('name');
106
        $user->address = $request->get('address');
107
        $user->postal_code = $request->get('postal_code');
108
        $user->city = $request->get('city');
109
        $user->email = $request->get('email');
110
        $user->assignRole($request->get('user_type'));
111
        $user->update();
112
113
        $user = User::find($id);
114
        Bouncer::assign($request->get('user_typ'))->to($user);
115
116
        session()->flash('message', trans('FlashSession.staffUpdate'));
117
118
        return \Redirect::back();
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function policies()
125
    {
126
        $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...
127
128
        return view('staff/roles', $data);
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134
    public function addpolicies()
135
    {
136
        $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...
137
138
        return view('staff/create_role', $data);
139
    }
140
141
    /**
142
     * @param Request $request
143
     *
144
     * @return string
145
     */
146
    public function addRole(Request $request)
147
    {
148
        $role = Role::create(['name' => $request->get('role_name')]);
149
150
        if ($role) {
151
            session()->flash('message', trans('FlashSession.insertRoleSuccess'));
152
            return redirect('staff/policies');
153
        } else {
154
            session()->flash('message', trans('FlashSession.insertRoleFailure'));
155
            return redirect()->back(302);
156
        }
157
    }
158
159
    /**
160
     * @param $id
161
     *
162
     * @return mixed
163
     */
164
    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...
165
    {
166
        $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...
167
168
        return view('staff/edit_role', $data);
169
    }
170
171
    /**
172
     * Remove the user role.
173
     *
174
     * @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...
175
     *
176
     * @return redirect
177
     */
178
    public function destroyRole($id)
179
    {
180
        $role = Role::find($id);
181
        $role->delete();
182
        session()->flash('message', trans('FlashSession.roleDestroy'));
183
184
        return redirect('staff/policies');
185
    }
186
187
    /**
188
     * Show all permission.
189
     */
190
    public function permissions()
191
    {
192
        $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...
193
194
        return view('staff/permissions', $data);
195
    }
196
197
    /**
198
     * Show the form to create a new permission.
199
     */
200
    public function createPermission()
201
    {
202
        return view('staff/create_permission');
203
    }
204
205
    /**
206
     * Show the form to edit a permission.
207
     */
208
    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...
209
    {
210
        $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...
211
        return view('staff/edit_permission', $data);
212
    }
213
214
    /**
215
     * Save the new permission.
216
     *
217
     * @param Request $request
218
     *
219
     * @return redirect
220
     */
221
    public function savePermission(Request $request)
222
    {
223
        Permission::create(['name' => $request->get('permission_name')]);
224
        session()->flash('message', trans('FlashSession.insertPermission'));
225
226
        return redirect('staff/permissions');
227
    }
228
229
    /**
230
     * Delete a permission out off timecontrol.
231
     *
232
     * @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...
233
     * @return Redirect
234
     */
235
    public function destroyPermission($id)
236
    {
237
        $permission = Permission::find($id);
238
        $permission->delete();
239
240
        session()->flash('message', trans('FlashSession.permissionDestroy'));
241
242
        return redirect('staff/permissions');
243
    }
244
245
    /**
246
     * Display the specified resource.
247
     *
248
     * @param int $id
249
     *
250
     * @return \Illuminate\Http\Response
251
     */
252
    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...
253
    {
254
        //
255
    }
256
257
    /**
258
     * Show the form for editing the specified resource.
259
     *
260
     * @param $id
261
     *
262
     * @return
263
     */
264
    public function edit($id)
265
    {
266
        $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...
267
        $data['departments'] = Departments::all();
268
        $data['teams'] = Teams::all();
269
        $data['countries'] = Countries::all();
270
        $data['roles'] = Role::all();
271
272
        return view('staff/edit_user', $data);
273
    }
274
275
    /**
276
     * @return mixed
277
     */
278
    public function profile()
279
    {
280
        $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...
281
282
        return view('staff/profile', $data);
283
    }
284
285
    /**
286
     * change a password.
287
     */
288
    public function chPass()
289
    {
290
291
    }
292
293
    /**
294
     * Update the specified resource in storage.
295
     *
296
     * @param Requests\accountManagementValidator|Request $request
297
     * @return \Illuminate\Http\Response
298
     */
299
    public function update(Requests\accountManagementValidator $request)
300
    {
301
        $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...
302
        $user->fname = $request->get('email');
303
        $user->name = $request->get('name');
304
        $user->email = $request->get('email');
305
        $user->address = $request->get('address');
306
        $user->email = $request->get('email');
307
308
        if (Input::file()) {
309
            $image = Input::file('avatar');
310
            $filename = time().'.'.$image->getClientOriginalExtension();
311
            $path = public_path('profilepics/'.$filename);
312
313
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
314
            $user->image = $filename;
315
        }
316
317
        $user->save();
318
319
        return redirect()->back();
320
    }
321
322
    /**
323
     * Remove the specified employee from the database.
324
     *
325
     * @param int $id
326
     *
327
     * @return \Illuminate\Http\Response
328
     */
329
    public function destroy($id)
330
    {
331
        if (!Auth::user()->is('Administrator')) {
332
            return Redirect::back();
333
        }
334
335
        User::Destroy($id);
336
        session()->flash('message', trans('FlashSession.staffDestroy'));
337
338
        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...
339
    }
340
}
341