Completed
Push — master ( 23587a...d53256 )
by Tim
8s
created

initializeStockStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 18
loc 18
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductInventoryUpdateObserver
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 2016 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Product\Utils\MemberNames;
24
25
/**
26
 * Observer that creates/updates the product's inventory.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 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
32
 * @link      http://www.techdivision.com
33
 */
34
class ProductInventoryUpdateObserver extends ProductInventoryObserver
35
{
36
37
    /**
38
     * Initialize the stock item with the passed attributes and returns an instance.
39
     *
40
     * @param array $attr The stock item attributes
41
     *
42
     * @return array The initialized stock item
43
     */
44
    protected function initializeStockItem(array $attr)
45
    {
46
47
        // load the stock item with the passed item/product/stock ID
48
        $entity = $this->loadStockItem(
49
            $attr[MemberNames::PRODUCT_ID],
50
            $attr[MemberNames::WEBSITE_ID],
51
            $attr[MemberNames::STOCK_ID]
52
        );
53
54
        // merge the attributes with the entity, if available
55
        if ($entity) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entity of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
            return $this->mergeEntity($entity, $attr);
57
        }
58
59
        // otherwise simply return the attributes
60
        return $attr;
61
    }
62
63
    /**
64
     * Load's and return's the stock status with the passed product/website/stock ID.
65
     *
66
     * @param integer $productId The product ID of the stock item to load
67
     * @param integer $websiteId The website ID of the stock item to load
68
     * @param integer $stockId   The stock ID of the stock item to load
69
     *
70
     * @return array The stock item
71
     */
72
    protected function loadStockItem($productId, $websiteId, $stockId)
73
    {
74
        return $this->getProductBunchProcessor()->loadStockItem($productId, $websiteId, $stockId);
75
    }
76
}
77