InventorySourceItemUpdateObserver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 12
cp 0
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadInventorySourceItemBySkuAndSourceCode() 0 3 1
A initializeInventorySourceItem() 0 19 5
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Observers\InventorySourceItemUpdateObserver
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2018 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-product-msi
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Product\Msi\Observers;
16
17
use TechDivision\Import\Product\Msi\Utils\ColumnKeys;
18
use TechDivision\Import\Product\Msi\Utils\MemberNames;
19
20
/**
21
 * Observer that prepares the MSI source item information found in the CSV file.
22
 *
23
 * @author    Tim Wagner <[email protected]>
24
 * @copyright 2018 TechDivision GmbH <[email protected]>
25
 * @license   https://opensource.org/licenses/MIT
26
 * @link      https://github.com/techdivision/import-product-msi
27
 * @link      http://www.techdivision.com
28
 */
29
class InventorySourceItemUpdateObserver extends InventorySourceItemObserver
30
{
31
32
    /**
33
     * Initialize the MSI inventory source item with the passed attributes and returns an instance.
34
     *
35
     * @param array $attr The inventory source item attributes
36
     *
37
     * @return array The initialized inventory source item
38
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
39
     */
40
    protected function initializeInventorySourceItem(array $attr)
41
    {
42
43
        // try to load the inventory source item with the given SKU and source code
44
        if ($entity = $this->loadInventorySourceItemBySkuAndSourceCode($attr[MemberNames::SKU], $attr[MemberNames::SOURCE_CODE])) {
45
            if (\array_key_exists(ColumnKeys::RELATIVE, $attr)) {
46
                if (((int) $attr[ColumnKeys::RELATIVE]) === 1) {
47
                    $attr[MemberNames::QUANTITY] += $entity[MemberNames::QUANTITY];
48
                }
49
                unset($attr[ColumnKeys::RELATIVE]);
50
            }
51
52
            return $this->mergeEntity($entity, $attr);
53
        }
54
        if (\array_key_exists(ColumnKeys::RELATIVE, $attr)) {
55
            unset($attr[ColumnKeys::RELATIVE]);
56
        }
57
        // simply return the attributes
58
        return $attr;
59
    }
60
61
    /**
62
     * Return's the inventory source item with the passed SKU and source code.
63
     *
64
     * @param string $sku        The SKU of the inventory source item to return
65
     * @param string $sourceCode The source code of the inventory source item to return
66
     *
67
     * @return array The inventory source item to return
68
     */
69
    protected function loadInventorySourceItemBySkuAndSourceCode($sku, $sourceCode)
70
    {
71
        return $this->getMsiBunchProcessor()->loadInventorySourceItemBySkuAndSourceCode($sku, $sourceCode);
72
    }
73
}
74