Completed
Pull Request — master (#107)
by Glenn
104:21 queued 64:56
created

DepartmentsController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 1
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\Departments;
8
use App\DepartmentMembers;
9
use Mail;
10
use App\User;
11
use App\Http\Controllers\Controller;
12
13
/**
14
 * Class DepartmentsController
15
 * @package App\Http\Controllers
16
 */
17
class DepartmentsController extends Controller
18
{
19
    /**
20
     * DepartmentsController Constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
        $this->middleware('lang');
26
    }
27
28
    /**
29
     * Get the departments index.
30
     *
31
     * @return view
32
     */ 
33
    public function index()
34
    {
35
        $departments = Departments::orderBy('department_name', 'asc')->with('managers')->paginate(10);
36
        return view('departments/list', ['departments' => $departments]);
37
    }
38
39
    /**
40
     * This is nothing for in the controller.
41
     * If you want to test this. You should create a phpunit function for the relation.$_COOKIE
42
     * 
43
     * TODO: Create phpunit testing function.
44
     */
45
    public function relationtest()
46
    {
47
        $departments = DepartmentMembers::All();
48
        return $departments;
49
    }    
50
51
    public function create()
52
    {
53
        $managers = User::all();
54
        return view('departments/create', ['managers' => $managers]);
55
    }
56
57
    /**
58
     * Store a newly created resource in storage.
59
     *
60
     * @param  Requests\DepartmentsValidator|Request $request
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(Requests\DepartmentsValidator $request)
64
    {
65
        // TODO: Check for mass assign - SQL queries.
66
        
67
        $departments = new Departments;
68
        $departments->department_name = $request->get('department_name');
69
        $departments->department_manager = $request->get('department_manager');
70
        $departments->department_description = $request->get('department_description');
71
        $departments->save();
72
73
        $department_id = $departments->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Departments>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
75
        $manager = new DepartmentMembers;
76
        $manager->departmentid = $department_id;
77
        $manager->userid = $request->get('department_manager');
78
        $manager->save();
79
80
        session()->flash('message', trans('FlashSession.departmentNew'));
81
        return redirect('staff/departments');
82
    }
83
84
    /**
85
     * Display the specified resource.
86
     *
87
     * @param  int  $id
88
     * @return \Illuminate\Http\Response
89
     */
90
    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...
91
    {
92
        //
93
    }
94
95
    /**
96
     * Show the form for editing the specified resource.
97
     *
98
     * @param  int  $id
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function edit($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...
102
    {
103
        return view('departments/edit');
104
    }
105
106
    /**
107
     * Update the specified resource in storage.
108
     *
109
     * @param  \Illuminate\Http\Request  $request
110
     * @param  int  $id
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function update(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...
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...
114
    {
115
        //
116
    }
117
118
    /**
119
     * Search a department in the database.
120
     *
121
     * @param Request $request
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function search(Request $request)
125
    {
126
        $term = $request->get('name');
127
128
        if (empty($term)) {
129
            return redirect('staff/departments', 302);
130
        }
131
132
        $data['departments'] = Departments::where('department_name', 'LIKE', "%$term%")
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
            ->orderBy('department_name', 'asc')
134
            ->with('managers')
135
            ->paginate(10);
136
137
        return view('departments/list', $data);
138
    }
139
140
    /**
141
     * Remove the specified resource from storage.
142
     *
143
     * @param  Request $request
144
     * @return \Illuminate\Http\Response
145
     */
146
    public function destroy(Request $request)
147
    {
148
        $query = Departments::destroy($request->get('department_id'));
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
        session()->flash('message', trans('FlashSession.departmentDestroy'));
150
151
        return redirect()->back(302);
152
    }
153
}
154