Test Failed
Push — master ( 5904eb...0e6af8 )
by Phaniraj
05:25
created

Export   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A returnExportProgress() 0 19 3
A downloadExportedFile() 0 10 3
A export() 0 13 1
1
<?php
2
3
namespace LWS\ImportExport;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use LWS\ImportExport\Jobs\ExportJob;
7
use LWS\ImportExport\Models\Export as ModelExport;
8
9
class Export
10
{
11
    public function export(Builder $query, $ext = 'xls')
12
    {
13
        // Create new export
14
        $export = new ModelExport;
15
16
        $export->type = $ext;
17
        $export->query = $query->getModel();
18
19
        $export->save();
20
21
        ExportJob::dispatch($export)->onQueue('exporting');
22
23
        return $export;
24
    }
25
26
    /**
27
     * Return export progress.
28
     *
29
     * @param  int  $id
30
     * @return \Illuminate\Http\Response json
31
     */
32
    public function returnExportProgress($id)
33
    {
34
        // Map export instance
35
        $export = ModelExport::findOrFail($id);
36
37
        $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...
38
39
        if ($export->result_rows > 0) {
40
            $data['progress'] = round(($export->row_processed / $export->result_rows) * 100);
41
        } else {
42
            $data['progress'] = 0;
43
        }
44
45
        // If progress completed return successful imported rows count
46
        if ($data['progress'] == 100) {
47
            $data['exported'] = route('ladybirdweb.export.download', $export->id);
48
        }
49
50
        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...
51
    }
52
53
    public function downloadExportedFile($id)
54
    {
55
        // Map export instance
56
        $export = ModelExport::findOrFail($id);
57
58
        if (! file_exists(storage_path('app/exports').'/'.$export->file) || empty($export->file)) {
59
            abort(404);
60
        }
61
62
        return response()->download(storage_path('app/exports/'.$export->file));
63
    }
64
}
65