Completed
Pull Request — master (#330)
by Stefan
04:25
created

StatefulVariantRegenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace ShopwarePlugins\Connect\Components\Variant;
4
5
use Shopware\CustomModels\Connect\Attribute;
6
use ShopwarePlugins\Connect\Components\Config;
7
use ShopwarePlugins\Connect\Components\ProductStream\ProductStreamsAssignments;
8
use ShopwarePlugins\Connect\Components\VariantRegenerator;
9
use ShopwarePlugins\Connect\Components\ConnectExport;
10
use ShopwarePlugins\Connect\Components\ProductStream\ProductStreamService;
11
12
class StatefulVariantRegenerator implements VariantRegenerator
13
{
14
    private static $initialSourceIds = [];
15
16
    private static $currentSourceIds = [];
17
18
    /**
19
     * @var ConnectExport
20
     */
21
    private $connectExport;
22
23
    /**
24
     * @var ProductStreamService
25
     */
26
    private $productStreamService;
27
28
    /**
29
     * @var string
30
     */
31
    private $autoUpdateProducts;
32
33
    /**
34
     * StatefulVariantRegenerator constructor.
35
     * @param ConnectExport $connectExport
36
     * @param ProductStreamService $productStreamService
37
     * @param $autoUpdateProducts
38
     */
39
    public function __construct(
40
        ConnectExport $connectExport,
41
        ProductStreamService $productStreamService,
42
        $autoUpdateProducts
43
    ) {
44
        $this->connectExport = $connectExport;
45
        $this->productStreamService = $productStreamService;
46
        $this->autoUpdateProducts = $autoUpdateProducts;
47
    }
48
49
    /**
50
     * @param int $articleId
51
     * @param array $sourceIds
52
     */
53
    public function setInitialSourceIds($articleId, array $sourceIds)
54
    {
55
        self::$initialSourceIds[$articleId] = $sourceIds;
56
    }
57
58
    /**
59
     * @param int $articleId
60
     * @param array $sourceIds
61
     */
62
    public function setCurrentSourceIds($articleId, array $sourceIds)
63
    {
64
        self::$currentSourceIds[$articleId] = $sourceIds;
65
    }
66
67
    /**
68
     * @param int $articleId
69
     */
70
    public function generateChanges($articleId)
71
    {
72
        if (!isset(self::$initialSourceIds[$articleId])) {
73
            throw new \InvalidArgumentException('Initial sourceIds are not set!');
74
        }
75
76
        if (!isset(self::$currentSourceIds[$articleId])) {
77
            throw new \InvalidArgumentException('Current sourceIds are not set!');
78
        }
79
80
        $sourceIdsForDelete = array_diff(
81
            self::$initialSourceIds[$articleId],
82
            self::$currentSourceIds[$articleId]
83
        );
84
        foreach ($sourceIdsForDelete as $sourceId) {
85
            $this->connectExport->recordDelete($sourceId);
86
        }
87
88
        $this->connectExport->updateConnectItemsStatus($sourceIdsForDelete, Attribute::STATUS_DELETE);
89
90
        if ($this->autoUpdateProducts == Config::UPDATE_CRON_JOB) {
91
            $this->connectExport->markArticleForCronUpdate($articleId);
92
            return;
93
        }
94
95
        $this->connectExport->export(
96
            self::$currentSourceIds,
97
            new ProductStreamsAssignments(
98
                ['assignments' => $this->productStreamService->collectRelatedStreamsAssignments([$articleId])]
99
            )
100
        );
101
102
        unset(self::$initialSourceIds[$articleId]);
103
        unset(self::$currentSourceIds[$articleId]);
104
    }
105
}