Issues (40)

src/ApiResourceController.php (3 issues)

1
<?php
2
3
namespace RafflesArgentina\ResourceController;
4
5
use DB;
6
7
use Illuminate\Http\Request;
8
9
use RafflesArgentina\ResourceController\Traits\WorksWithRelations;
10
use RafflesArgentina\ResourceController\Traits\WorksWithFileUploads;
11
use RafflesArgentina\ResourceController\Traits\FormatsResponseMessages;
12
use RafflesArgentina\ResourceController\Exceptions\ResourceControllerException;
13
14
class ApiResourceController extends AbstractResourceController
15
{
16
    use WorksWithRelations, WorksWithFileUploads, FormatsResponseMessages;
0 ignored issues
show
The trait RafflesArgentina\Resourc...ts\WorksWithFileUploads requires some properties which are not provided by RafflesArgentina\Resourc...r\ApiResourceController: $files, $attributes
Loading history...
The trait RafflesArgentina\Resourc...aits\WorksWithRelations requires some properties which are not provided by RafflesArgentina\Resourc...r\ApiResourceController: $request, $pruneHasOne, $id, $pruneHasMany
Loading history...
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @param Request $request The request object.
22
     *
23
     * @return \Illuminate\Http\JsonResponse
24
     */
25
    public function index(Request $request)
26
    {
27
        $this->getFormRequestInstance();
28
29
        $items = $this->getItemsCollection();
30
31
        return $this->validSuccessJsonResponse('Success', $items);
0 ignored issues
show
$items of type Illuminate\Database\Eloquent\Collection is incompatible with the type array expected by parameter $data of RafflesArgentina\Resourc...idSuccessJsonResponse(). ( Ignorable by Annotation )

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

31
        return $this->validSuccessJsonResponse('Success', /** @scrutinizer ignore-type */ $items);
Loading history...
32
    }
33
34
    /**
35
     * Show the form for creating a new resource.
36
     *
37
     * @param Request $request The request object.
38
     *
39
     * @return \Illuminate\Http\JsonResponse
40
     */
41
    public function create(Request $request)
42
    {
43
        return $this->validNotFoundJsonResponse();
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param Request $request The request object.
50
     *
51
     * @throws ResourceControllerException
52
     *
53
     * @return \Illuminate\Http\JsonResponse
54
     */
55
    public function store(Request $request)
56
    {
57
        $this->getFormRequestInstance();
58
59
        DB::beginTransaction();
60
        try {
61
            $instance = $this->repository->create($request->all());
62
            $model = $instance[1];
63
            $number = $model->{$model->getRouteKeyName()};
64
            $mergedRequest = $this->uploadFiles($request, $model);
65
            $this->updateOrCreateRelations($mergedRequest, $model);
66
        } catch (\Exception $e) {
67
            DB::rollback();
68
69
            $message = $this->storeFailedMessage($e->getMessage());
70
            throw new ResourceControllerException($message);
71
        }
72
73
        DB::commit();
74
75
        $message = $this->storeSuccessfulMessage($number);
76
        $data = [$model];
77
78
        return $this->validSuccessJsonResponse($message, $data);
79
    }
80
81
    /**
82
     * Display the specified resource.
83
     *
84
     * @param Request $request The request object.
85
     * @param string  $key     The model key.
86
     *
87
     * @return \Illuminate\Http\JsonResponse
88
     */
89
    public function show(Request $request, $key)
90
    {
91
        $model = $this->findFirstByKey($key);
92
93
        if (!$model) {
94
            return $this->validNotFoundJsonResponse();
95
        }
96
97
        return response()->json($model, 200, [], JSON_PRETTY_PRINT);
98
    }
99
100
    /**
101
     * Show the form for editing the specified resource.
102
     *
103
     * @param Request $request The request object.
104
     * @param string  $key     The model key.
105
     *
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function edit(Request $request, $key)
109
    {
110
        return $this->validNotFoundJsonResponse();
111
    }
112
113
    /**
114
     * Update the specified resource in storage.
115
     *
116
     * @param Request $request The request object.
117
     * @param string  $key     The model key.
118
     *
119
     * @throws ResourceControllerException
120
     *
121
     * @return \Illuminate\Http\JsonResponse
122
     */
123
    public function update(Request $request, $key)
124
    {
125
        $this->getFormRequestInstance();
126
127
        $model = $this->findFirstByKey($key);
128
129
        if (!$model) {
130
            return $this->validNotFoundJsonResponse();
131
        }
132
133
        DB::beginTransaction();
134
       
135
        try { 
136
            $instance = $this->repository->update($model, $request->all());
137
            $model = $instance[1];
138
            $mergedRequest = $this->uploadFiles($request, $model);
139
            $this->updateOrCreateRelations($mergedRequest, $model);
140
        } catch (\Exception $e) {
141
            DB::rollback();
142
143
            $message = $this->updateFailedMessage($key, $e->getMessage());
144
            throw new ResourceControllerException($message);
145
        }
146
147
        DB::commit();
148
149
        $message = $this->updateSuccessfulMessage($key);
150
        $data = [$model];
151
152
        return $this->validSuccessJsonResponse($message, $data);
153
    }
154
155
    /**
156
     * Remove the specified resource from storage.
157
     *
158
     * @param Request $request The request object.
159
     * @param string  $key     The model key.
160
     *
161
     * @throws ResourceControllerException
162
     *
163
     * @return \Illuminate\Http\JsonResponse
164
     */
165
    public function destroy(Request $request, $key)
166
    {
167
        $this->getFormRequestInstance();
168
169
        $model = $this->findFirstByKey($key);
170
171
        if (!$model) {
172
            return $this->validNotFoundJsonResponse();
173
        }
174
175
        DB::beginTransaction();
176
177
        try {
178
            $this->repository->delete($model);
179
        } catch (\Exception $e) {
180
            DB::rollback();
181
182
            $message = $this->destroyFailedMessage($key, $e->getMessage());
183
            throw new ResourceControllerException($message);
184
        }
185
186
        DB::commit();
187
188
        $message = $this->destroySuccessfulMessage($key);
189
        $data = [$model];
190
191
        return $this->validSuccessJsonResponse($message, $data);
192
    }
193
}
194