Completed
Push — api/develop ( 3a7de4...11afe9 )
by Bertrand
04:51
created

EmploymentStatusController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 351
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 3
dl 351
loc 351
ccs 38
cts 40
cp 0.95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A destroy() 12 12 2
A index() 11 11 1
A setupDataTable() 16 16 1
A store() 10 10 2
A update() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link    http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Api\Controllers\Admin\Job;
11
12
use Exception;
13
use HRis\Api\Controllers\BaseController;
14
use HRis\Api\Eloquent\EmploymentStatus;
15
use HRis\Api\Requests\Admin\Job\EmploymentStatusRequest;
16
use Swagger\Annotations as SWG;
17
18
/**
19
 * Class EmploymentStatusController.
20
 */
21 View Code Duplication
class EmploymentStatusController extends BaseController
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
    /**
24
     * @var EmploymentStatus
25
     */
26
    protected $employment_status;
27
28
    /**
29
     * @param EmploymentStatus $employment_status
30
     *
31
     * @author Bertrand Kintanar <[email protected]>
32
     */
33 14
    public function __construct(EmploymentStatus $employment_status)
34
    {
35 14
        $this->employment_status = $employment_status;
36 14
    }
37
38
    /**
39
     * Delete the Admin - Employment Status.
40
     *
41
     * @SWG\Delete(
42
     *     path="/admin/job/employment-status",
43
     *     tags={"Administration"},
44
     *     consumes={"application/json"},
45
     *     summary="Delete the Admin - Employment Status.",
46
     *     @SWG\Response(response="200", description="Success",
47
     *         @SWG\Schema(
48
     *             title="data",
49
     *             type="object",
50
     *             required={"message", "status_code"},
51
     *             @SWG\Property(property="message", type="string", default="Record successfully deleted.", description="Status message from server"),
52
     *             @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"),
53
     *         )
54
     *     ),
55
     *     @SWG\Response(response="400", description="Token not provided",
56
     *         @SWG\Schema(
57
     *             title="data",
58
     *             type="object",
59
     *             required={"message", "status_code", "debug"},
60
     *             @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"),
61
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
62
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
63
     *         )
64
     *     ),
65
     *     @SWG\Response(response="422", description="Unable to delete record from the database.",
66
     *         @SWG\Schema(
67
     *             title="data",
68
     *             type="object",
69
     *             required={"message", "status_code"},
70
     *             @SWG\Property(property="message", type="string", default="Unable to delete record from the database.", description="Status message from server"),
71
     *             @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"),
72
     *         )
73
     *     ),
74
     *     @SWG\Parameter(
75
     *         name="id",
76
     *         in="formData",
77
     *         description="Employment status id to be deleted",
78
     *         required=true,
79
     *         type="integer",
80
     *         format="int64",
81
     *         default=1,
82
     *     ),
83
     *     @SWG\Parameter(
84
     *         name="Authorization",
85
     *         in="header",
86
     *         description="JWT Token",
87
     *         required=true,
88
     *         type="string",
89
     *         default="Bearer "
90
     *     ),
91
     * )
92
     *
93
     * @param EmploymentStatusRequest $request
94
     *
95
     * @return \Illuminate\Http\RedirectResponse
96
     *
97
     * @author Bertrand Kintanar <[email protected]>
98
     */
99 4
    public function destroy(EmploymentStatusRequest $request)
100
    {
101 4
        $employment_status_id = $request->get('id');
102
103 4
        $response_code = $this->employment_status->whereId($employment_status_id)->delete();
0 ignored issues
show
Documentation Bug introduced by
The method whereId does not exist on object<HRis\Api\Eloquent\EmploymentStatus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
105 4
        if (!$response_code) {
106 2
            return $this->response()->array(['message' => UNABLE_DELETE_MESSAGE, 'status_code' => 422])->statusCode(422);
107
        }
108
109 2
        return $this->response()->array(['message' => SUCCESS_DELETE_MESSAGE, 'status_code' => 200])->statusCode(200);
110
    }
111
112
    /**
113
     * Retrieve the Admin - Employment Status.
114
     *
115
     * @SWG\Get(
116
     *     path="/admin/job/employment-status",
117
     *     tags={"Administration"},
118
     *     consumes={"application/json"},
119
     *     summary="Save the Admin - Employment Status.",
120
     *     @SWG\Response(response="200", description="Success",
121
     *         @SWG\Schema(
122
     *             title="data",
123
     *             type="object",
124
     *             required={"data", "table", "message", "status_code"},
125
     *             @SWG\Property(property="data", type="object",
126
     *                 @SWG\Property(property="total", type="integer", default=22),
127
     *                 @SWG\Property(property="per_page", type="integer", default=10),
128
     *                 @SWG\Property(property="current_page", type="integer", default=1),
129
     *                 @SWG\Property(property="last_page", type="integer", default=3),
130
     *                 @SWG\Property(property="next_page_url", type="string", default="https://api.hris.dev/api/admin/job/employment-status?page=2"),
131
     *                 @SWG\Property(property="prev_page_url", type="string", default="null"),
132
     *                 @SWG\Property(property="from", type="integer", default=1),
133
     *                 @SWG\Property(property="to", type="integer", default=10),
134
     *                 @SWG\Property(property="data", type="array",
135
     *                     @SWG\Items(title="employment_status", ref="#/definitions/EmploymentStatus"),
136
     *                 ),
137
     *             ),
138
     *             @SWG\Property(property="table", type="object",
139
     *                 @SWG\Property(property="title", type="string", default="Employment Status"),
140
     *                 @SWG\Property(property="permission", type="string", default="admin.job.employment-status"),
141
     *                 @SWG\Property(property="headers", type="array",
142
     *                     @SWG\Items(title="Id", type="string", default="Id"),
143
     *                 ),
144
     *                 @SWG\Property(property="model", type="object",
145
     *                     @SWG\Property(property="singular", type="string", default="employment_status"),
146
     *                     @SWG\Property(property="plural", type="string", default="employment_statuses"),
147
     *                     @SWG\Property(property="dashed", type="string", default="employment-statuses"),
148
     *                 ),
149
     *                 @SWG\Property(property="data", type="array"),
150
     *             ),
151
     *             @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"),
152
     *             @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"),
153
     *         )
154
     *     ),
155
     *     @SWG\Parameter(
156
     *         name="page",
157
     *         in="query",
158
     *         description="Page number for pagination",
159
     *         required=true,
160
     *         type="string",
161
     *         default="1"
162
     *     ),
163
     *     @SWG\Parameter(
164
     *         name="Authorization",
165
     *         in="header",
166
     *         description="JWT Token",
167
     *         required=true,
168
     *         type="string",
169
     *         default="Bearer "
170
     *     ),
171
     * )
172
     *
173
     * @param EmploymentStatusRequest $request
174
     *
175
     * @return \Illuminate\View\View
176
     *
177
     * @author Bertrand Kintanar <[email protected]>
178
     */
179 2
    public function index(EmploymentStatusRequest $request)
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...
180
    {
181 2
        $employment_statuses = $this->employment_status->paginate(ROWS_PER_PAGE);
0 ignored issues
show
Documentation Bug introduced by
The method paginate does not exist on object<HRis\Api\Eloquent\EmploymentStatus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
182
183 2
        $response['data'] = $employment_statuses;
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...
184 2
        $response['table'] = $this->setupDataTable($employment_statuses);
185 2
        $response['message'] = SUCCESS_RETRIEVE_MESSAGE;
186 2
        $response['status_code'] = 200;
187
188 2
        return $this->response()->array($response)->statusCode(200);
189
    }
190
191
    /**
192
     * Setup table for employment status.
193
     *
194
     * @param $employment_statuses
195
     *
196
     * @return array
197
     *
198
     * @author Bertrand Kintanar <[email protected]>
199
     */
200 2
    protected function setupDataTable($employment_statuses)
201
    {
202 2
        $table = [];
203
204 2
        $table['title'] = 'Employment Statuses';
205 2
        $table['permission'] = 'admin.job.employment-status';
206 2
        $table['headers'] = ['Id', 'Name'];
207 2
        $table['model'] = [
208 2
            'singular' => 'employment_status',
209 2
            'plural'   => 'employment_statuses',
210 2
            'dashed'   => 'employment-statuses',
211
        ];
212 2
        $table['items'] = $employment_statuses;
213
214 2
        return $table;
215
    }
216
217
    /**
218
     * Save the Admin - Employment Status.
219
     *
220
     * @SWG\Post(
221
     *     path="/admin/job/employment-status",
222
     *     tags={"Administration"},
223
     *     consumes={"application/json"},
224
     *     summary="Save the Admin - Employment Status.",
225
     *     @SWG\Response(response="201", description="Success",
226
     *         @SWG\Schema(
227
     *             title="data",
228
     *             type="object",
229
     *             required={"employment_status", "message", "status_code"},
230
     *             @SWG\Property(property="employment_status", ref="#/definitions/EmploymentStatus"),
231
     *             @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"),
232
     *             @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"),
233
     *         )
234
     *     ),
235
     *     @SWG\Response(response="400", description="Token not provided",
236
     *         @SWG\Schema(
237
     *             title="data",
238
     *             type="object",
239
     *             required={"message", "status_code", "debug"},
240
     *             @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"),
241
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
242
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
243
     *         )
244
     *     ),
245
     *     @SWG\Response(response="422", description="Unable to add record to the database.",
246
     *         @SWG\Schema(
247
     *             title="data",
248
     *             type="object",
249
     *             required={"message", "status_code"},
250
     *             @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"),
251
     *             @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"),
252
     *         )
253
     *     ),
254
     *     @SWG\Parameter(
255
     *         name="employment_status",
256
     *         in="body",
257
     *         required=true,
258
     *         @SWG\Property(ref="#/definitions/EmploymentStatus")
259
     *     ),
260
     *     @SWG\Parameter(
261
     *         name="Authorization",
262
     *         in="header",
263
     *         description="JWT Token",
264
     *         required=true,
265
     *         type="string",
266
     *         default="Bearer "
267
     *     ),
268
     * )
269
     *
270
     * @param EmploymentStatusRequest $request
271
     *
272
     * @return \Illuminate\Http\RedirectResponse
273
     *
274
     * @author Bertrand Kintanar <[email protected]>
275
     */
276 6
    public function store(EmploymentStatusRequest $request)
277
    {
278
        try {
279 6
            $employment_status = $this->employment_status->create($request->all());
280 6
        } catch (Exception $e) {
281
            return $this->response()->array(['message' => UNABLE_ADD_MESSAGE, 'status_code' => 422])->statusCode(422);
282
        }
283
284 6
        return $this->response()->array(['employment_status' => $employment_status, 'message' => SUCCESS_ADD_MESSAGE, 'status_code' => 201])->statusCode(201);
285
    }
286
287
    /**
288
     * Update the Admin - Employment Status.
289
     *
290
     * @SWG\Patch(
291
     *     path="/admin/job/employment-status",
292
     *     tags={"Administration"},
293
     *     consumes={"application/json"},
294
     *     summary="Update the Admin - Employment Status.",
295
     *     @SWG\Response(response="200", description="Success",
296
     *         @SWG\Schema(
297
     *             title="data",
298
     *             type="object",
299
     *             required={"message", "status_code"},
300
     *             @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"),
301
     *             @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"),
302
     *         )
303
     *     ),
304
     *     @SWG\Response(response="400", description="Token not provided",
305
     *         @SWG\Schema(
306
     *             title="data",
307
     *             type="object",
308
     *             required={"message", "status_code", "debug"},
309
     *             @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"),
310
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
311
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
312
     *         )
313
     *     ),
314
     *     @SWG\Response(response="404", description="Unable to retrieve record from database.",
315
     *         @SWG\Schema(
316
     *             title="data",
317
     *             type="object",
318
     *             required={"message", "status_code", "debug"},
319
     *             @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"),
320
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
321
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
322
     *         )
323
     *     ),
324
     *     @SWG\Response(response="422", description="Unable to update record.",
325
     *         @SWG\Schema(
326
     *             title="data",
327
     *             type="object",
328
     *             required={"message", "status_code"},
329
     *             @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"),
330
     *             @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"),
331
     *         )
332
     *     ),
333
     *     @SWG\Parameter(
334
     *         name="employment_status",
335
     *         in="body",
336
     *         required=true,
337
     *         description="employment status object that needs to be updated",
338
     *         @SWG\Property(ref="#/definitions/EmploymentStatus")
339
     *     ),
340
     *     @SWG\Parameter(
341
     *         name="Authorization",
342
     *         in="header",
343
     *         description="JWT Token",
344
     *         required=true,
345
     *         type="string",
346
     *         default="Bearer "
347
     *     ),
348
     * )
349
     *
350
     * @param EmploymentStatusRequest $request
351
     *
352
     * @return \Illuminate\Http\RedirectResponse
353
     *
354
     * @author Bertrand Kintanar <[email protected]>
355
     */
356 4
    public function update(EmploymentStatusRequest $request)
357
    {
358 4
        $employment_status = $this->employment_status->whereId($request->get('id'))->first();
0 ignored issues
show
Documentation Bug introduced by
The method whereId does not exist on object<HRis\Api\Eloquent\EmploymentStatus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
359
360 4
        if (!$employment_status) {
361 2
            return $this->response()->array(['message' => UNABLE_RETRIEVE_MESSAGE, 'status_code' => 404])->statusCode(404);
362
        }
363
        try {
364 2
            $employment_status->update($request->only(['name', 'description']));
365 2
        } catch (Exception $e) {
366
            return $this->response()->array(['message' => UNABLE_UPDATE_MESSAGE, 'status_code' => 422])->statusCode(422);
367
        }
368
369 2
        return $this->response()->array(['message' => SUCCESS_UPDATE_MESSAGE, 'status_code' => 200])->statusCode(200);
370
    }
371
}
372