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

ConfiguredExport::setFileModelData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace NovaExportConfiguration\Export;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Log;
7
use Maatwebsite\Excel\Concerns\Exportable;
8
use Maatwebsite\Excel\Concerns\FromQuery;
9
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
10
use Maatwebsite\Excel\Concerns\WithEvents;
11
use Maatwebsite\Excel\Concerns\WithHeadings;
12
use Maatwebsite\Excel\Concerns\WithMapping;
13
use Maatwebsite\Excel\Events\AfterSheet;
14
15
abstract class ConfiguredExport implements FromQuery, WithMapping, WithEvents, WithHeadings, WithCustomChunkSize
16
{
17
    use Exportable, HasFileModel, WithNotification;
0 ignored issues
show
introduced by
The trait Maatwebsite\Excel\Concerns\Exportable requires some properties which are not provided by NovaExportConfiguration\Export\ConfiguredExport: $disk, $diskOptions, $fileName, $filePath, $writerType, $headers
Loading history...
introduced by
The trait NovaExportConfiguration\Export\WithNotification requires some properties which are not provided by NovaExportConfiguration\Export\ConfiguredExport: $name, $path, $exists
Loading history...
18
19
    protected ExportQuery $exportQuery;
20
21
    protected int $chunkSize = 500;
22
23
    public function __construct(ExportQuery $exportQuery)
24
    {
25
        $this->exportQuery = $exportQuery;
26
    }
27
28
    abstract public function headings(): array;
29
30
    abstract public function map($row): array;
31
32
    public function query()
33
    {
34
        return $this->exportQuery->query();
35
    }
36
37
    public function setChunkSize(int $chunkSize): static
38
    {
39
        $this->chunkSize = $chunkSize;
40
41
        return $this;
42
    }
43
44
    public function chunkSize(): int
45
    {
46
        return $this->chunkSize;
47
    }
48
49
    public function registerEvents(): array
50
    {
51
        return [
52
            AfterSheet::class => function (AfterSheet $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event 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

52
            AfterSheet::class => function (/** @scrutinizer ignore-unused */ AfterSheet $event) {

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...
53
                try {
54
                    $this->exportQuery->getConfigurationModel()->update([
55
                        'last_export_at' => Carbon::now(),
56
                    ]);
57
                } catch (\Exception $e) {
58
                    Log::error($e->getMessage());
59
                }
60
61
                $fileModel = $this->saveFileFromModel();
62
                if($fileModel?->exists) {
63
                    $this->notifyUser($fileModel);
64
                }
65
            },
66
        ];
67
    }
68
}
69