replaceFieldValuesWhenOnResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
4
namespace NovaResourceDynamicExport\Nova\Actions;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use Laravel\Nova\Actions\Action;
10
use Laravel\Nova\Fields\BooleanGroup;
11
use Laravel\Nova\Http\Requests\ActionRequest;
12
use Laravel\Nova\Nova;
13
use Maatwebsite\Excel\Facades\Excel;
14
use Maatwebsite\LaravelNovaExcel\Actions\ExportToExcel;
15
use Maatwebsite\LaravelNovaExcel\Requests\ExportActionRequest;
16
use NovaResourceDynamicExport\Models\ExportStoredFile;
17
18
class ExportResourceAction extends ExportToExcel
19
{
20
    protected bool $columnsSelected = false;
21
    protected array $columns;
22
23
    protected ?\Closure $postReplaceFieldValuesWhenOnResource = null;
24
25 5
    public function askForColumns(array $options, string $label = null, callable $callback = null): static
26
    {
27 5
        $this->columns = collect($options)
0 ignored issues
show
Bug introduced by
$options of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

27
        $this->columns = collect(/** @scrutinizer ignore-type */ $options)
Loading history...
28 5
            ->mapWithKeys(function ($value, $key) {
29 5
                if (is_integer($key)) {
30 5
                    return [$value => Nova::humanize(Str::camel($value))];
31
                }
32
33 5
                return [$key => $value];
34 5
            })
35 5
            ->all();
36
37 5
        $field = BooleanGroup::make(
38 5
            __($label ?: 'Columns'),
39 5
            'columns'
40 5
        )
41 5
            ->options($this->columns)
0 ignored issues
show
Bug introduced by
$this->columns of type array is incompatible with the type Closure expected by parameter $options of Laravel\Nova\Fields\BooleanGroup::options(). ( Ignorable by Annotation )

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

41
            ->options(/** @scrutinizer ignore-type */ $this->columns)
Loading history...
42 5
            ->default(function () {
43 1
                return array_combine(
44 1
                    array_keys($this->columns),
45 1
                    array_fill(0, count($this->columns), true)
46 1
                );
47 5
            });
48
49 5
        if (is_callable($callback)) {
50 5
            $callback($field);
51
        }
52
53 5
        $this->actionFields[] = $field;
54
55 5
        return $this;
56
    }
57
58 4
    public function handleRequest(ActionRequest $request)
59
    {
60 4
        $this->handleColumns($request);
61
62 4
        return parent::handleRequest($request);
63
    }
64
65 4
    public function handle(ActionRequest $request, Action $exportable): mixed
66
    {
67 4
        $dbExport = ExportStoredFile::init(
68 4
            $request->resource()::uriKey(),
69 4
            $this->getDisk() ?: config('nova-resource-dynamic-export.defaults.disk'),
70 4
            date('Y/m/d/') . Str::uuid() . '.' . $this->getDefaultExtension(),
71 4
            $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like $this->getFilename() can also be of type null; however, parameter $name of NovaResourceDynamicExpor...xportStoredFile::init() 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

71
            /** @scrutinizer ignore-type */ $this->getFilename(),
Loading history...
72 4
            function ($file) use ($request) {
73 4
                $file->meta
74 4
                    ->setAttribute('fields', $request->resolveFields())
75 4
                    ->setAttribute('filters', $request->filters);
76 4
                if ($user = $request->user()) {
77 4
                    $file->meta->toMorph('author', $user);
78
                }
79 4
            }
80 4
        );
81
82
        try {
83 4
            $response = Excel::store(
84 4
                $exportable,
85 4
                $dbExport->path,
86 4
                $dbExport->disk,
87 4
                $this->getWriterType()
88 4
            );
89 1
        } catch (\Exception $e) {
90 1
            $response = $e->getMessage();
91
        }
92
93 4
        if (true !== $response) {
94 1
            return \is_callable($this->onFailure)
95 1
                ? ($this->onFailure)($request, $response)
96 1
                : Action::danger($response ?: __('Resource could not be exported.'));
97
        }
98
99 3
        $dbExport->save();
100
101 3
        return \is_callable($this->onSuccess)
102 1
            ? ($this->onSuccess)($request, $response)
103 3
            : Action::message(__('Data exported to file.'));
104
    }
105
106 4
    protected function handleColumns(ActionRequest $request): void
107
    {
108 4
        $fields = $request->resolveFields();
109
110 4
        if ($columns = $fields->get('columns')) {
111 1
            $activeColumns = array_keys(array_filter($columns));
0 ignored issues
show
Bug introduced by
$columns of type Illuminate\Support\TGetD...luminate\Support\TValue is incompatible with the type array expected by parameter $array of array_filter(). ( Ignorable by Annotation )

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

111
            $activeColumns = array_keys(array_filter(/** @scrutinizer ignore-type */ $columns));
Loading history...
112 1
            if (count($activeColumns) > 0) {
113 1
                $this->columnsSelected = true;
114 1
                $this->only            = $activeColumns;
115
            }
116
        }
117
    }
118
119 4
    protected function handleHeadings($query, ExportActionRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

119
    protected function handleHeadings($query, /** @scrutinizer ignore-unused */ ExportActionRequest $request)

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...
Unused Code introduced by
The parameter $query 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

119
    protected function handleHeadings(/** @scrutinizer ignore-unused */ $query, ExportActionRequest $request)

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...
120
    {
121 4
        if ($this->columnsSelected) {
122 1
            $this->headings = Arr::only($this->columns, $this->only);
123
        } else {
124 3
            $this->headings = collect($this->only)
0 ignored issues
show
Bug introduced by
$this->only of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

124
            $this->headings = collect(/** @scrutinizer ignore-type */ $this->only)
Loading history...
125 3
                ->map(function ($item) {
126 3
                    return Str::title($item);
127 3
                })
128 3
                ->all();
129
        }
130
    }
131
132 5
    public function setPostReplaceFieldValuesWhenOnResource(?\Closure $closure): static
133
    {
134 5
        $this->postReplaceFieldValuesWhenOnResource = $closure;
135
136 5
        return $this;
137
    }
138
139
140 4
    protected function replaceFieldValuesWhenOnResource(Model $model, array $only = []): array
141
    {
142 4
        $row = parent::replaceFieldValuesWhenOnResource($model, $only);
143
144 4
        if (is_callable($this->postReplaceFieldValuesWhenOnResource)) {
145 2
            $row = call_user_func_array(
146 2
                $this->postReplaceFieldValuesWhenOnResource,
0 ignored issues
show
Bug introduced by
It seems like $this->postReplaceFieldValuesWhenOnResource can also be of type null; however, parameter $callback of call_user_func_array() does only seem to accept callable, 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

146
                /** @scrutinizer ignore-type */ $this->postReplaceFieldValuesWhenOnResource,
Loading history...
147 2
                [$row, $model, $only,]
148 2
            );
149
        }
150
151 4
        return $row;
152
    }
153
}
154