DiscountStoreWriterStep   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdStoreByName() 0 8 2
A getIdDiscountByKey() 0 8 2
A execute() 0 7 1
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\DiscountStore;
11
12
use Orm\Zed\Discount\Persistence\SpyDiscountQuery;
13
use Orm\Zed\Discount\Persistence\SpyDiscountStoreQuery;
14
use Orm\Zed\Store\Persistence\SpyStoreQuery;
15
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
16
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
17
18
class DiscountStoreWriterStep implements DataImportStepInterface
19
{
20
    public const BULK_SIZE = 100;
21
22
    public const KEY_DISCOUNT_KEY = 'discount_key';
23
24
    public const KEY_STORE_NAME = 'store_name';
25
26
    /**
27
     * @var array<int> Keys are discount keys, values are discount IDs.
28
     */
29
    protected static array $idDiscountBuffer = [];
30
31
    /**
32
     * @var array<int> Keys are store names, values are store ids.
33
     */
34
    protected static array $idStoreBuffer = [];
35
36
    public function execute(DataSetInterface $dataSet): void
37
    {
38
        (new SpyDiscountStoreQuery())
39
            ->filterByFkDiscount($this->getIdDiscountByKey($dataSet[static::KEY_DISCOUNT_KEY]))
40
            ->filterByFkStore($this->getIdStoreByName($dataSet[static::KEY_STORE_NAME]))
41
            ->findOneOrCreate()
42
            ->save();
43
    }
44
45
    protected function getIdDiscountByKey(string $discountKey): int
46
    {
47
        if (!isset(static::$idDiscountBuffer[$discountKey])) {
48
            static::$idDiscountBuffer[$discountKey] =
49
                SpyDiscountQuery::create()->findOneByDiscountKey($discountKey)->getIdDiscount();
50
        }
51
52
        return static::$idDiscountBuffer[$discountKey];
53
    }
54
55
    protected function getIdStoreByName(string $storeName): int
56
    {
57
        if (!isset(static::$idStoreBuffer[$storeName])) {
58
            static::$idStoreBuffer[$storeName] =
59
                SpyStoreQuery::create()->findOneByName($storeName)->getIdStore();
60
        }
61
62
        return static::$idStoreBuffer[$storeName];
63
    }
64
}
65