|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DansMaCulotte\PrestashopWebService\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use DansMaCulotte\PrestashopWebService\Exceptions\PrestashopWebServiceException; |
|
6
|
|
|
use DansMaCulotte\PrestashopWebService\PrestashopWebService; |
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractPrestashopSuppliersImportCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
const PRESTASHOP_RESOURCE_NAME = 'suppliers'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'prestashop:import-suppliers {--id=* : The Prestashop ID of the supplier}'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Import and sync vendors with Prestashop suppliers database'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The prestashop singleton from the service provider |
|
29
|
|
|
* @var PrestashopWebService |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $prestashop; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new command instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param PrestashopWebService $prestashop |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(PrestashopWebService $prestashop) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
|
|
42
|
|
|
$this->prestashop = $prestashop; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Execute the console command. |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function handle() |
|
51
|
|
|
{ |
|
52
|
|
|
$ids = $this->option('id'); |
|
53
|
|
|
$debug = $this->getOutput()->isDebug(); |
|
54
|
|
|
|
|
55
|
|
|
$this->info('Importing suppliers'); |
|
56
|
|
|
|
|
57
|
|
|
$suppliers = count($ids) ? $ids : $this->getSuppliers(); |
|
58
|
|
|
$bar = $this->output->createProgressBar(count($suppliers)); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($suppliers as $supplierId) { |
|
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$rawSupplier = $this->getSupplier($supplierId); |
|
63
|
|
|
$supplier = $this->importSupplier($rawSupplier); |
|
64
|
|
|
|
|
65
|
|
|
$bar->advance(); |
|
66
|
|
|
|
|
67
|
|
|
if ($debug) { |
|
68
|
|
|
$this->info("Prestashop: Imported supplier {$supplier->name}"); |
|
69
|
|
|
} |
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
$this->error("Prestashop: Failed to request supplier {$rawSupplier->id}"); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$bar->finish(); |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
private function getSuppliers() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
$xml = $this->prestashop->get([ |
|
87
|
|
|
'resource' => self::PRESTASHOP_RESOURCE_NAME, |
|
88
|
|
|
]); |
|
89
|
|
|
} catch (PrestashopWebServiceException $e) { |
|
90
|
|
|
$this->error('Prestashop: Failed to request suppliers'); |
|
91
|
|
|
return []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$ids = []; |
|
95
|
|
|
|
|
96
|
|
|
foreach ($xml->suppliers->children() as $supplier) { |
|
97
|
|
|
foreach ($supplier->attributes() as $key => $value) { |
|
98
|
|
|
if ($key === 'id') { |
|
99
|
|
|
array_push($ids, (string) $value); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $ids; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $id |
|
109
|
|
|
* @return \SimpleXMLElement |
|
110
|
|
|
* @throws \DansMaCulotte\PrestashopWebService\Exceptions\PrestashopWebServiceException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getSupplier(string $id) |
|
113
|
|
|
{ |
|
114
|
|
|
$xml = $this->prestashop->get([ |
|
115
|
|
|
'resource' => self::PRESTASHOP_RESOURCE_NAME, |
|
116
|
|
|
'id' => $id, |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
return $xml->supplier; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param \SimpleXMLElement $supplier |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract public function importSupplier(\SimpleXMLElement $supplier); |
|
126
|
|
|
} |
|
127
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.