Passed
Push — main ( 3a0b15...2e9909 )
by Yaroslav
16:35
created

CustomFileExports::getDefaultExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
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\ActionRequest;
14
use Laravel\Nova\Http\Requests\NovaRequest;
15
use Maatwebsite\LaravelNovaExcel\Concerns\WithDisk;
16
use Maatwebsite\LaravelNovaExcel\Concerns\WithFilename;
17
use Maatwebsite\LaravelNovaExcel\Concerns\WithWriterType;
18
use Maatwebsite\LaravelNovaExcel\Interactions\AskForFilename;
19
use Maatwebsite\LaravelNovaExcel\Interactions\AskForWriterType;
20
use NovaExportConfiguration\Export\CustomExport;
21
use NovaExportConfiguration\Models\ExportStoredFile;
22
use NovaExportConfiguration\NovaExportConfig;
23
24
class CustomFileExports extends Action
25
{
26
    use InteractsWithQueue, Queueable;
27
    use AskForFilename,
28
        AskForWriterType,
29
        WithDisk,
30
        WithFilename,
31
        WithWriterType,
32
        WithQueue;
33
34
    public $standalone = true;
35
36
    public $showOnIndex = true;
37
38
    public $showInline = false;
39
40
    public $showOnDetail = false;
41
42
    protected $actionFields = [];
43
44
    protected array $exportsList = [];
45
46
    public function __construct(array $exportsList = [])
47
    {
48
        $this->exportsList = $exportsList;
49
    }
50
51
    public function exportsList(array $exportsList = []): static
52
    {
53
        $this->exportsList = $exportsList;
54
55
        return $this;
56
    }
57
58
59
    public function name()
60
    {
61
        return __('Custom Exports');
62
    }
63
64
    public function handleRequest(ActionRequest $request)
65
    {
66
        $this->handleWriterType($request);
67
        $this->handleFilename($request);
68
69
        return parent::handleRequest($request);
70
    }
71
72
    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

72
    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...
73
    {
74
        $exportName = $fields->get('export');
75
76
        if (!$exportName) {
77
            return Action::danger(__('Export not selected.'));
0 ignored issues
show
Bug introduced by
It seems like __('Export not selected.') can also be of type array; however, parameter $name 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

77
            return Action::danger(/** @scrutinizer ignore-type */ __('Export not selected.'));
Loading history...
78
        }
79
80
        /** @var CustomExport $exportable */
81
        $exportable = NovaExportConfig::customExportsByKey($exportName);
82
        if (!$exportable) {
0 ignored issues
show
introduced by
$exportable is of type NovaExportConfiguration\Export\CustomExport, thus it always evaluated to true.
Loading history...
83
            return Action::danger(__('Exportable config not found'));
84
        }
85
86
        $writerType = $fields->get('writer_type');
87
88
        $dbExport = ExportStoredFile::init(
89
            'custom-export',
90
            $this->getDisk() ?: $exportable::diskName(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDisk() ?: $exportable::diskName() can also be of type null; however, parameter $disk of NovaExportConfiguration\...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

90
            /** @scrutinizer ignore-type */ $this->getDisk() ?: $exportable::diskName(),
Loading history...
91
            date('Y/m/d/') . Str::uuid() . '.' . $this->getDefaultExtension(),
92
            $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like $this->getFilename() can also be of type null; however, parameter $name of NovaExportConfiguration\...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

92
            /** @scrutinizer ignore-type */ $this->getFilename(),
Loading history...
93
            function ($file) {
94
                if ($user = Auth::user()) {
95
                    $file->meta->toMorph('author', $user);
96
                }
97
            }
98
        );
99
100
101
        $exportable->useStoreFile($dbExport);
102
103
        if ($queueName = $this->getQueue($exportable::queueName())) {
104
            $exportable->queue(
105
                $dbExport->path,
106
                $dbExport->disk,
107
                $writerType
108
            )->allOnQueue($queueName);
109
110
            return Action::message(__('Request added to queue. Please wait a while to complete it.'));
0 ignored issues
show
Bug introduced by
It seems like __('Request added to que...while to complete it.') 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 */ __('Request added to queue. Please wait a while to complete it.'));
Loading history...
111
        }
112
113
        $exportable->store(
114
            $dbExport->path,
115
            $dbExport->disk,
116
            $writerType
117
        );
118
119
        return Action::message(__('Data exported to file.'));
120
    }
121
122
    public function fields(NovaRequest $request)
123
    {
124
125
        return array_merge([
126
            Select::make('Export', 'export')
127
                ->options($this->exportsList)
128
                ->required()
129
                ->displayUsingLabels()
130
                ->rules(['required']),
131
        ], $this->actionFields);
132
    }
133
134
    protected function getDefaultExtension(): string
135
    {
136
        return $this->getWriterType() ? strtolower($this->getWriterType()) : 'xlsx';
137
    }
138
}
139