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

CustomFileExports::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace NovaExportConfiguration\Nova\Actions;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Queue\InteractsWithQueue;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Str;
10
use Laravel\Nova\Actions\Action;
11
use Laravel\Nova\Fields\ActionFields;
12
use Laravel\Nova\Fields\Select;
13
use Laravel\Nova\Http\Requests\NovaRequest;
14
use Maatwebsite\Excel\Facades\Excel;
15
use Maatwebsite\LaravelNovaExcel\Concerns\WithDisk;
16
use Maatwebsite\LaravelNovaExcel\Interactions\AskForFilename;
17
use Maatwebsite\LaravelNovaExcel\Interactions\AskForWriterType;
18
use NovaExportConfiguration\Export\CustomExport;
19
use NovaExportConfiguration\Models\ExportStoredFile;
20
use NovaExportConfiguration\NovaExportConfig;
21
22
class CustomFileExports extends Action
23
{
24
    use InteractsWithQueue, Queueable;
25
    use AskForFilename,
26
        AskForWriterType,
27
        WithDisk,
28
        WithQueue;
29
30
    public $standalone = true;
31
32
    public $showOnIndex = true;
33
34
    public $showInline = false;
35
36
    public $showOnDetail = false;
37
38
    protected $actionFields = [];
39
40
    protected array $exportsList = [];
41
42
    public function __construct(array $exportsList = [])
43
    {
44
        $this->exportsList = $exportsList;
45
    }
46
47
    public function exportsList(array $exportsList = []): static
48
    {
49
        $this->exportsList = $exportsList;
50
51
        return $this;
52
    }
53
54
55
    public function name()
56
    {
57
        return __('Custom Exports');
58
    }
59
60
    public function handle(ActionFields $fields, Collection $models)
0 ignored issues
show
Unused Code introduced by
The parameter $models 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

60
    public function handle(ActionFields $fields, /** @scrutinizer ignore-unused */ Collection $models)

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...
61
    {
62
        /** @var CustomExport $exportable */
63
        $exportable = NovaExportConfig::customExportsByKey($fields->get('export'));
64
        if (!$exportable) {
0 ignored issues
show
introduced by
$exportable is of type NovaExportConfiguration\Export\CustomExport, thus it always evaluated to true.
Loading history...
65
            return Action::danger(__('Exportable config not found'));
66
        }
67
68
        $writerType = $fields->get('writer_type');
69
        $type       = 'custom-export';
70
        $name       = $fields->get('filename', $exportable::name()) . '.' . Str::lower($writerType ?: 'xlsx');
71
        $filename   = date('Y/m/d/') . Str::uuid() . '.' . Str::lower($writerType ?: 'xlsx');
72
        $disk       = $this->getDisk()?: $exportable::diskName();
73
74
        $response = Excel::store(
75
            $exportable,
76
            $filename,
77
            $disk,
78
            $writerType
79
        );
80
81
        if (false === $response) {
82
            return Action::danger(__('Resource could not be exported.'));
0 ignored issues
show
Bug introduced by
It seems like __('Resource could not be exported.') 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

82
            return Action::danger(/** @scrutinizer ignore-type */ __('Resource could not be exported.'));
Loading history...
83
        }
84
85
        $dbExport       = new ExportStoredFile();
86
        $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...
87
        $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...
88
        $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...
89
        $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...
90
        if ($user = Auth::user()) {
91
            $dbExport->meta->toMorph('author', $user);
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...
92
        }
93
94
        $exportable->setFileModelData(serialize($dbExport));
95
96
        if ($queueName = $this->getQueue($exportable::queueName())) {
97
            $exportable->queue(
98
                $filename,
99
                $disk,
100
                $writerType
101
            )->allOnQueue($queueName);
102
        } else {
103
            $exportable->store(
104
                $filename,
105
                $disk,
106
                $writerType
107
            );
108
        }
109
110
        return Action::message(__('Data exported to file.'));
0 ignored issues
show
Bug introduced by
It seems like __('Data exported to file.') 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

110
        return Action::message(/** @scrutinizer ignore-type */ __('Data exported to file.'));
Loading history...
111
    }
112
113
    public function fields(NovaRequest $request)
114
    {
115
116
        return array_merge([
117
            Select::make('Export', 'export')
118
                ->options($this->exportsList)
119
                ->required()
120
                ->displayUsingLabels(),
121
        ], $this->actionFields);
122
    }
123
}
124