Completed
Push — master ( 015a60...9d3bbf )
by Glenn
05:38 queued 03:02
created

DepartmentController::get_departments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Departments;
6
use Illuminate\Http\Request;
7
8
use App\Http\Requests;
9
10
/**
11
 * Class DepartmentController
12
 * @package App\Http\Controllers
13
 *
14
 * TODO: set routes.
15
 * TODO: the controller needs phpunit testing.
16
 */
17
class DepartmentController extends Controller
18
{
19
    /**
20
     * DepartmentController constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
        $this->middleware('lang');
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index()
34
    {
35
        $data['query'] = Departments::with('managers')->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...
36
        return view('departments.index', $data);
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function create()
45
    {
46
        return view('departments.insert');
47
    }
48
49
    /**
50
     * Search for a specific department
51
     *
52
     * @param  Request $request
53
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
54
     */
55
    public function search(Request $request)
56
    {
57
        $term = $request->get('name');
58
        $data['query'] = Departments::where('name', 'LIKE', "%$term%")->with('managers')->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...
59
        return view('departments.index', $data);
60
    }
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  Requests\DepartmentsValidator $input
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function store(Requests\DepartmentsValidator $input)
69
    {
70
        Departments::create($input->except('_token'));
71
        return redirect()->route('departments.index');
72
    }
73
74
    /**
75
     * Display the specified resource.
76
     *
77
     * @param  int  $id the department id in the database.
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function show($id)
81
    {
82
        $data['query'] = Departments::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...
83
        return view('departments.specific', $data);
84
    }
85
86
    /**
87
     * Show the form for editing the specified resource.
88
     *
89
     * @param  int  $id the department id in the database.
90
     * @return \Illuminate\Http\Response
91
     */
92
    public function edit($id)
93
    {
94
        $data['query'] = Departments::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...
95
        return view('departments.update', $data);
96
    }
97
98
    /**
99
     * Update the specified resource in storage.
100
     *
101
     * @param  Requests\DepartmentsValidator $input
102
     * @param  int $id the department id in the database.
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function update(Requests\DepartmentsValidator $input, $id)
106
    {
107
        Departments::find($id)->update($input->except('_token'));
108
        return redirect()->route('departments.index');
109
    }
110
111
    /**
112
     * Remove the specified resource from storage.
113
     *
114
     * @param  int  $id the department id in the database.
115
     * @return \Illuminate\Http\Response
116
     */
117
    public function destroy($id)
118
    {
119
        Departments::destroy($id);
120
        return redirect()->back(302);
121
    }
122
123
    /**
124
     * Get all the departments
125
     *
126
     * @return string
127
     */
128 View Code Duplication
    public function get_departments()
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...
129
    {
130
        $items = Departments::all();
131
        $data2 = [];
132
133
        foreach($items as $department) {
134
            $data2[] = [
135
                'value' => $department["id"],
136
                'text'  => $department["name"]
137
            ];
138
        }
139
140
        return json_encode($data2);
141
    }    
142
}
143