KegiatanController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Kegiatan\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Kegiatan\Facades\KegiatanFacade;
9
10
/* Models */
11
use Bantenprov\Kegiatan\Models\Bantenprov\Kegiatan\Kegiatan;
12
13
/* Etc */
14
use Validator;
15
16
/**
17
 * The KegiatanController class.
18
 *
19
 * @package Bantenprov\Kegiatan
20
 * @author  bantenprov <[email protected]>
21
 */
22
class KegiatanController 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(Kegiatan $kegiatan)
30
    {
31
        $this->kegiatan = $kegiatan;
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->kegiatan->orderBy($sortCol, $sortDir);
45
        } else {
46
            $query = $this->kegiatan->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
        $kegiatan = $this->kegiatan;
73
74
        $response['kegiatan'] = $kegiatan;
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...
75
        $response['status'] = true;
76
77
        return response()->json($kegiatan);
78
    }
79
80
    /**
81
     * Display the specified resource.
82
     *
83
     * @param  \App\Kegiatan  $kegiatan
0 ignored issues
show
Bug introduced by
There is no parameter named $kegiatan. 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...
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function store(Request $request)
87
    {
88
        $kegiatan = $this->kegiatan;
89
90
        $validator = Validator::make($request->all(), [
91
            'label' => 'required|unique:kegiatans,label',
92
            'description' => 'required',
93
            'tanggal_mulai' => 'required',
94
            'tanggal_selesai' => 'required',
95
        ]);
96
97 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...
98
            $check = $kegiatan->where('label',$request->label)->whereNull('deleted_at')->count();
99
100
            if ($check > 0) {
101
                $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...
102
            } else {
103
                $kegiatan->label = $request->input('label');
104
                $kegiatan->description = $request->input('description');
105
                $kegiatan->tanggal_mulai = $request->input('tanggal_mulai');
106
                $kegiatan->tanggal_selesai = $request->input('tanggal_selesai');
107
                $kegiatan->save();
108
109
                $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...
110
            }
111
        } else {
112
            $kegiatan->label = $request->input('label');
113
            $kegiatan->description = $request->input('description');
114
            $kegiatan->tanggal_mulai = $request->input('tanggal_mulai');
115
            $kegiatan->tanggal_selesai = $request->input('tanggal_selesai');
116
            $kegiatan->save();
117
118
            $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...
119
        }
120
121
        $response['status'] = true;
122
123
        return response()->json($response);
124
    }
125
126
    /**
127
     * Store a newly created resource in storage.
128
     *
129
     * @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...
130
     * @return \Illuminate\Http\Response
131
     */
132 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...
133
    {
134
        $kegiatan = $this->kegiatan->findOrFail($id);
135
136
        $response['kegiatan'] = $kegiatan;
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...
137
        $response['status'] = true;
138
139
        return response()->json($response);
140
    }
141
142
    /**
143
     * Show the form for editing the specified resource.
144
     *
145
     * @param  \App\Kegiatan  $kegiatan
0 ignored issues
show
Bug introduced by
There is no parameter named $kegiatan. 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...
146
     * @return \Illuminate\Http\Response
147
     */
148 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...
149
    {
150
        $kegiatan = $this->kegiatan->findOrFail($id);
151
152
        $response['kegiatan'] = $kegiatan;
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...
153
        $response['status'] = true;
154
155
        return response()->json($response);
156
    }
157
158
    /**
159
     * Update the specified resource in storage.
160
     *
161
     * @param  \Illuminate\Http\Request  $request
162
     * @param  \App\Kegiatan  $kegiatan
0 ignored issues
show
Bug introduced by
There is no parameter named $kegiatan. 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...
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function update(Request $request, $id)
166
    {
167
        $kegiatan = $this->kegiatan->findOrFail($id);
168
169
        if ($request->input('old_label') == $request->input('label'))
170
        {
171
            $validator = Validator::make($request->all(), [
172
                'label' => 'required',
173
                'description' => 'required',
174
                'tanggal_mulai' => 'required',
175
                'tanggal_selesai' => 'required',
176
            ]);
177
        } else {
178
            $validator = Validator::make($request->all(), [
179
                'label' => 'required|unique:kegiatans,label',
180
                'description' => 'required',
181
                'tanggal_mulai' => 'required',
182
                'tanggal_selesai' => 'required',
183
            ]);
184
        }
185
186 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...
187
            $check = $kegiatan->where('label',$request->label)->whereNull('deleted_at')->count();
188
189
            if ($check > 0) {
190
                $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...
191
            } else {
192
                $kegiatan->label = $request->input('label');
193
                $kegiatan->description = $request->input('description');
194
                $kegiatan->tanggal_mulai = $request->input('tanggal_mulai');
195
                $kegiatan->tanggal_selesai = $request->input('tanggal_selesai');
196
                $kegiatan->save();
197
198
                $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...
199
            }
200
        } else {
201
            $kegiatan->label = $request->input('label');
202
            $kegiatan->description = $request->input('description');
203
            $kegiatan->tanggal_mulai = $request->input('tanggal_mulai');
204
            $kegiatan->tanggal_selesai = $request->input('tanggal_selesai');
205
206
            $kegiatan->save();
207
208
            $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...
209
        }
210
211
        $response['status'] = true;
212
213
        return response()->json($response);
214
    }
215
216
    /**
217
     * Remove the specified resource from storage.
218
     *
219
     * @param  \App\Kegiatan  $kegiatan
0 ignored issues
show
Bug introduced by
There is no parameter named $kegiatan. 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...
220
     * @return \Illuminate\Http\Response
221
     */
222 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...
223
    {
224
        $kegiatan = $this->kegiatan->findOrFail($id);
225
226
        if ($kegiatan->delete()) {
227
            $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...
228
        } else {
229
            $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...
230
        }
231
232
        return json_encode($response);
233
    }
234
}
235