withDefaultFilename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace NovaExportConfiguration\Nova\Actions;
4
5
use Illuminate\Support\Str;
6
use Laravel\Nova\Actions\Action;
7
use Laravel\Nova\Http\Requests\ActionRequest;
8
use Maatwebsite\LaravelNovaExcel\Actions\ExportToExcel;
9
use NovaExportConfiguration\NovaExportConfig;
10
use NovaResourceDynamicExport\Models\ExportStoredFile;
11
12
class ConfiguredExportToExcelAction extends ExportToExcel
13
{
14
    use WithQueue;
15
16
    public $showOnIndex = false;
17
18
    public $showInline = true;
19
20
    public $showOnDetail = true;
21
22
    public function name(): string
23
    {
24
        return __('Export Config');
25
    }
26
27
    protected function withDefaultFilename(ActionRequest $request): void
28
    {
29
        $model = $request->findModelOrFail($request->resources);
30
        $this->withFilename(Str::kebab($model->name) . date('-Ymd') . '.' . $this->getDefaultExtension());
31
    }
32
33
34
    public function handle(ActionRequest $request, Action $exportable)
35
    {
36
        /** @var \NovaExportConfiguration\Models\ExportConfig $model */
37
        $model = $request->findModelOrFail($request->resources);
38
39
        $repo = NovaExportConfig::getRepositories()->getByName($model->type);
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on NovaExportConfiguration\Models\ExportConfig. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
40
        if (!$repo) {
41
            return Action::danger(__('Export repository not found.'));
42
        }
43
44
        $dbExport = ExportStoredFile::init(
45
            $request->resource()::uriKey(),
46
            $this->getDisk() ?: $repo->disk(),
47
            date('Y/m/d/') . Str::uuid() . '.' . $this->getDefaultExtension(),
48
            $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

48
            /** @scrutinizer ignore-type */ $this->getFilename(),
Loading history...
49
            function (ExportStoredFile $model) use ($request) {
50
                $model->meta->setAttribute('fields', $request->resolveFields());
51
                $model->meta->setAttribute('filters', $request->filters);
52
                if ($user = $request->user()) {
53
                    $model->meta->toMorph('author', $user);
54
                }
55
            }
56
        );
57
58
        $export = $repo->export($model, $dbExport);
59
60
        if ($queueName = $this->getQueue($repo->queue())) {
61
            $export->queue(
62
                $dbExport->path,
63
                $dbExport->disk,
64
                $this->getWriterType()
65
            )->allOnQueue($queueName);
66
        } else {
67
            $export->store(
68
                $dbExport->path,
69
                $dbExport->disk,
70
                $this->getWriterType()
71
            );
72
        }
73
74
        return Action::message(__('Export started.'));
75
    }
76
}
77
78
79
// Global ESG Equity Market Neutral strategy with a focus on Europe.
80
// Impact with less volatility and market factors.
81
//Working across renewables, waste, water, circular economy, clean transportation and Argriculture.
82