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

StaffController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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