Passed
Push — master ( 564b7e...61dc89 )
by Tim
06:19 queued 11s
created

ProductSourceItemObserver::addArtefacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Observers\ProductSourceItemObserver
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
25
/**
26
 * Observer that extracts the MSI source item data to a specific CSV file.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-product-msi
32
 * @link      http://www.techdivision.com
33
 */
34
class ProductSourceItemObserver extends AbstractMsiImportObserver
35
{
36
37
    /**
38
     * The artefact type.
39
     *
40
     * @var string
41
     */
42
    const ARTEFACT_TYPE = 'product-import-inventory-msi';
43
44
    /**
45
     * Process the observer's business logic.
46
     *
47
     * @return array The processed row
48
     */
49
    protected function process()
50
    {
51
52
        // initialize the array for the artefacts and the store view codes
53
        $artefacts = array();
54
55
        // Unserialize the inventory source item data from from the column that looks like
56
        // source_code=default,quantity=10.0,status=1|source_code=default,quantity=11.0,status=0
57
        $msiInventorySources = $this->getValue(ColumnKeys::INVENTORY_SOURCE_ITEMS, array(), function ($value) {
58
            return $this->explode($value, '|');
0 ignored issues
show
Deprecated Code introduced by
The function TechDivision\Import\Obse...ractObserver::explode() has been deprecated: Will be removed with version 1.0.0, use subject method instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
            return /** @scrutinizer ignore-deprecated */ $this->explode($value, '|');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
59
        });
60
61
        // iterate over the found inventory source items
62
        foreach ($msiInventorySources as $msiInventorySource) {
63
            // explode the key => values pairs
64
            $extractedColumns = $this->explode($msiInventorySource);
0 ignored issues
show
Deprecated Code introduced by
The function TechDivision\Import\Obse...ractObserver::explode() has been deprecated: Will be removed with version 1.0.0, use subject method instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
            $extractedColumns = /** @scrutinizer ignore-deprecated */ $this->explode($msiInventorySource);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
            // initialize the array with the column we want to export
66
            $columns = array(ColumnKeys::SKU => $this->getValue(ColumnKeys::SKU));
67
            // append the extracted values to the array
68
            foreach ($extractedColumns as $extractedColumn) {
69
                // extract key => value pair
70
                list ($key, $value) = $this->explode($extractedColumn, '=');
0 ignored issues
show
Deprecated Code introduced by
The function TechDivision\Import\Obse...ractObserver::explode() has been deprecated: Will be removed with version 1.0.0, use subject method instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
                list ($key, $value) = /** @scrutinizer ignore-deprecated */ $this->explode($extractedColumn, '=');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
                // append the key => value pair
72
                $columns[$key] = $value;
73
            }
74
75
            // create a new artefact with the column information
76
            $artefacts[] = $this->newArtefact($columns, array());
77
        }
78
79
        // append the artefacts that has to be exported to the subject
80
        $this->addArtefacts($artefacts);
81
    }
82
83
    /**
84
     * Create's and return's a new empty artefact entity.
85
     *
86
     * @param array $columns             The array with the column data
87
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
88
     *
89
     * @return array The new artefact entity
90
     */
91
    protected function newArtefact(array $columns, array $originalColumnNames)
92
    {
93
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
94
    }
95
96
    /**
97
     * Add the passed product type artefacts to the product with the
98
     * last entity ID.
99
     *
100
     * @param array $artefacts The product type artefacts
101
     *
102
     * @return void
103
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
104
     */
105
    protected function addArtefacts(array $artefacts)
106
    {
107
        $this->getSubject()->addArtefacts(ProductSourceItemObserver::ARTEFACT_TYPE, $artefacts, false);
108
    }
109
}
110