Completed
Push — api/develop ( ceb0fc...52cb6e )
by Bertrand
25:03
created

BaseController::loggedEmployee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
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;
11
12
use Dingo\Api\Routing\Helpers;
13
use Exception;
14
use HRis\Api\Eloquent\Employee;
15
use Illuminate\Routing\Controller;
16
use Swagger\Annotations as SWG;
17
use Tymon\JWTAuth\Facades\JWTAuth;
18
19
/**
20
 * @SWG\Swagger(
21
 *     schemes={"https", "http"},
22
 *     host="api.hris.dev",
23
 *     basePath="/api",
24
 *     @SWG\Info(
25
 *         version="1.0.0",
26
 *         title="HRis",
27
 *         description="Human Resource and Payroll System",
28
 *         @SWG\Contact(
29
 *             email="[email protected]"
30
 *         )
31
 *     ),
32
 *     @SWG\ExternalDocumentation(
33
 *         description="Fork HRis on GitHub",
34
 *         url="https://github.com/bkintanar/HRis"
35
 *     )
36
 * )
37
 */
38
class BaseController extends Controller
39
{
40
    use Helpers;
41
42
    public $data = [];
43
44
    /**
45
     * @return \Dingo\Api\Http\Response
46
     */
47
    public function apiInformation()
48
    {
49
        $data = [
50
            'schema'        => ['http', 'https'],
51
            'host'          => 'api.hris.dev',
52
            'version'       => '1.0.0',
53 188
            'title'         => 'HRis',
54
            'description'   => 'Human Resource and Payroll System',
55 188
            'contact'       => [
56 188
                'email' => '[email protected]',
57 188
            ],
58
            'documentation' => [
59 188
                'description' => 'Fork HRis on GitHub',
60 186
                'url'         => 'https://github.com/bkintanar/HRis',
61 186
            ],
62
        ];
63 188
64
        return $this->responseAPI(200, SUCCESS_RETRIEVE_MESSAGE, ['api' => $data]);
65
    }
66
67
    /**
68
     * @return Employee
69
     */
70
    protected function loggedEmployee()
71
    {
72
        $user = JWTAuth::parseToken()->authenticate();
73
74
        return $user->employee;
75
    }
76
77 76
    /**
78
     * Standard saving of model.
79
     *
80 76
     * @param $request
81 76
     * @param $model
82 6
     * @param $name
83
     *
84
     * @return \Dingo\Api\Http\Response
85 70
     *
86
     * @author Bertrand Kintanar <[email protected]>
87
     */
88
    protected function storeModel($request, $model, $name)
89
    {
90
        try {
91
            $item = $model->create($request->all());
92
        } catch (Exception $e) {
93
            return $this->responseAPI(422, UNABLE_ADD_MESSAGE);
94
        }
95
96
        return $this->responseAPI(201, SUCCESS_ADD_MESSAGE, [$name => $item]);
97
    }
98
99 32
    /**
100
     * Standard response for any request.
101 32
     *
102
     * @param        $status_code
103 32
     * @param string $message
104
     * @param array  $data
105 32
     *
106 16
     * @return \Dingo\Api\Http\Response
107
     *
108
     * @author Bertrand Kintanar <[email protected]>
109 16
     */
110 4
    protected function responseAPI($status_code, $message = '', $data = [])
111 4
    {
112 12
        $response = [];
113
        $response['message'] = $message;
114 16
        $response['status_code'] = $status_code;
115
116
        if (!empty($data)) {
117
            $response = array_merge($response, $data);
118 16
        }
119
120
        return $this->response->withArray($response)->statusCode($status_code);
121
    }
122
123
    /**
124
     * Standard updating of model.
125
     *
126
     * @param       $request
127
     * @param       $model
128
     * @param array $only
129
     *
130
     * @return \Dingo\Api\Http\Response
131 20
     *
132
     * @author Bertrand Kintanar <[email protected]>
133 20
     */
134
    protected function updateModel($request, $model, $only = [])
135 20
    {
136
        $model_id = $request->get('id');
137
138
        $item = $model->whereId($model_id)->first();
139 20
140
        if (!$item) {
141
            return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE);
142
        }
143
        try {
144
            if (!empty($only)) {
145
                $item->update($request->only($only));
146
            } else {
147
                $item->update($request->all());
148
            }
149
        } catch (Exception $e) {
150
            return $this->responseAPI(422, UNABLE_UPDATE_MESSAGE);
151
        }
152
153
        return $this->responseAPI(200, SUCCESS_UPDATE_MESSAGE);
154
    }
155
156
    /**
157
     * Standard deleting of model.
158
     *
159
     * @param $item
160
     * @param $model
161
     *
162
     * @return \Dingo\Api\Http\Response
163
     *
164
     * @author Bertrand Kintanar <[email protected]>
165
     */
166
    protected function destroyModel($item, $model)
167
    {
168
        $response_code = $model->whereId($item->id)->delete();
169
170
        if (!$response_code) {
171
            return $this->responseAPI(422, UNABLE_DELETE_MESSAGE);
172
        }
173
174
        return $this->responseAPI(200, SUCCESS_DELETE_MESSAGE);
175
    }
176
}
177