ProductAbstractSkuToIdProductAbstractStep   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 12 3
A resolveIdProductAbstract() 0 10 2
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Zed\DataImport\Business\Model\ProductAbstract;
11
12
use Orm\Zed\Product\Persistence\SpyProductAbstractQuery;
13
use Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException;
14
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
15
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
16
17
class ProductAbstractSkuToIdProductAbstractStep implements DataImportStepInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    public const KEY_SOURCE = 'sku';
23
24
    /**
25
     * @var string
26
     */
27
    public const KEY_TARGET = 'idProductAbstract';
28
29
    /**
30
     * @var string
31
     */
32
    protected $source;
33
34
    /**
35
     * @var string
36
     */
37
    protected $target;
38
39
    /**
40
     * @var array<string, int>
41
     */
42
    protected static $resolved = [];
43
44
    /**
45
     * @param string $source
46
     * @param string $target
47
     */
48
    public function __construct(string $source = self::KEY_SOURCE, string $target = self::KEY_TARGET)
49
    {
50
        $this->source = $source;
51
        $this->target = $target;
52
    }
53
54
    /**
55
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
56
     *
57
     * @return void
58
     */
59
    public function execute(DataSetInterface $dataSet): void
60
    {
61
        if (empty($dataSet[$this->source])) {
62
            return;
63
        }
64
65
        $sku = $dataSet[$this->source];
66
        if (!isset(static::$resolved[$sku])) {
67
            static::$resolved[$sku] = $this->resolveIdProductAbstract($sku);
68
        }
69
70
        $dataSet[$this->target] = static::$resolved[$sku];
71
    }
72
73
    /**
74
     * @param string $sku
75
     *
76
     * @throws \Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException
77
     *
78
     * @return int
79
     */
80
    protected function resolveIdProductAbstract(string $sku): int
81
    {
82
        $query = SpyProductAbstractQuery::create();
83
        $productAbstractEntity = $query->findOneBySku($sku);
84
85
        if (!$productAbstractEntity) {
86
            throw new EntityNotFoundException(sprintf('Abstract product with sku "%s" not found.', $sku));
87
        }
88
89
        return $productAbstractEntity->getIdProductAbstract();
90
    }
91
}
92