Completed
Push — master ( d06fb7...7b57f2 )
by Tim
02:32
created

StaffController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
     * @TODO   Build up the view
31
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
32
     */
33
    public function create()
34
    {
35
        return view('staff.create');
36
    }
37
38
    /**
39
     * Store the new member in the database
40
     *
41
     * @TODO:  Needs phpunit test.
42
     * @TODO:  Build up the controller logic.
43
     * @TODO:  Build up the request validator.
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46
    public function store()
47
    {
48
        return redirect()->back(302);
49
    }
50
51
    /**
52
     * Update the staff member.
53
     *
54
     * @TODO:  Needs phpunit test
55
     * @TODO:  Build up the controller.
56
     * @TODO:  BUild up the request validator.
57
     * @param  int $id The staff member id in the database.
58
     * @return \Illuminate\Http\RedirectResponse
59
     */
60
    public function update($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...
61
    {
62
        return redirect()->back(302);
63
    }
64
65
    /**
66
     * Edit view for a staff member.
67
     *
68
     * @TODO   Build up the view.
69
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
70
     */
71
    public function edit($id)
72
    {
73
        $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...
74
        return view('staff.edit', $data);
75
    }
76
77
    /**
78
     * Display all the staff.
79
     *
80
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
81
     */
82
    public function index()
83
	{
84
		$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...
85
    	return view('users/index', $data);
86
    }
87
88
    /**
89
     * Set the user available.
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93 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...
94
    {
95
        $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...
96
        Bouncer::retract('unavailable')->from($user);
97
        Bouncer::assign('available')->to($user);
98
99
        return redirect()->back(302);
100
    }
101
102
    /**
103
     * Set the user unavailable.
104
     *
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107 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...
108
    {
109
        $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...
110
        Bouncer::retract('available')->from($user);
111
        Bouncer::assign('unavailable')->to($user);
112
113
        return redirect()->back(302);
114
    }
115
116
    /**
117
     * Display the profile.
118
     *
119
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
120
     */
121
    public function profile()
122
    {
123
    	return view('users/profile');
124
    }
125
126
    /**
127
     * Destroy or multiple staff members.
128
     *
129
     * @param  int $id THe id off the staff member in the database.
130
     * @return \Illuminate\Http\RedirectResponse
131
     */
132
    public function destroy($id)
133
    {
134
        $user = User::find($id);
135
        $user->roles()->sync([]);
136
137
        User::destroy($id);
138
        session()->flash('message', 'User deleted');
139
140
        return redirect()->to('/staff');
141
    }
142
}
143