FlexibleUpdatingAttribute::flexibleSetAttribute()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 3
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Nova\Fields;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Storage;
7
use Laravel\Nova\Fields\Field;
8
use Laravel\Nova\Http\Requests\NovaRequest;
9
use NovaFlexibleContent\Flexible;
10
use NovaFlexibleContent\Http\FlexibleAttribute;
11
12
trait FlexibleUpdatingAttribute
13
{
14
15 8
    protected function defaultPreviewCallback(): \Closure
16
    {
17 8
        return function ($value, ?string $disk, $model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
        return function ($value, ?string $disk, /** @scrutinizer ignore-unused */ $model) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18 6
            return $value ? Storage::disk($disk)->url($value) : null;
19 8
        };
20
    }
21
22 9
    protected function defaultDownloadCallback(): \Closure
23
    {
24 9
        return function (NovaRequest $request, Model $model, ?string $disk, $value) {
25 1
            return $value ? Storage::disk($disk)->download($value) : null;
26 9
        };
27
    }
28
29 9
    protected function defaultDeleteCallback(): \Closure
30
    {
31 9
        return function (NovaRequest $request, $model, ?string $disk, $value) {
32 1
            if ($model instanceof Model) {
33 1
                $this->flexibleSetAttribute($request, $model, null);
34
35 1
                if ($request->isMethod('DELETE')) {
36
                    // Prevent trafficCop error.
37 1
                    $model->timestamps = false;
38
                }
39
            }
40
41 1
            if ($value) {
42 1
                Storage::disk($disk)->delete($value);
43
            }
44
45 1
            return true;
46 9
        };
47
    }
48
49
    /**
50
     * Currently this is bad bad bad solution.
51
     * But currently need think about deadline.
52
     * TODO: refactor and find other clever solution.
53
     *
54
     * @param NovaRequest $request
55
     * @param Model       $model
56
     * @param mixed|null  $newValue
57
     * @return Model
58
     */
59 1
    protected function flexibleSetAttribute(NovaRequest $request, Model $model, mixed $newValue = null): Model
60
    {
61 1
        if (str_contains($request->route('field'), FlexibleAttribute::GROUP_SEPARATOR)) {
0 ignored issues
show
Bug introduced by
It seems like $request->route('field') can also be of type Illuminate\Routing\Route and null and object; however, parameter $haystack of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        if (str_contains(/** @scrutinizer ignore-type */ $request->route('field'), FlexibleAttribute::GROUP_SEPARATOR)) {
Loading history...
62 1
            [$groupKey, $fieldKey] = explode(FlexibleAttribute::GROUP_SEPARATOR, $request->field, 2);
63
64
            /** @var Flexible $field */
65 1
            $request->findResourceOrFail()
66 1
                    ->availableFields($request)
67 1
                    ->each(function (Field $field) use ($model, $groupKey, $fieldKey, $newValue) {
68 1
                        if ($field instanceof Flexible) {
69
                            /** @var Flexible $field */
70 1
                            $field->resolve($model);
71 1
                            if ($field->setAttributeRecursive($groupKey, $fieldKey, $newValue)) {
72 1
                                $field->reFillValue($model);
73
74
                                // Break loop
75 1
                                return false;
76
                            }
77
                        }
78 1
                    });
79
        }
80
81 1
        return $model;
82
    }
83
}
84