Exporter::excel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.9332
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\DB;
10
use LWS\ExportActions\Jobs\WritePdf;
11
use Illuminate\Support\Facades\Storage;
12
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...
13
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...
14
15
class Exporter extends Controller
16
{
17
    public function download(Request $request)
18
    {
19
        if (DB::table('job_update')->where('id', $request->id)->exists()) {
20
            $file_download_progress = DB::table('job_update')->where('id', $request->id)->first();
21
            //dd($file_download_progress->file_name);
22
            return response()->download($file_download_progress->file_name);
23
        } else {
24
            return response()->json(['message' => 'Something Went Wrong'], 500);
25
        }
26
    }
27
28
    public function progress()
29
    {
30
        $data = DB::table('job_update')->get()->toJson();
31
32
        return response($data, 200);
33
    }
34
35
    public function export(Request $requestData)
36
    {
37
        $client = new Client();
38
39
        $request = new \GuzzleHttp\Psr7\Request('GET', $requestData->url);
40
        $promise = $client->sendAsync($request)->then(function ($responseData) {
41
            $formatter = Formatter::make($responseData->getBody(), Formatter::JSON);
42
            $csv = $formatter->toCsv();
43
44
            Storage::disk('local')->put('csvfile.csv', $csv);
45
        });
46
47
        $promise->wait();
48
49
        $file_name = storage_path().'/app/csvfile.csv';
50
51
        switch (strtolower($requestData->format)) {
52
            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...
53
            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...
54
            case 'pdf': return $this->pdf($file_name); break;
55
        } //switch
56
    }
57
58
    public function csv($file_name)
59
    {
60
        $headers = [
61
            'Content-Type' => 'text/csv',
62
         ];
63
64
        return response()->download($file_name, 'export.csv', $headers);
65
    }
66
67
    public function excel($file_name)
68
    {
69
        $spreadsheet = new Spreadsheet();
0 ignored issues
show
Unused Code introduced by
The assignment to $spreadsheet is dead and can be removed.
Loading history...
70
        $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...
71
72
        /* Set CSV parsing options */
73
        $reader->setDelimiter(',');
74
        $reader->setEnclosure('"');
75
        $reader->setSheetIndex(0);
76
77
        /* Load a CSV file and save as a XLS */
78
        $spreadsheet = $reader->load($file_name);
79
        $writer = new Xlsx($spreadsheet);
80
81
        $writer->save('export.xlsx');
82
83
        $spreadsheet->disconnectWorksheets();
84
        unset($spreadsheet);
85
    }
86
87
    public function pdf($file_name)
88
    {
89
        $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

89
        $csv = array_map('str_getcsv', /** @scrutinizer ignore-type */ file($file_name));
Loading history...
90
91
        $thead = array_shift($csv);
92
93
        $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...
94
95
        $html = '
96
        <html>
97
        </head>
98
        <style>
99
        td, th {
100
            border: 1px solid #dddddd;
101
            text-align: left;
102
            padding: 8px;
103
          }
104
          
105
          th {
106
107
            font-family : bold;
108
109
          }
110
        
111
        </style>
112
        </head>
113
        ';
114
115
        $html .= '<body><table><thead><tr>';
116
117
        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...
118
            $html .= "<th bgcolor='#ccc'>$thead[$i]</th>";
119
        }
120
121
        if ($i >= count($thead)) {
122
            $html .= '</tr></thead>';
123
        }
124
125
        foreach ($csv as $tbody) {
126
            $html .= '<tr>';
127
            foreach ($tbody as $td) {
128
                $html .= "<td>$td</td>";
129
            }
130
            $html .= '</tr>';
131
        }
132
133
        $html .= '</table></body>';
134
135
        $id = DB::table('job_update')->insertGetId([
136
            'name' => 'PDF',
137
            'status' => 'processing',
138
            'file_name' => '0',
139
        ]);
140
        WritePdf::dispatch($html, $id);
141
142
        return response()->json(['message'=>'Preparing File For Download...'], 200);
143
    }
144
}
145