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

Export   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadExportedFile() 0 10 3
A returnExportProgress() 0 23 3
A export() 0 13 1
A showExportStatus() 0 6 1
1
<?php
2
3
namespace Ladybirdweb\ImportExport;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Ladybirdweb\ImportExport\Jobs\ExportJob;
7
use Ladybirdweb\ImportExport\Models\Export as ModelExport;
8
9
class Export
10
{
11
	
12
	public function export(Builder $query, $ext = 'xls')
13
	{
14
		// Create new export
15
		$export = new ModelExport;
16
17
		$export->type = $ext;
18
		$export->query = $query->getModel();
19
20
		$export->save();
21
22
		ExportJob::dispatch( $export )->onQueue( 'exporting' );
23
24
		return $export;
25
	}
26
27
28
29
	public function showExportStatus($id)
30
    {
31
        // Map export instance
32
        $export = ModelExport::findOrFail( $id );
0 ignored issues
show
Unused Code introduced by
The assignment to $export is dead and can be removed.
Loading history...
33
34
        return view( 'importexport::export.progress', compact( 'id' ) );
35
    }
36
37
38
    /**
39
     * Return export progress
40
     *
41
     * @param  int  $id
42
     * @return \Illuminate\Http\Response json
43
     */
44
    public function returnExportProgress($id)
45
    {
46
        // Map export instance
47
        $export = ModelExport::findOrFail( $id );
48
49
        $data['status'] = 200;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
50
51
        if ( $export->result_rows > 0 ) {
52
53
        	$data['progress'] = round( ( $export->row_processed / $export->result_rows ) * 100 );
54
55
        } else {
56
57
        	$data['progress'] = 0;
58
59
        }
60
61
        // If progress completed return successful imported rows count
62
        if ( $data['progress'] == 100 ) {
63
            $data['exported'] = route( 'ladybirdweb.export.download', $export->id );
64
        }
65
66
        return response()->json( $data );
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($data) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
67
    }
68
69
70
    public function downloadExportedFile($id)
71
    {
72
        // Map export instance
73
        $export = ModelExport::findOrFail( $id );
74
75
        if ( ! file_exists( storage_path( 'app/exports' ) . '/' . $export->file ) || empty( $export->file ) ) {
76
            abort(404);
77
        }
78
79
        return response()->download( storage_path( 'app/exports/' . $export->file ) );
80
    }
81
}
82