Completed
Branch develop (351c2c)
by Phaniraj
05:20
created

Exporter::csv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace LWS\ExportActions\Http\Controllers;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Http\Request;
7
use SoapBox\Formatter\Formatter;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Support\Facades\Storage;
10
use PhpOffice\PhpSpreadsheet\Spreadsheet;
0 ignored issues
show
Bug introduced by
The type PhpOffice\PhpSpreadsheet\Spreadsheet was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
0 ignored issues
show
Bug introduced by
The type PhpOffice\PhpSpreadsheet\Writer\Xlsx was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use LWS\ExportActions\Jobs\WritePdf;
13
use Illuminate\Support\Facades\DB;
14
15
16
class Exporter extends Controller
17
{
18
19
    public function download(Request $request)
20
    {
21
        if(DB::table('job_update')->where('id', $request->id)->exists()) {
22
            $file_download_progress = DB::table('job_update')->where('id',$request->id)->first();
23
            //dd($file_download_progress->file_name);
24
            return response()->download($file_download_progress->file_name);
25
        }
26
            
27
        else {
28
            return response()->json(['message' => 'Something Went Wrong'],500);
29
        }    
30
    }
31
32
    public function progress()
33
    {
34
        $data = DB::table('job_update')->get()->toJson();
35
        return response($data,200);
36
    }
37
38
39
    public function export(Request $requestData)
40
    {
41
        
42
43
        $client = new Client();
44
        
45
46
        $request = new \GuzzleHttp\Psr7\Request('GET', $requestData->url);
47
        $promise = $client->sendAsync($request)->then(function ($responseData) {
48
49
            $formatter = Formatter::make($responseData->getBody(), Formatter::JSON);
50
            $csv   = $formatter->toCsv();
51
52
            Storage::disk('local')->put('csvfile.csv',$csv);
53
54
        });
55
56
        $promise->wait();
57
58
        $file_name = storage_path().'/app/csvfile.csv';
59
        
60
        switch(strtolower($requestData->format)) {
61
            case 'csv': return $this->csv($file_name);break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
62
            case 'excel': return $this->excel($file_name);break;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->excel($file_name) targeting LWS\ExportActions\Http\C...llers\Exporter::excel() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
            case 'pdf' : return $this->pdf($file_name);break;
64
        } //switch
65
        
66
        
67
68
        
69
    }
70
71
72
    public function csv($file_name)
73
    {
74
        $headers = [
75
            'Content-Type' => 'text/csv'
76
         ];
77
        
78
        return response()->download($file_name,"export.csv",$headers);
79
80
    }
81
82
83
    public function excel($file_name)
84
    {
85
        $spreadsheet = new Spreadsheet();
0 ignored issues
show
Unused Code introduced by
The assignment to $spreadsheet is dead and can be removed.
Loading history...
86
        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
0 ignored issues
show
Bug introduced by
The type PhpOffice\PhpSpreadsheet\Reader\Csv was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
        
88
        /* Set CSV parsing options */
89
        $reader->setDelimiter(',');
90
        $reader->setEnclosure('"');
91
        $reader->setSheetIndex(0);
92
        
93
        /* Load a CSV file and save as a XLS */
94
        $spreadsheet = $reader->load($file_name);
95
        $writer = new Xlsx($spreadsheet);
96
        
97
        $writer->save('export.xlsx');
98
        
99
        $spreadsheet->disconnectWorksheets();
100
        unset($spreadsheet);
101
    }
102
103
    public function pdf($file_name)
104
    {
105
106
        $csv = array_map('str_getcsv',file($file_name));
0 ignored issues
show
Bug introduced by
It seems like file($file_name) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, 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

106
        $csv = array_map('str_getcsv',/** @scrutinizer ignore-type */ file($file_name));
Loading history...
107
108
        $thead = array_shift($csv);
109
110
        $lastKey = array_search(end($thead), $thead);
0 ignored issues
show
Unused Code introduced by
The assignment to $lastKey is dead and can be removed.
Loading history...
111
112
        
113
114
        $html = '
115
        <html>
116
        </head>
117
        <style>
118
        td, th {
119
            border: 1px solid #dddddd;
120
            text-align: left;
121
            padding: 8px;
122
          }
123
          
124
          th {
125
126
            font-family : bold;
127
128
          }
129
        
130
        </style>
131
        </head>
132
        ';
133
134
        $html .= '<body><table><thead><tr>';
135
136
        
137
138
139
        for($i = 0 ; $i<count($thead); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
140
            $html.= "<th bgcolor='#ccc'>$thead[$i]</th>";
141
        }
142
        
143
            
144
        if($i>=count($thead)) $html.= "</tr></thead>";
145
146
        foreach($csv as $tbody) {
147
            $html .= "<tr>";
148
            foreach ($tbody as $td ) {
149
                $html .= "<td>$td</td>";
150
            }
151
            $html .= "</tr>";
152
        }
153
154
        
155
        $html .= "</table></body>";
156
157
        $id = DB::table('job_update')->insertGetId([
158
            "name" => "PDF",
159
            "status" => 'processing',
160
            "file_name" => '0'
161
        ]);
162
        WritePdf::dispatch($html,$id);
163
        return response()->json(["message"=>"Preparing File For Download..."], 200);
164
    }
165
166
}