Completed
Push — master ( 32bab7...677091 )
by
unknown
09:28
created

StoreController   A

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
            // Inherited from parent controller
22
            $resource = $request->resource();
23
24
            $resource::authorizeToCreate($request);
25
            $resource::validateForCreation($request);
26
27
            $model = DB::transaction(function () use ($request, $resource) {
28
                [$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...
29
                    $request, $resource::newModel()
30
                );
31
32
                if ($request->viaRelationship()) {
33
                    $request->findParentModelOrFail()
34
                        ->{$request->viaRelationship}()
35
                        ->save($model);
36
                } else {
37
                    $model->save();
38
                }
39
40
                ActionEvent::forResourceCreate($request->user(), $model)->save();
41
42
                collect($callbacks)->each->__invoke();
43
44
                return $model;
45
            });
46
47
            // Create base translation
48
            $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...
49
            $baseTranslation = $model->upsertTranslationEntry($currentLocale->id, 0);
50
51
            // Create base model
52
            $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...
53
            foreach ($otherLocales as $otherLocale) {
54
                $otherModel = $resource::newModel();
55
                foreach ($model->getNonTranslatable() as $field) {
56
                    $otherModel->$field = $model->$field;
57
                }
58
                $otherModel->save();
59
                $otherModel->upsertTranslationEntry($otherLocale->id, $baseTranslation->translation_id);
60
            }
61
62
            // Inherited from parent controller
63
            return response()->json([
64
                'id' => $model->getKey(),
65
                'resource' => $model->attributesToArray(),
66
                'redirect' => $resource::redirectAfterCreate($request, $request->newResourceWith($model)),
67
            ], 201);
68
        } else {
69
            return parent::handle($request);
70
        }
71
    }
72
}
73