Completed
Push — api/develop ( 8a36b7...f02bbb )
by Bertrand
09:05
created

JobTitlesController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.032
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 HRis\Api\Requests\Profile\JobRequest;
17
use HRis\Api\Transformers\JobTitleTransformer;
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\DB;
20
21
/**
22
 * Class JobTitlesController.
23
 */
24
class JobTitlesController extends BaseController
25
{
26
    /**
27
     * @var JobTitle
28
     */
29
    protected $job_title;
30
31
    /**
32
     * @param JobTitle $job_title
33
     *
34
     * @author Bertrand Kintanar <[email protected]>
35
     */
36 14
    public function __construct(JobTitle $job_title)
37
    {
38 14
        $this->job_title = $job_title;
39 14
    }
40
41
    /**
42
     * Delete the PIM - Custom Field Section.
43
     *
44
     * @param JobTitleRequest $request
45
     *
46
     * @return \Illuminate\Http\RedirectResponse
47
     *
48
     * @author Bertrand Kintanar <[email protected]>
49
     */
50 4
    public function destroy(JobTitleRequest $request)
51
    {
52 4
        $job_title_id = $request->get('id');
53
54 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...
55
56 4
        if (!$response_code) {
57 2
            return $this->response()->array(['message' => UNABLE_DELETE_MESSAGE, 'status_code' => 422])->statusCode(422);
58
        }
59
60 2
        return $this->response()->array(['message' => SUCCESS_DELETE_MESSAGE, 'status_code' => 200])->statusCode(200);
61
    }
62
63
    /**
64
     * Show a PIM - Custom Field Section.
65
     *
66
     * @param JobRequest $request
67
     *
68
     * @return \Illuminate\View\View
69
     *
70
     * @author Bertrand Kintanar <[email protected]>
71
     */
72 2
    public function index(JobRequest $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...
73
    {
74 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...
75
76 2
        $response['data'] = $job_titles;
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...
77 2
        $response['table'] = $this->setupDataTable($job_titles);
78 2
        $response['message'] = SUCCESS_RETRIEVE_MESSAGE;
79 2
        $response['status_code'] = 200;
80
81 2
        return $this->response()->array($response)->statusCode(200);
82
    }
83
84
    /**
85
     * Save the PIM - Custom Field Section.
86
     *
87
     * @param JobTitleRequest $request
88
     *
89
     * @return \Illuminate\Http\RedirectResponse
90
     *
91
     * @author Bertrand Kintanar <[email protected]>
92
     */
93 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...
94
    {
95
        try {
96 6
            $job_title = $this->job_title->create($request->all());
97 6
        } catch (Exception $e) {
98
            return $this->response()->array(['message' => UNABLE_ADD_MESSAGE, 'status_code' => 422])->statusCode(422);
99
        }
100
101 6
        return $this->response()->array(['job_title' => $job_title, 'message' => SUCCESS_ADD_MESSAGE, 'status_code' => 201])->statusCode(201);
102
    }
103
104
    /**
105
     * Setup table for custom field section.
106
     *
107
     * @param $job_titles
108
     *
109
     * @return array
110
     *
111
     * @author Bertrand Kintanar <[email protected]>
112
     */
113 2 View Code Duplication
    public function setupDataTable($job_titles)
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...
114
    {
115 2
        $table = [];
116
117 2
        $table['title'] = 'Job Titles';
118 2
        $table['permission'] = 'admin.job.titles';
119 2
        $table['headers'] = ['Id', 'Name', 'Description'];
120 2
        $table['model'] = [
121 2
            'singular' => 'job_title',
122 2
            'plural'   => 'job_titles',
123 2
            'dashed'   => 'job-titles',
124
        ];
125 2
        $table['items'] = $job_titles;
126
127 2
        return $table;
128
    }
129
130
    /**
131
     * Update the PIM - Custom Field Section.
132
     *
133
     * @param JobTitleRequest $request
134
     *
135
     * @return \Illuminate\Http\RedirectResponse
136
     *
137
     * @author Bertrand Kintanar <[email protected]>
138
     */
139 4 View Code Duplication
    public function update(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...
140
    {
141 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...
142
143 4
        if (!$job_title) {
144 2
            return $this->response()->array(['message' => UNABLE_RETRIEVE_MESSAGE, 'status_code' => 404])->statusCode(404);
145
        }
146
        try {
147
148 2
            $job_title->update($request->only(['name', 'description']));
149 2
        } catch (Exception $e) {
150
151
            return $this->response()->array(['message' => UNABLE_UPDATE_MESSAGE, 'status_code' => 422])->statusCode(422);
152
        }
153
154 2
        return $this->response()->array(['message' => SUCCESS_UPDATE_MESSAGE, 'status_code' => 200])->statusCode(200);
155
    }
156
}
157