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; |
|
|
|
|
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
|
|
|
|
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.