Test Failed
Push — main ( 706477...92dbda )
by Yaroslav
03:43
created

defaultDownloadCallback()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
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 5
    protected function defaultPreviewCallback(): \Closure
16
    {
17 5
        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 3
            return $value ? Storage::disk($disk)->url($value) : null;
19 5
        };
20
    }
21
22 7
    protected function defaultDownloadCallback(): \Closure
23
    {
24 7
        return function (NovaRequest $request, Model $model, ?string $disk, $value) {
25
            return $value ? Storage::disk($disk)->download($value) : null;
26 7
        };
27
    }
28
29 7
    protected function defaultDeleteCallback(): \Closure
30
    {
31 7
        return function (NovaRequest $request, $model, ?string $disk, $value) {
32
            if ($model instanceof Model) {
33
                $this->flexibleSetAttribute($request, $model, null);
34
35
                if ($request->isMethod('DELETE')) {
36
                    // Prevent trafficCop error.
37
                    $model->timestamps = false;
38
                }
39
            }
40
41
            if ($value) {
42
                Storage::disk($disk)->delete($value);
43
            }
44
45
            return true;
46 7
        };
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
    protected function flexibleSetAttribute(NovaRequest $request, Model $model, mixed $newValue = null): Model
60
    {
61
        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
            [$groupKey, $fieldKey] = explode(FlexibleAttribute::GROUP_SEPARATOR, $request->field, 2);
63
64
            /** @var Flexible $field */
65
            $request->findResourceOrFail()
66
                    ->availableFields($request)
67
                    ->each(function (Field $field) use ($model, $groupKey, $fieldKey, $newValue) {
68
                        if ($field instanceof Flexible) {
69
                            /** @var Flexible $field */
70
                            $field->resolve($model);
71
                            if ($field->setAttributeRecursive($groupKey, $fieldKey, $newValue)) {
72
                                $field->reFillValue($model);
73
74
                                // Break loop
75
                                return false;
76
                            }
77
                        }
78
                    });
79
        }
80
81
        return $model;
82
    }
83
}
84