Completed
Push — master ( 25f5fb...fb8990 )
by richard
11:05
created

BaseController::getResourceSingleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Api\V1;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
use Dingo\Api\Routing\Helpers;
9
10
use Validator;
11
12
class BaseController extends Controller
13
{
14
    use Helpers;
15
16
    protected $model;
17
18
    protected $requests;
19
20
    /**
21
     * Create a new controller instance.
22
     *
23
     * @return void
24
     */
25
    public function __construct(Request $request)
26
    {
27
        $this->requests = $request;
28
    }
29
30
31
    /**
32
     * Get resource name.
33
     *
34
     * @return string
35
     */
36
    public function getResourceName()
37
    {
38
        return $this->getResourceSingleName().'s';
39
    }
40
41
    /**
42
     * Get resource single name.
43
     *
44
     * @return string
45
     */
46
    public function getResourceSingleName()
47
    {
48
        $parts = explode('\\', $this->model);
49
50
        return strtolower($parts[sizeof($parts)-1]);
51
    }
52
53
    /**
54
     * Yield resource listing.
55
     *
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function index()
59
    {
60
        $resource = $this->model::all();
61
62
63
        return $this->response->array([
64
            'status' => 'success',
65
            'message' => '',
66
            'data' => [
67
                $this->getResourceName() => $resource,
68
            ],
69
        ]);
70
    }
71
72
    /**
73
     * Yield ID specified resource.
74
     *
75
     * @param int $id
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function show($id)
79
    {
80
        $resource = $this->model::findOrFail($id);
81
82
        return $this->response->array([
83
            'status' => 'success',
84
            'message' => '',
85
            'data' => [
86
                $this->getResourceSingleName() => $resource,
87
            ],
88
        ]);
89
    }
90
91
    /**
92
     * Create a new resource.
93
     *
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function store()
97
    {
98
        $validator = $this->validator($this->requests);
0 ignored issues
show
Bug introduced by
The method validator() does not exist on App\Http\Controllers\Api\V1\BaseController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        /** @scrutinizer ignore-call */ 
99
        $validator = $this->validator($this->requests);
Loading history...
99
        if ($validator->fails()) {
100
            throw new \Dingo\Api\Exception\StoreResourceFailedException(
101
                'Could not create new resource.',
102
                $validator->errors()
103
            );
104
        }
105
106
        $resource = $this->model::create($this->requests->toArray());
107
108
        return $this->response->array([
109
            'status' => 'success',
110
            'message' => '',
111
            'data' => [
112
                $this->getResourceSingleName() => $resource,
113
            ],
114
        ]);
115
    }
116
117
    /**
118
     * Update a resource.
119
     *
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function update($id)
123
    {
124
        $resource = $this->model::findOrFail($id);
125
        $resource->update($this->requests->toArray());
126
127
        return $this->response->array([
128
            'status' => 'success',
129
            'message' => '',
130
            'data' => [
131
                $this->getResourceSingleName() => $resource,
132
            ],
133
        ]);
134
    }
135
136
    /**
137
     * Delete a resource.
138
     *
139
     * @return \Illuminate\Http\Response
140
     */
141
    public function delete($id)
142
    {
143
        $resource = $this->model::findOrFail($id);
144
        $resource->delete();
145
146
        return $this->response->array([
147
            'status' => 'success',
148
            'message' => 'Resource deleted.',
149
        ]);
150
    }
151
}
152