Completed
Push — master ( 664134...f10342 )
by Tim
04:39 queued 02:23
created

ProductSourceItemDefaultObserver::newArtefact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Observers\ProductSourceItemDefaultObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-msi
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Msi\Observers;
22
23
use TechDivision\Import\Product\Msi\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
25
26
/**
27
 * Observer that extracts the MSI source item data to a specific CSV file.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2019 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-msi
33
 * @link      http://www.techdivision.com
34
 */
35
class ProductSourceItemDefaultObserver extends AbstractProductImportObserver
36
{
37
38
    /**
39
     * The artefact type.
40
     *
41
     * @var string
42
     */
43
    const ARTEFACT_TYPE = 'inventory-msi';
44
45
    /**
46
     * Process the observer's business logic.
47
     *
48
     * @return array The processed row
49
     */
50
    protected function process()
51
    {
52
53
        // query whether or not, we've found a new SKU => means we've found a new product
54
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::SKU))) {
55
            return;
56
        }
57
58
        // initialize the array for the artefacts and the store view codes
59
        $artefacts = array();
60
61
        // create a new artefact with the column information
62
        $artefacts[] = $this->newArtefact(
63
            array(
64
                ColumnKeys::SKU         => $this->getValue(ColumnKeys::SKU),
65
                ColumnKeys::SOURCE_CODE => 'default',
66
                ColumnKeys::STATUS      => 1,
67
                ColumnKeys::QUANTITY    => $this->getValue(ColumnKeys::QTY, 0)
68
            ),
69
            array(
70
                ColumnKeys::SKU         => ColumnKeys::SKU,
71
                ColumnKeys::SOURCE_CODE => null,
72
                ColumnKeys::STATUS      => null,
73
                ColumnKeys::QUANTITY    => ColumnKeys::QTY
74
            )
75
        );
76
77
        // append the artefacts that has to be exported to the subject
78
        $this->addArtefacts($artefacts);
79
    }
80
81
    /**
82
     * Create's and return's a new empty artefact entity.
83
     *
84
     * @param array $columns             The array with the column data
85
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
86
     *
87
     * @return array The new artefact entity
88
     */
89
    protected function newArtefact(array $columns, array $originalColumnNames)
90
    {
91
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
92
    }
93
94
    /**
95
     * Add the passed product type artefacts to the product with the
96
     * last entity ID.
97
     *
98
     * @param array $artefacts The product type artefacts
99
     *
100
     * @return void
101
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
102
     */
103
    protected function addArtefacts(array $artefacts)
104
    {
105
        $this->getSubject()->addArtefacts(ProductSourceItemObserver::ARTEFACT_TYPE, $artefacts, false);
106
    }
107
}
108