Completed
Push — master ( c0a42e...1f844e )
by
unknown
01:30
created

OrangTuaController::index()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 1
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\OrangTua\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\OrangTua\Facades\OrangTuaFacade;
9
10
/* Models */
11
use Bantenprov\OrangTua\Models\Bantenprov\OrangTua\OrangTua;
12
13
/* Etc */
14
use Validator;
15
16
/**
17
 * The OrangTuaController class.
18
 *
19
 * @package Bantenprov\OrangTua
20
 * @author  bantenprov <[email protected]>
21
 */
22
class OrangTuaController extends Controller
23
{
24
    /**
25
     * Create a new controller instance.
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct(OrangTua $orang_tua)
30
    {
31
        $this->orang_tua = $orang_tua;
32
    }
33
34
    /**
35
     * Display a listing of the resource.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function index(Request $request)
40
    {
41
        if ($request->has('sort')) {
42
            list($sortCol, $sortDir) = explode('|', $request->sort);
43
44
            $query = $this->orang_tua->orderBy($sortCol, $sortDir);
45
        } else {
46
            $query = $this->orang_tua->orderBy('id', 'asc');
47
        }
48
49
        if ($request->exists('filter')) {
50
            $query->where(function($q) use($request) {
51
                $value = "%{$request->filter}%";
52
                $q->where('label', 'like', $value)
53
                    ->orWhere('description', 'like', $value);
54
            });
55
        }
56
57
        $perPage = $request->has('per_page') ? (int) $request->per_page : null;
58
        $response = $query->paginate($perPage);
59
60
        return response()->json($response)
61
            ->header('Access-Control-Allow-Origin', '*')
62
            ->header('Access-Control-Allow-Methods', 'GET');
63
    }
64
65
    /**
66
     * Show the form for creating a new resource.
67
     *
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function create()
71
    {
72
        $orang_tua              = $this->orang_tua;
73
        $orang_tua->id          = null;
74
        $orang_tua->label       = null;
75
        $orang_tua->description = null;
76
77
        $response['orang_tua'] = $orang_tua;
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...
78
        $response['loaded'] = true;
79
80
        return response()->json($response);
81
    }
82
83
    /**
84
     * Display the specified resource.
85
     *
86
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function store(Request $request)
90
    {
91
        $orang_tua = $this->orang_tua;
92
93
        $validator = Validator::make($request->all(), [
94
            'label'         => 'required|max:16|unique:orang_tuas,label,NULL,id,deleted_at,NULL',
95
            'description'   => 'required|max:255',
96
        ]);
97
98 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...
99
            $response['error']  = 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...
100
            $response['message'] = $validator->errors()->first();
101
        } else {
102
            $orang_tua->label       = $request->label;
103
            $orang_tua->description = $request->description;
104
            $orang_tua->save();
105
106
            $response['error'] = 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...
107
            $response['message'] = 'Success';
108
        }
109
110
        $response['loaded'] = true;
111
112
        return response()->json($response);
113
    }
114
115
    /**
116
     * Store a newly created resource in storage.
117
     *
118
     * @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...
119
     * @return \Illuminate\Http\Response
120
     */
121 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...
122
    {
123
        $orang_tua = $this->orang_tua->findOrFail($id);
124
125
        $response['orang_tua'] = $orang_tua;
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...
126
        $response['loaded'] = true;
127
128
        return response()->json($response);
129
    }
130
131
    /**
132
     * Show the form for editing the specified resource.
133
     *
134
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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 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...
138
    {
139
        $orang_tua = $this->orang_tua->findOrFail($id);
140
141
        $response['orang_tua'] = $orang_tua;
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['loaded'] = true;
143
144
        return response()->json($response);
145
    }
146
147
    /**
148
     * Update the specified resource in storage.
149
     *
150
     * @param  \Illuminate\Http\Request  $request
151
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
152
     * @return \Illuminate\Http\Response
153
     */
154
    public function update(Request $request, $id)
155
    {
156
        $orang_tua = $this->orang_tua->findOrFail($id);
157
158
        $validator = Validator::make($request->all(), [
159
            'label'         => 'required|max:16|unique:orang_tuas,label,'.$id.',id,deleted_at,NULL',
160
            'description'   => 'required|max:255',
161
        ]);
162
163 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...
164
            $response['error']  = 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...
165
            $response['message'] = $validator->errors()->first();
166
        } else {
167
            $orang_tua->label       = $request->label;
168
            $orang_tua->description = $request->description;
169
            $orang_tua->save();
170
171
            $response['error'] = 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...
172
            $response['message'] = 'Success';
173
        }
174
175
        $response['loaded'] = true;
176
177
        return response()->json($response);
178
    }
179
180
    /**
181
     * Remove the specified resource from storage.
182
     *
183
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
184
     * @return \Illuminate\Http\Response
185
     */
186 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...
187
    {
188
        $orang_tua = $this->orang_tua->findOrFail($id);
189
190
        if ($orang_tua->delete()) {
191
            $response['loaded'] = 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...
192
        } else {
193
            $response['loaded'] = 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...
194
        }
195
196
        return json_encode($response);
197
    }
198
}
199