StoreController::handle()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.6925
c 0
b 0
f 0
cc 5
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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