Test Failed
Branch develop (5056e3)
by Abhishek Kumar
05:17
created

ExportJob   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 43 2
A __construct() 0 3 1
1
<?php
2
3
namespace Ladybirdweb\ImportExport\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\SerializesModels;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Carbon\Carbon;
11
use Ladybirdweb\ImportExport\Models\Export;
12
use Maatwebsite\Excel\Facades\Excel;
13
14
class ExportJob implements ShouldQueue
15
{
16
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Ladybirdweb\ImportExport\Jobs\ExportJob: $id, $class, $relations
Loading history...
17
18
    public $tries = 1;
19
20
    protected $export;
21
22
    /**
23
     * Create a new job instance.
24
     *
25
     * @return void
26
     */
27
    public function __construct(Export $export)
28
    {
29
        $this->export = $export;
30
    }
31
32
    /**
33
     * Execute the job.
34
     *
35
     * @return void
36
     */
37
    public function handle()
38
    {
39
        // Custom file path
40
        $file_path = date( 'Y/m' );
41
42
        // Export instance assign to variable
43
        $export = $this->export;
44
45
        // Store total row count
46
        $export->result_rows = $export->query->count();
47
        $export->save();
48
49
        // Create export file
50
        $excel = Excel::create( 'export-' . date( 'dmYhis' ), function($excel) use ($export) {
51
52
            // Create new sheet
53
            $excel->sheet( 'export', function($sheet) use ($export) {
54
55
                // Retrive data in chunk
56
                $export->query->chunk( 10, function($data) use ($export, $sheet) {
57
58
                    // Process chunk data
59
                    foreach ( $data as $row ) {
60
                        
61
                        // Append row to sheet
62
                        $sheet->appendRow( $row->toArray() );
63
64
                    }
65
66
                    // Store processed row count
67
                    $export->row_processed += 10;
68
                    $export->save();
69
70
                } );
71
72
            });
73
74
        })->store( $this->export->type, storage_path( 'app/exports/' . $file_path ), true );
75
76
        // Update export data
77
        $export->file = $file_path . '/' . $excel['file'];
78
        $export->completed_at = Carbon::now();
79
        $export->save();
80
81
    }
82
}
83