Completed
Push — master ( b5f631...d39ce5 )
by Gaël
03:21
created

PrestashopSuppliersImportCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use DansMaCulotte\PrestashopWebService\Exceptions\PrestashopWebServiceException;
6
use DansMaCulotte\PrestashopWebService\PrestashopWebService;
7
use Illuminate\Console\Command;
8
9
abstract class PrestashopSuppliersImportCommand 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) {
0 ignored issues
show
Bug introduced by
The expression $suppliers of type array|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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