CustomFileExports::prepareExportable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 4
crap 2
1
<?php
2
3
namespace NovaResourceDynamicExport\Nova\Actions;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Queue\InteractsWithQueue;
8
use Illuminate\Support\Collection;
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 NovaResourceDynamicExport\CustomResourcesExport;
21
use NovaResourceDynamicExport\Export\CustomExport;
22
use NovaResourceDynamicExport\Models\ExportStoredFile;
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
    protected ?Model $user = null;
47
48 3
    public function __construct(array $exportsList = [])
49
    {
50 3
        $this->exportsList = $exportsList;
51
    }
52
53 3
    public function exportsList(array $exportsList = []): static
54
    {
55 3
        $this->exportsList = $exportsList;
56
57 3
        return $this;
58
    }
59
60
61 2
    public function name()
62
    {
63 2
        return __('Custom Exports');
64
    }
65
66 1
    public function handleRequest(ActionRequest $request)
67
    {
68 1
        $this->user = $request->user();
69
70 1
        $this->handleWriterType($request);
71 1
        $this->handleFilename($request);
72
73 1
        return parent::handleRequest($request);
74
    }
75
76 1
    public function handle(ActionFields $fields, Collection $models)
77
    {
78 1
        $exportName = $fields->get('export');
79
80 1
        if (!$exportName) {
81
            return Action::danger(__('Export not selected.'));
82
        }
83
84
        /** @var CustomExport $exportable */
85 1
        $exportable = CustomResourcesExport::findByKey($exportName);
86 1
        if (!$exportable) {
0 ignored issues
show
introduced by
$exportable is of type NovaResourceDynamicExport\Export\CustomExport, thus it always evaluated to true.
Loading history...
87
            return Action::danger(__('Exportable config not found'));
88
        }
89
90 1
        $writerType = $fields->get('writer_type');
91
92 1
        $dbExport = ExportStoredFile::init(
93 1
            'custom-export',
94 1
            $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 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

94
            /** @scrutinizer ignore-type */ $this->getDisk() ?: $exportable::diskName(),
Loading history...
95 1
            date('Y/m/d/') . Str::uuid() . '.' . $this->getDefaultExtension(),
96 1
            $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

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

145
    protected function prepareExportable(CustomExport $exportable, ExportStoredFile $dbExport, /** @scrutinizer ignore-unused */ ActionFields $fields, Collection $models): void

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...
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

145
    protected function prepareExportable(CustomExport $exportable, ExportStoredFile $dbExport, ActionFields $fields, /** @scrutinizer ignore-unused */ Collection $models): void

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...
146
    {
147 1
        if ($this->user) {
148 1
            $exportable->setNotificationUser($this->user);
149
        }
150
151 1
        $exportable->setDownloadLink('link:' . $dbExport->download_link);
152
    }
153
}
154