UpdateController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 50 5
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Controllers\TranslatableResource;
4
5
use BBSLab\NovaTranslation\Models\Translation;
6
use Illuminate\Support\Facades\DB;
7
use Laravel\Nova\Actions\ActionEvent;
8
use Laravel\Nova\Http\Controllers\ResourceUpdateController;
9
use Laravel\Nova\Http\Requests\UpdateResourceRequest;
10
11
class UpdateController extends ResourceUpdateController
12
{
13
    use Traits\TranslatableController;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function handle(UpdateResourceRequest $request)
19
    {
20
        if (! $this->isTranslatableResource($request)) {
21
            return parent::handle($request);
22
        }
23
24
        // Inherited from parent controller
25
        [$model, $resource] = DB::transaction(function () use ($request) {
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 $resource 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...
26
            $model = $request->findModelQuery()->lockForUpdate()->firstOrFail();
27
28
            $resource = $request->newResourceWith($model);
29
            $resource->authorizeToUpdate($request);
30
            $resource::validateForUpdate($request);
31
32
            if ($this->modelHasBeenUpdatedSinceRetrieval($request, $model)) {
33
                return response('', 409)->throwResponse();
34
            }
35
36
            [$model, $callbacks] = $resource::fillForUpdate($request, $model);
0 ignored issues
show
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...
37
38
            ActionEvent::forResourceUpdate($request->user(), $model)->save();
39
40
            $model->save();
41
42
            collect($callbacks)->each->__invoke();
43
44
            return [$model, $resource];
45
        });
46
47
        // Update translations nonTranslatable fields
48
        $translations = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
            ->select('translatable_id')
50
            ->where('translation_id', '=', $model->translation->translation_id)
51
            ->where('translatable_type', '=', get_class($model))
52
            ->get();
53
        foreach ($translations as $translation) {
54
            $translatedModel = $resource::newModel()->find($translation->translatable_id);
55
            foreach ($model->getNonTranslatable() as $field) {
56
                $translatedModel->$field = $model->$field;
57
            }
58
            $translatedModel->save();
59
        }
60
61
        // Inherited from parent controller
62
        return response()->json([
63
            'id' => $model->getKey(),
64
            'resource' => $model->attributesToArray(),
65
            'redirect' => $resource::redirectAfterUpdate($request, $resource),
66
        ]);
67
    }
68
}
69