StoreController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 54 5
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Controllers\TranslatableResource;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use Illuminate\Support\Facades\DB;
7
use Laravel\Nova\Actions\ActionEvent;
8
use Laravel\Nova\Http\Controllers\ResourceStoreController;
9
use Laravel\Nova\Http\Requests\CreateResourceRequest;
10
11
class StoreController extends ResourceStoreController
12
{
13
    use Traits\TranslatableController;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function handle(CreateResourceRequest $request)
19
    {
20
        if (! $this->isTranslatableResource($request)) {
21
            return parent::handle($request);
22
        }
23
24
        // Inherited from parent controller
25
        $resource = $request->resource();
26
27
        $resource::authorizeToCreate($request);
28
        $resource::validateForCreation($request);
29
30
        $model = DB::transaction(function () use ($request, $resource) {
31
            [$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...
32
                $request, $resource::newModel()
33
            );
34
35
            if ($request->viaRelationship()) {
36
                $request->findParentModelOrFail()
37
                    ->{$request->viaRelationship}()
38
                    ->save($model);
39
            } else {
40
                $model->save();
41
            }
42
43
            ActionEvent::forResourceCreate($request->user(), $model)->save();
44
45
            collect($callbacks)->each->__invoke();
46
47
            return $model;
48
        });
49
50
        // Create base translation
51
        $currentLocale = Locale::query()->select('id')->where('iso', '=', app()->getLocale())->first();
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...
52
        $baseTranslation = $model->upsertTranslationEntry($currentLocale->id, 0);
53
54
        // Create base model
55
        $otherLocales = Locale::query()->select('id')->where('id', '!=', $currentLocale->id)->get();
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...
56
        foreach ($otherLocales as $otherLocale) {
57
            $otherModel = $resource::newModel();
58
            foreach ($model->getOnCreateTranslatable() as $field) {
59
                $otherModel->$field = $model->$field;
60
            }
61
            $otherModel->save();
62
            $otherModel->upsertTranslationEntry($otherLocale->id, $baseTranslation->translation_id);
63
        }
64
65
        // Inherited from parent controller
66
        return response()->json([
67
            'id' => $model->getKey(),
68
            'resource' => $model->attributesToArray(),
69
            'redirect' => $resource::redirectAfterCreate($request, $request->newResourceWith($model)),
70
        ], 201);
71
    }
72
}
73