Completed
Push — master ( 7c8b8b...4a15ab )
by Tim
02:41
created

StaffController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Http\Request;
7
8
use App\Http\Requests;
9
use Bouncer;
10
11
/**
12
 * Class StaffController
13
 * @package App\Http\Controllers
14
 */
15
class StaffController extends Controller
16
{
17
18
    /**
19
     * StaffController constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->middleware('auth');
24
        $this->middleware('lang');
25
    }
26
27
    /**
28
     * Create new staff member.
29
     *
30
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
31
     */
32
    public function create()
33
    {
34
        return view('staff.create');
35
    }
36
37
    /**
38
     * Edit view for a staff member.
39
     *
40
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
41
     */
42
    public function edit($id)
43
    {
44
        $data['query'] = User::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...
45
        return view('staff.edit', $data);
46
    }
47
48
    /**
49
     * Display all the staff.
50
     *
51
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
52
     */
53
    public function index()
54
	{
55
		$data['users'] = User::paginate(15);
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
    	return view('users/index', $data);
57
    }
58
59
    /**
60
     * Set the user available.
61
     *
62
     * @return \Illuminate\Http\RedirectResponse
63
     */
64 View Code Duplication
    public function setAvailable()
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...
65
    {
66
        $user = User::find(auth()->user()->id);
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        Bouncer::retract('unavailable')->from($user);
68
        Bouncer::assign('available')->to($user);
69
70
        return redirect()->back(302);
71
    }
72
73
    /**
74
     * Set the user unavailable.
75
     *
76
     * @return \Illuminate\Http\RedirectResponse
77
     */
78 View Code Duplication
    public function setUnavailable()
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...
79
    {
80
        $user = User::find(auth()->user()->id);
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        Bouncer::retract('available')->from($user);
82
        Bouncer::assign('unavailable')->to($user);
83
84
        return redirect()->back(302);
85
    }
86
87
    /**
88
     * Display the profile.
89
     *
90
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
91
     */
92
    public function profile()
93
    {
94
    	return view('users/profile');
95
    }
96
97
    /**
98
     * Destroy or multiple staff members.
99
     *
100
     * @param  int $id THe id off the staff member in the database.
101
     * @return \Illuminate\Http\RedirectResponse
102
     */
103
    public function destroy($id)
104
    {
105
        $user = User::find($id);
106
        $user->roles()->sync([]);
107
108
        User::destroy($id);
109
        session()->flash('message', 'User deleted');
110
111
        return redirect()->to('/staff');
112
    }
113
}
114