WriteCsv::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LWS\ExportActions\Jobs;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Http\Request;
7
use Illuminate\Bus\Queueable;
8
use SoapBox\Formatter\Formatter;
9
use Illuminate\Queue\SerializesModels;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Queue\InteractsWithQueue;
12
use Illuminate\Contracts\Queue\ShouldQueue;
13
use Illuminate\Foundation\Bus\Dispatchable;
14
15
class WriteCsv implements ShouldQueue
16
{
17
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by LWS\ExportActions\Jobs\WriteCsv: $id, $relations, $class, $keyBy
Loading history...
18
19
    protected $request;
20
21
    public $timeout = 120;
22
23
    /**
24
     * Create a new job instance.
25
     *
26
     * @return void
27
     */
28
    public function __construct(Request $request)
29
    {
30
        $this->requestData = $request->all();
0 ignored issues
show
Bug Best Practice introduced by
The property requestData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
    }
32
33
    /**
34
     * Execute the job.
35
     *
36
     * @return void
37
     */
38
    public function handle()
39
    {
40
        $client = new Client();
41
42
        $request = new \GuzzleHttp\Psr7\Request('GET', $this->requestData->url);
43
        $promise = $client->sendAsync($request)->then(function ($responseData) {
44
            $formatter = Formatter::make($responseData->getBody(), Formatter::JSON);
45
            $csv = $formatter->toCsv();
46
47
            Storage::disk('local')->put('csvfile.csv', $csv);
48
        });
49
50
        $promise->wait();
51
    }
52
}
53