ConfiguredExportToExcelAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 63
ccs 0
cts 35
cp 0
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A withDefaultFilename() 0 4 1
A handle() 0 41 5
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