Passed
Push — main ( ac25fd...00e2bd )
by Yaroslav
14:14
created

ConfiguredExportToExcelAction::handle()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 34
nc 5
nop 2
dl 0
loc 46
ccs 0
cts 35
cp 0
crap 30
rs 9.0648
c 1
b 0
f 0
1
<?php
2
3
namespace NovaExportConfiguration\Nova\Actions;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Str;
7
use Laravel\Nova\Actions\Action;
8
use Laravel\Nova\Http\Requests\ActionRequest;
9
use NovaExportConfiguration\Models\ExportStoredFile;
10
use NovaExportConfiguration\NovaExportConfig;
11
12
class ConfiguredExportToExcelAction extends \Maatwebsite\LaravelNovaExcel\Actions\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()
23
    {
24
        return __('Export Config');
25
    }
26
27
    protected function withDefaultFilename(ActionRequest $request)
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): array
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.'));
0 ignored issues
show
Bug introduced by
It seems like __('Export repository not found.') can also be of type array; however, parameter $message of Laravel\Nova\Actions\Action::danger() 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

41
            return Action::danger(/** @scrutinizer ignore-type */ __('Export repository not found.'));
Loading history...
42
        }
43
        $resource = $request->resource();
44
        $type     = $resource::uriKey();
45
        $name     = $this->getFilename();
46
        $filename = date('Y/m/d/').Str::uuid().'.'.$this->getDefaultExtension();
47
        $disk     = $this->getDisk() ?: $repo->disk();
48
        $filters  = $request->filters;
49
        $fields   = $request->resolveFields();
50
51
        $dbExport       = new ExportStoredFile();
52
        $dbExport->type = $type;
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. 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...
53
        $dbExport->disk = $disk;
0 ignored issues
show
Bug introduced by
The property disk does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. 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...
54
        $dbExport->path = $filename;
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. 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...
55
        $dbExport->name = $name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. 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...
56
        $dbExport->meta->setAttribute('fields', $fields);
0 ignored issues
show
Bug introduced by
The property meta does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. 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...
57
        $dbExport->meta->setAttribute('filters', $filters);
58
        if ($user = Auth::user()) {
59
            $dbExport->meta->setAttribute('author.type', $user->getMorphClass());
60
            $dbExport->meta->setAttribute('author.id', $user->getKey());
61
        }
62
63
        $export    = $repo->export($model, serialize($dbExport));
64
        if ($queueName = $this->getQueue($repo->queue())) {
65
            $export->queue(
66
                $filename,
67
                $disk,
68
                $this->getWriterType()
69
            )->allOnQueue($queueName);
70
        } else {
71
            $export->store(
72
                $filename,
73
                $disk,
74
                $this->getWriterType()
75
            );
76
        }
77
78
79
        return Action::message(__('Export started.'));
0 ignored issues
show
Bug introduced by
It seems like __('Export started.') can also be of type array; however, parameter $message of Laravel\Nova\Actions\Action::message() 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

79
        return Action::message(/** @scrutinizer ignore-type */ __('Export started.'));
Loading history...
80
    }
81
}
82