Completed
Push — master ( bbc98f...1d64bf )
by
unknown
03:50
created

NilaiController::index()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 16
nop 1
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Nilai\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Nilai\Facades\NilaiFacade;
9
10
/* Models */
11
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
12
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The NilaiController class.
19
 *
20
 * @package Bantenprov\Nilai
21
 * @author  bantenprov <[email protected]>
22
 */
23
class NilaiController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $siswaModel;
31
32
    public function __construct(Nilai $nilai, Siswa $siswa)
33
    {
34
        $this->nilai = $nilai;
35
        $this->siswaModel = $siswa;
36
    }
37
38
    /**
39
     * Display a listing of the resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function index(Request $request)
44
    {
45
        if (request()->has('sort')) {
46
            list($sortCol, $sortDir) = explode('|', request()->sort);
47
48
            $query = $this->nilai->orderBy($sortCol, $sortDir);
49
        } else {
50
            $query = $this->nilai->orderBy('id', 'asc');
51
        }
52
53
        if ($request->exists('filter')) {
54
            $query->where(function($q) use($request) {
55
                $value = "%{$request->filter}%";
56
                $q->where('label', 'like', $value)
57
                    ->orWhere('description', 'like', $value);
58
            });
59
        }
60
61
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
62
        $response = $query->paginate($perPage);
63
        
64
        foreach($response as $siswa){            
65
            array_set($response->data, 'siswa_id', $siswa->siswa->label);           
66
        }
67
        
68
        return response()->json($response)
69
            ->header('Access-Control-Allow-Origin', '*')
70
            ->header('Access-Control-Allow-Methods', 'GET');
71
    }
72
73
    /**
74
     * Show the form for creating a new resource.
75
     *
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function create()
79
    {        
80
        $siswa = $this->siswaModel->all();
81
        
82
        $response['siswa'] = $siswa;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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
        $response['status'] = true;
84
85
        return response()->json($response);
86
    }
87
88
    /**
89
     * Display the specified resource.
90
     *
91
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
     * @return \Illuminate\Http\Response
93
     */
94
    public function store(Request $request)
95
    {
96
        $nilai = $this->nilai;
97
98
        $validator = Validator::make($request->all(), [
99
            'siswa_id' => 'required',
100
            'label' => 'required|max:16|unique:nilais,label',
101
            'description' => 'max:255',
102
        ]);
103
104 View Code Duplication
        if($validator->fails()){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
            $check = $nilai->where('label',$request->label)->whereNull('deleted_at')->count();
106
107
            if ($check > 0) {
108
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
109
            } else {
110
                $nilai->siswa_id = $request->input('siswa_id');
111
                $nilai->label = $request->input('label');
112
                $nilai->description = $request->input('description');
113
                $nilai->save();
114
115
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
116
            }
117
        } else {
118
            $nilai->siswa_id = $request->input('siswa_id');
119
            $nilai->label = $request->input('label');
120
            $nilai->description = $request->input('description');
121
            $nilai->save();
122
123
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
124
        }
125
126
        $response['status'] = true;
127
128
        return response()->json($response);
129
    }
130
131
    /**
132
     * Store a newly created resource in storage.
133
     *
134
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
135
     * @return \Illuminate\Http\Response
136
     */
137 View Code Duplication
    public function show($id)
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...
138
    {
139
        $nilai = $this->nilai->findOrFail($id);
140
141
        $response['nilai'] = $nilai;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
142
        $response['status'] = true;
143
144
        return response()->json($response);
145
    }
146
147
    /**
148
     * Show the form for editing the specified resource.
149
     *
150
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
151
     * @return \Illuminate\Http\Response
152
     */
153 View Code Duplication
    public function edit($id)
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...
154
    {
155
        $nilai = $this->nilai->findOrFail($id);
156
157
        $response['nilai'] = $nilai;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
158
        $response['siswa'] = $nilai->siswa;
159
        $response['status'] = true;
160
161
        return response()->json($response);
162
    }
163
164
    /**
165
     * Update the specified resource in storage.
166
     *
167
     * @param  \Illuminate\Http\Request  $request
168
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
169
     * @return \Illuminate\Http\Response
170
     */
171
    public function update(Request $request, $id)
172
    {
173
        $nilai = $this->nilai->findOrFail($id);
174
175
        if ($request->input('old_label') == $request->input('label'))
176
        {
177
            $validator = Validator::make($request->all(), [
178
                'label' => 'required|max:16',
179
                'description' => 'max:255',
180
                'siswa_id' => 'required'
181
            ]);
182
        } else {
183
            $validator = Validator::make($request->all(), [
184
                'label' => 'required|max:16|unique:nilais,label',
185
                'description' => 'max:255',
186
                'siswa_id' => 'required'
187
            ]);
188
        }
189
190 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
191
            $check = $nilai->where('label',$request->label)->whereNull('deleted_at')->count();
192
193
            if ($check > 0) {
194
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
195
            } else {
196
                $nilai->label = $request->input('label');
197
                $nilai->description = $request->input('description');
198
                $nilai->siswa_id = $request->input('siswa_id');
199
                $nilai->save();
200
201
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
202
            }
203
        } else {
204
            $nilai->label = $request->input('label');
205
            $nilai->description = $request->input('description');
206
            $nilai->siswa_id = $request->input('siswa_id');
207
            $nilai->save();
208
209
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
210
        }
211
212
        $response['status'] = true;
213
214
        return response()->json($response);
215
    }
216
217
    /**
218
     * Remove the specified resource from storage.
219
     *
220
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
221
     * @return \Illuminate\Http\Response
222
     */
223 View Code Duplication
    public function destroy($id)
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...
224
    {
225
        $nilai = $this->nilai->findOrFail($id);
226
227
        if ($nilai->delete()) {
228
            $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
229
        } else {
230
            $response['status'] = false;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
231
        }
232
233
        return json_encode($response);
234
    }
235
}
236