Completed
Push — master ( 5cd3ef...ae45f7 )
by
unknown
17:04 queued 07:03
created

StoreController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 34 2
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Controllers\TranslatableResource;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\DB;
7
use Laravel\Nova\Actions\ActionEvent;
8
use Laravel\Nova\Http\Requests\CreateResourceRequest;
9
10
class StoreController extends ResourceStoreController
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function handle(CreateResourceRequest $request)
16
    {
17
        $resource = $request->resource();
18
19
        $resource::authorizeToCreate($request);
20
21
        $resource::validateForCreation($request);
22
23
        $model = DB::transaction(function () use ($request, $resource) {
24
            [$model, $callbacks] = $resource::fill(
0 ignored issues
show
Bug introduced by
The variable $model does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $callbacks does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
25
                $request, $resource::newModel()
26
            );
27
28
            if ($request->viaRelationship()) {
29
                $request->findParentModelOrFail()
30
                    ->{$request->viaRelationship}()
31
                    ->save($model);
32
            } else {
33
                $model->save();
34
            }
35
36
            ActionEvent::forResourceCreate($request->user(), $model)->save();
37
38
            collect($callbacks)->each->__invoke();
39
40
            return $model;
41
        });
42
43
        return response()->json([
44
            'id' => $model->getKey(),
45
            'resource' => $model->attributesToArray(),
46
            'redirect' => $resource::redirectAfterCreate($request, $request->newResourceWith($model)),
47
        ], 201);
48
    }
49
}
50