1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Jobs; |
4
|
|
|
|
5
|
|
|
use App\Events\ImportParseFinished; |
6
|
|
|
use App\Models\Import; |
7
|
|
|
use App\Services\Import\DataImporter; |
8
|
|
|
use App\Services\Import\GoogleSpreadsheet; |
9
|
|
|
use Illuminate\Bus\Queueable; |
10
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
11
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
12
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
13
|
|
|
use Illuminate\Queue\SerializesModels; |
14
|
|
|
use const EXTR_OVERWRITE; |
15
|
|
|
use function collect; |
16
|
|
|
use function extract; |
17
|
|
|
|
18
|
|
|
class ParseVocabulary implements ShouldQueue |
19
|
|
|
{ |
20
|
|
|
use InteractsWithQueue, Queueable, SerializesModels, Dispatchable; |
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Import |
23
|
|
|
*/ |
24
|
|
|
private $import; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $worksheet_id; |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $spreadsheet; |
33
|
|
|
|
34
|
|
|
public function __construct(Import $import, string $worksheet_id, array $spreadsheet) |
35
|
|
|
{ |
36
|
|
|
$this->import = $import; |
37
|
|
|
$this->worksheet_id = $worksheet_id; |
38
|
|
|
$this->spreadsheet = $spreadsheet; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the job. |
43
|
|
|
*/ |
44
|
|
|
public function handle() |
45
|
|
|
{ |
46
|
|
|
$source_file_name = ''; |
47
|
|
|
$import_type = 0; |
|
|
|
|
48
|
|
|
extract($this->spreadsheet, EXTR_OVERWRITE); |
49
|
|
|
$sheet = new GoogleSpreadsheet($source_file_name); |
50
|
|
|
$data = collect($sheet->getWorksheetData($this->worksheet_id)->toArray()); |
51
|
|
|
//TODO: Handle $import_type in the importer |
52
|
|
|
$importer = new DataImporter($data, $this->import->export); |
53
|
|
|
//then we pass them to the importer |
54
|
|
|
$changeSet = $importer->getChangeset(); |
55
|
|
|
$this->import->instructions = $changeSet; |
|
|
|
|
56
|
|
|
$this->import->preprocess = $importer->getStats(); |
|
|
|
|
57
|
|
|
$this->import->errors = $importer->getErrors()->toJson(); |
58
|
|
|
$this->import->save(); |
59
|
|
|
|
60
|
|
|
event(new ImportParseFinished($this->import)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|