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
|
|||||
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
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
![]() |
|||||
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 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.