BaseController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 136
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getResourceSingleName() 0 5 1
A getResourceName() 0 3 1
A store() 0 17 2
A index() 0 9 1
A update() 0 10 1
A show() 0 9 1
A delete() 0 8 1
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
        return $this->response->array([
63
            'status' => 'success',
64
            'message' => '',
65
            'data' => [
66
                $this->getResourceName() => $resource,
67
            ],
68
        ]);
69
    }
70
71
    /**
72
     * Yield ID specified resource.
73
     *
74
     * @param int $id
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function show($id)
78
    {
79
        $resource = $this->model::findOrFail($id);
80
81
        return $this->response->array([
82
            'status' => 'success',
83
            'message' => '',
84
            'data' => [
85
                $this->getResourceSingleName() => $resource,
86
            ],
87
        ]);
88
    }
89
90
    /**
91
     * Create a new resource.
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function store()
96
    {
97
        $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

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