ArticleImporter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace TEiling\Scd16\Importer;
4
5
use TEiling\Scd16\Mapper\ArticleMapper;
6
use TEiling\Scd16\Resource\ArticleResourceInterface as ArticleRes;
7
use TEiling\Scd16\Reader\CsvReaderInterface;
8
use TEiling\Scd16\Mapper\ArticleMapperInterface;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
11
class ArticleImporter implements ArticleImporterInterface
12
{
13
    /** @var  ArticleRes */
14
    private $articleRes;
15
16
    /** @var  CsvReaderInterface */
17
    private $reader;
18
19
    /** @var  ArticleMapper */
20
    private $articleMapper;
21
22
    /**
23
     * ArticleMapper constructor.
24
     *
25
     * @param ArticleRes             $articleRes
26
     * @param CsvReaderInterface     $reader
27
     * @param ArticleMapperInterface $articleMapper
28
     */
29
    public function __construct(ArticleRes $articleRes, CsvReaderInterface $reader, ArticleMapperInterface $articleMapper)
30
    {
31
        $this->articleRes = $articleRes;
32
        $this->reader = $reader;
33
        $this->articleMapper = $articleMapper;
0 ignored issues
show
Documentation Bug introduced by
$articleMapper is of type object<TEiling\Scd16\Map...ArticleMapperInterface>, but the property $articleMapper was declared to be of type object<TEiling\Scd16\Mapper\ArticleMapper>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
34
    }
35
36
37
    /**
38
     * @param             $csvPath
39
     * @param ProgressBar $progressBar
40
     *
41
     * @todo get csvPath from config
42
     *
43
     * @return array
44
     * @throws \Shopware\Components\Api\Exception\NotFoundException
45
     * @throws \Shopware\Components\Api\Exception\ParameterMissingException
46
     * @throws \Shopware\Components\Api\Exception\ValidationException
47
     */
48
    public function importArticles($csvPath, ProgressBar $progressBar = null)
49
    {
50
        $csv = $this->reader->readCsv($csvPath);
51
        $articles = $this->articleMapper->mapArticles($csv);
52
53
        $status = [
54
            'updated' => 0,
55
            'created' => 0,
56
            'errors' => ''
57
        ];
58
59
        foreach ($articles as $article) {
60
            // check if article exists then update it otherwise create it
61
            $orderNumber = $article['mainDetail']['number'];
62
            try {
63
                $articleId = $this->articleRes->getIdByData($article);
64
                if ($articleId > 0) {
65
                    $this->articleRes->update($articleId, $article);
66
                    $status['updated'] ++;
67
                } else {
68
                    $this->articleRes->create($article);
69
                    $status['created'] ++;
70
                }
71
            } catch (\Exception $e) {
72
                $status['errors'] .= $orderNumber . ' - ' . $e->getMessage() . ' - ' . $e->getTraceAsString() . "\n\r";
73
            }
74
75
            if ($progressBar !== null) {
76
                $progressBar->advance();
77
            }
78
        }
79
80
        return $status;
81
82
    }
83
}
84