Completed
Push — api/develop ( 11afe9...d1fbdd )
by Bertrand
04:59
created

JobTitlesController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\JobTitle;
15
use HRis\Api\Requests\Admin\Job\JobTitleRequest;
16
use Swagger\Annotations as SWG;
17
18
/**
19
 * Class JobTitlesController.
20
 */
21
class JobTitlesController extends BaseController
22
{
23
    /**
24
     * @var JobTitle
25
     */
26
    protected $job_title;
27
28
    /**
29
     * @param JobTitle $job_title
30
     *
31
     * @author Bertrand Kintanar <[email protected]>
32
     */
33 14
    public function __construct(JobTitle $job_title)
34
    {
35 14
        $this->job_title = $job_title;
36 14
    }
37
38
    /**
39
     * Delete the Admin - Job Title.
40
     *
41
     * @SWG\Delete(
42
     *     path="/admin/job/titles",
43
     *     tags={"Administration"},
44
     *     consumes={"application/json"},
45
     *     summary="Delete the Admin - Job Title.",
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="Job title 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 JobTitleRequest $request
94
     *
95
     * @return \Illuminate\Http\RedirectResponse
96
     *
97
     * @author Bertrand Kintanar <[email protected]>
98
     */
99 4
    public function destroy(JobTitleRequest $request)
100
    {
101 4
        $job_title_id = $request->get('id');
102
103 4
        $response_code = $this->job_title->whereId($job_title_id)->delete();
0 ignored issues
show
Documentation Bug introduced by
The method whereId does not exist on object<HRis\Api\Eloquent\JobTitle>? 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(422, UNABLE_DELETE_MESSAGE);
107
        }
108
109 2
        return $this->_response(200, SUCCESS_DELETE_MESSAGE);
110
    }
111
112
    /**
113
     * Retrieve the Admin - Job Title.
114
     *
115
     * @SWG\Get(
116
     *     path="/admin/job/titles",
117
     *     tags={"Administration"},
118
     *     consumes={"application/json"},
119
     *     summary="Save the Admin - Job Title.",
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/titles?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="job_title", ref="#/definitions/JobTitle"),
136
     *                 ),
137
     *             ),
138
     *             @SWG\Property(property="table", type="object",
139
     *                 @SWG\Property(property="title", type="string", default="Job Title"),
140
     *                 @SWG\Property(property="permission", type="string", default="admin.job.titles"),
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="job_title"),
146
     *                     @SWG\Property(property="plural", type="string", default="job_titles"),
147
     *                     @SWG\Property(property="dashed", type="string", default="job-titles"),
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 JobTitleRequest $request
174
     *
175
     * @return \Illuminate\View\View
176
     *
177
     * @author Bertrand Kintanar <[email protected]>
178
     */
179 2
    public function index(JobTitleRequest $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
        $job_titles = $this->job_title->paginate(ROWS_PER_PAGE);
0 ignored issues
show
Documentation Bug introduced by
The method paginate does not exist on object<HRis\Api\Eloquent\JobTitle>? 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
        return $this->_response(200, SUCCESS_RETRIEVE_MESSAGE, ['data' => $job_titles, 'table' => $this->setupDataTable($job_titles)]);
184
    }
185
186
    /**
187
     * Setup table for job title.
188
     *
189
     * @param $job_titles
190
     *
191
     * @return array
192
     *
193
     * @author Bertrand Kintanar <[email protected]>
194
     */
195 2
    protected function setupDataTable($job_titles)
196
    {
197 2
        $table = [];
198
199 2
        $table['title'] = 'Job Titles';
200 2
        $table['permission'] = 'admin.job.titles';
201 2
        $table['headers'] = ['Id', 'Name', 'Description'];
202 2
        $table['model'] = [
203 2
            'singular' => 'job_title',
204 2
            'plural'   => 'job_titles',
205 2
            'dashed'   => 'job-titles',
206
        ];
207 2
        $table['items'] = $job_titles;
208
209 2
        return $table;
210
    }
211
212
    /**
213
     * Save the Admin - Job Title.
214
     *
215
     * @SWG\Post(
216
     *     path="/admin/job/titles",
217
     *     tags={"Administration"},
218
     *     consumes={"application/json"},
219
     *     summary="Save the Admin - Job Title.",
220
     *     @SWG\Response(response="201", description="Success",
221
     *         @SWG\Schema(
222
     *             title="data",
223
     *             type="object",
224
     *             required={"job_title", "message", "status_code"},
225
     *             @SWG\Property(property="job_title", ref="#/definitions/JobTitle"),
226
     *             @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"),
227
     *             @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"),
228
     *         )
229
     *     ),
230
     *     @SWG\Response(response="400", description="Token not provided",
231
     *         @SWG\Schema(
232
     *             title="data",
233
     *             type="object",
234
     *             required={"message", "status_code", "debug"},
235
     *             @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"),
236
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
237
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
238
     *         )
239
     *     ),
240
     *     @SWG\Response(response="422", description="Unable to add record to the database.",
241
     *         @SWG\Schema(
242
     *             title="data",
243
     *             type="object",
244
     *             required={"message", "status_code"},
245
     *             @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"),
246
     *             @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"),
247
     *         )
248
     *     ),
249
     *     @SWG\Parameter(
250
     *         name="job_title",
251
     *         in="body",
252
     *         required=true,
253
     *         @SWG\Property(ref="#/definitions/JobTitle")
254
     *     ),
255
     *     @SWG\Parameter(
256
     *         name="Authorization",
257
     *         in="header",
258
     *         description="JWT Token",
259
     *         required=true,
260
     *         type="string",
261
     *         default="Bearer "
262
     *     ),
263
     * )
264
     *
265
     * @param JobTitleRequest $request
266
     *
267
     * @return \Illuminate\Http\RedirectResponse
268
     *
269
     * @author Bertrand Kintanar <[email protected]>
270
     */
271 6 View Code Duplication
    public function store(JobTitleRequest $request)
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...
272
    {
273
        try {
274 6
            $job_title = $this->job_title->create($request->all());
275 6
        } catch (Exception $e) {
276
            return $this->_response(422, UNABLE_ADD_MESSAGE);
277
        }
278
279 6
        return $this->_response(201, SUCCESS_ADD_MESSAGE, compact('job_title'));
280
    }
281
282
    /**
283
     * Update the Admin - Job Title.
284
     *
285
     * @SWG\Patch(
286
     *     path="/admin/job/titles",
287
     *     tags={"Administration"},
288
     *     consumes={"application/json"},
289
     *     summary="Update the Admin - Job Title.",
290
     *     @SWG\Response(response="200", description="Success",
291
     *         @SWG\Schema(
292
     *             title="data",
293
     *             type="object",
294
     *             required={"message", "status_code"},
295
     *             @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"),
296
     *             @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"),
297
     *         )
298
     *     ),
299
     *     @SWG\Response(response="400", description="Token not provided",
300
     *         @SWG\Schema(
301
     *             title="data",
302
     *             type="object",
303
     *             required={"message", "status_code", "debug"},
304
     *             @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"),
305
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
306
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
307
     *         )
308
     *     ),
309
     *     @SWG\Response(response="404", description="Unable to retrieve record from database.",
310
     *         @SWG\Schema(
311
     *             title="data",
312
     *             type="object",
313
     *             required={"message", "status_code", "debug"},
314
     *             @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"),
315
     *             @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"),
316
     *             @SWG\Property(property="debug", type="object", description="Debug back trace"),
317
     *         )
318
     *     ),
319
     *     @SWG\Response(response="422", description="Unable to update record.",
320
     *         @SWG\Schema(
321
     *             title="data",
322
     *             type="object",
323
     *             required={"message", "status_code"},
324
     *             @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"),
325
     *             @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"),
326
     *         )
327
     *     ),
328
     *     @SWG\Parameter(
329
     *         name="job_title",
330
     *         in="body",
331
     *         required=true,
332
     *         description="job title object that needs to be updated",
333
     *         @SWG\Property(ref="#/definitions/JobTitle")
334
     *     ),
335
     *     @SWG\Parameter(
336
     *         name="Authorization",
337
     *         in="header",
338
     *         description="JWT Token",
339
     *         required=true,
340
     *         type="string",
341
     *         default="Bearer "
342
     *     ),
343
     * )
344
     *
345
     * @param JobTitleRequest $request
346
     *
347
     * @return \Illuminate\Http\RedirectResponse
348
     *
349
     * @author Bertrand Kintanar <[email protected]>
350
     */
351 4
    public function update(JobTitleRequest $request)
352
    {
353 4
        $job_title = $this->job_title->whereId($request->get('id'))->first();
0 ignored issues
show
Documentation Bug introduced by
The method whereId does not exist on object<HRis\Api\Eloquent\JobTitle>? 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...
354
355 4
        if (!$job_title) {
356 2
            return $this->_response(404, UNABLE_RETRIEVE_MESSAGE);
357
        }
358
        try {
359 2
            $job_title->update($request->only(['name', 'description']));
360 2
        } catch (Exception $e) {
361
            return $this->_response(422, UNABLE_UPDATE_MESSAGE);
362
        }
363
364 2
        return $this->_response(200, SUCCESS_UPDATE_MESSAGE);
365
    }
366
}
367