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

addSkuSourceItemIdMapping()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Observers\InventorySourceItemObserver
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 2018 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\Msi\Utils\MemberNames;
25
use TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface;
26
27
/**
28
 * Observer that prepares the MSI source item information found in the CSV file.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2018 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-msi
34
 * @link      http://www.techdivision.com
35
 */
36
class InventorySourceItemObserver extends AbstractMsiImportObserver
37
{
38
39
    /**
40
     * The MSI bunch processor instance.
41
     *
42
     * @var \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface
43
     */
44
    protected $msiBunchProcessor;
45
46
    /**
47
     * Initialize the observer with the passed MSI bunch processor instance.
48
     *
49
     * @param \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface $msiBunchProcessor The MSI bunch processor instance
50
     */
51
    public function __construct(MsiBunchProcessorInterface $msiBunchProcessor)
52
    {
53
        $this->msiBunchProcessor = $msiBunchProcessor;
54
    }
55
56
    /**
57
     * Return's the MSI bunch processor instance.
58
     *
59
     * @return \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface The MSI bunch processor instance
60
     */
61
    protected function getMsiBunchProcessor()
62
    {
63
        return $this->msiBunchProcessor;
64
    }
65
66
    /**
67
     * Process the observer's business logic.
68
     *
69
     * @return array The processed row
70
     */
71
    protected function process()
72
    {
73
74
        // load SKU and inventory source code
75
        $sku = $this->getValue(ColumnKeys::SKU);
76
        $sourceCode = $this->getValue(ColumnKeys::SOURCE_CODE);
77
78
        // query whether or not, we've found a new SKU/source code combination => means we've found a inventory source item
79
        if ($this->hasBeenProcessed($sku, $sourceCode)) {
80
            return;
81
        }
82
83
        // query whether or not the inventory source is with the given code is avaiable
84
        if ($this->hasInventorySource($sourceCode)) {
85
            // create the MSI inventory source item
86
            $inventorySourceItem = $this->initializeInventorySourceItem($this->prepareAttributes());
87
            $this->persistInventorySourceItem($inventorySourceItem);
88
89
            // finaly, add the SKU + source code => source item ID mapping
90
            $this->addSkuSourceItemIdMapping($sku, $sourceCode);
91
        } else {
92
            // throw a new exception
93
            throw new \Exception(
94
                $this->appendExceptionSuffix(
0 ignored issues
show
Deprecated Code introduced by
The function TechDivision\Import\Obse...appendExceptionSuffix() 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

94
                /** @scrutinizer ignore-deprecated */ $this->appendExceptionSuffix(

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...
95
                    sprintf('Can\'t load inventory source with code "%s"', $sourceCode)
96
                )
97
            );
98
        }
99
    }
100
101
    /**
102
     * Prepare the attributes of the entity that has to be persisted.
103
     *
104
     * @return array The prepared attributes
105
     */
106
    protected function prepareAttributes()
107
    {
108
109
        // load the MSI inventory source item values
110
        $sku = $this->getValue(ColumnKeys::SKU);
111
        $status = $this->getValue(ColumnKeys::STATUS);
112
        $sourceCode = $this->getValue(ColumnKeys::SOURCE_CODE);
113
        $quantity = $this->getValue(ColumnKeys::QUANTITY);
114
115
        // return the prepared MSI inventory source item
116
        return $this->initializeEntity(
117
            array(
118
                MemberNames::SKU         => $sku,
119
                MemberNames::STATUS      => $status,
120
                MemberNames::SOURCE_CODE => $sourceCode,
121
                MemberNames::QUANTITY    => $quantity
122
            )
123
        );
124
    }
125
126
    /**
127
     * Initialize the MSI inventory source item with the passed attributes and returns an instance.
128
     *
129
     * @param array $attr The inventory source item attributes
130
     *
131
     * @return array The initialized inventory source item
132
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
133
     */
134
    protected function initializeInventorySourceItem(array $attr)
135
    {
136
        return $attr;
137
    }
138
139
    /**
140
     * Queries whether or not the inventory source with the passed code is available.
141
     *
142
     * @param string $sourceCode The code of the inventory source to query for
143
     *
144
     * @return boolean TRUE if the inventory source with the passed code is available, FALSE if not
145
     */
146
    protected function hasInventorySource($sourceCode)
147
    {
148
        return $this->getSubject()->hasInventorySource($sourceCode);
149
    }
150
151
    /**
152
     * Add the passed SKU/source code => source item ID mapping.
153
     *
154
     * @param string $sku        The SKU
155
     * @param string $sourceCode The source code
156
     *
157
     * @return void
158
     */
159
    protected function addSkuSourceItemIdMapping($sku, $sourceCode)
160
    {
161
        $this->getSubject()->addSkuSourceItemIdMapping($sku, $sourceCode);
162
    }
163
164
    /**
165
     * Persist's the passed inventory source item data.
166
     *
167
     * @param array $inventorySourceItem The inventory source item data to persist
168
     *
169
     * @return void
170
     */
171
    protected function persistInventorySourceItem($inventorySourceItem)
172
    {
173
        $this->getMsiBunchProcessor()->persistInventorySourceItem($inventorySourceItem);
174
    }
175
}
176