Completed
Push — master ( ebfb32...4f44f9 )
by
unknown
09:51
created

DestroyController::deleteTranslatableModelFields()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Controllers\TranslatableResource;
4
5
use BBSLab\NovaTranslation\Models\Translation;
6
use BBSLab\NovaTranslation\Resources\TranslatableResource;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\DB;
9
use Laravel\Nova\Actions\Actionable;
10
use Laravel\Nova\Actions\ActionEvent;
11
use Laravel\Nova\Contracts\Deletable;
12
use Laravel\Nova\DeleteField;
13
use Laravel\Nova\Http\Controllers\DeletesFields;
14
use Laravel\Nova\Http\Controllers\ResourceDestroyController;
15
use Laravel\Nova\Http\Requests\DeleteResourceRequest;
16
use Laravel\Nova\Http\Requests\NovaRequest;
17
18
class DestroyController extends ResourceDestroyController
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function handle(DeleteResourceRequest $request)
24
    {
25
        $resource = $request->resource();
26
27
        if (is_subclass_of($resource, TranslatableResource::class)) {
28
            $request->chunks(150, function ($models) use ($request) {
29
                $models->each(function ($model) use ($request) {
30
                    $translatableIds = 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...
31
                        ->select('translatable_id')
32
                        ->where('translatable_type', '=', get_class($model))
33
                        ->where('translation_id', '=', $model->translation->translation_id)
34
                        ->get()
35
                        ->pluck('translatable_id')
36
                        ->toArray();
37
38
                    $translatedModels = $model->query()->whereIn($model->getKeyName(), $translatableIds)->get();
39
                    foreach ($translatedModels as $translatedModel) {
40
                        $this->deleteSingleTranslatableModel($request, $translatedModel);
41
                    }
42
                });
43
            });
44
        } else {
45
            return parent::handle($request);
46
        }
47
    }
48
49
    /**
50
     * Delete a single model.
51
     *
52
     * @param  \Laravel\Nova\Http\Requests\DeleteResourceRequest  $request
53
     * @param  \Illuminate\Database\Eloquent\Model  $model
54
     * @return void
55
     * @throws \Exception
56
     */
57
    protected function deleteSingleTranslatableModel(DeleteResourceRequest $request, Model $model)
58
    {
59
        $this->deleteTranslatableModelFields($request, $model);
60
61
        if (in_array(Actionable::class, class_uses_recursive($model))) {
62
            $model->actions()->delete();
63
        }
64
65
        $model->delete();
66
67
        DB::table('action_events')->insert(
68
            ActionEvent::forResourceDelete($request->user(), collect([$model]))->map->getAttributes()->all()
69
        );
70
    }
71
72
    /**
73
     * Delete the deletable fields on the given model / resource.
74
     *
75
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
76
     * @param $model
77
     * @param  bool  $skipSoftDeletes
78
     * @return void
79
     */
80
    protected function deleteTranslatableModelFields(NovaRequest $request, $model, $skipSoftDeletes = true)
81
    {
82
        if ($skipSoftDeletes && $request->newResourceWith($model)->softDeletes()) {
83
            return;
84
        }
85
86
        // @TODO... Filter throught detailFields
87
        if (false) {
88
            $request->newResourceWith($model)
89
                ->detailFields($request)
90
                ->whereInstanceOf(Deletable::class)
91
                ->filter->isPrunable()
92
                ->each(function ($field) use ($request, $model) {
93
                    DeleteField::forRequest($request, $field, $model);
94
                });
95
        }
96
    }
97
}
98