Completed
Pull Request — master (#17)
by Tim
03:38
created

ProductUpdateObserver::initializeProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductUpdateObserver
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 add's/update's the product itself.
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 ProductUpdateObserver extends ProductObserver
35
{
36
37
    /**
38
     * Initialize the product with the passed attributes and returns an instance.
39
     *
40
     * @param array $attr The product attributes
41
     *
42
     * @return array The initialized product
43
     */
44 1
    protected function initializeProduct(array $attr)
45
    {
46
47
        // load the product with the passed SKU and merge it with the attributes
48 1
        if ($entity = $this->loadProduct($attr[MemberNames::SKU])) {
49 1
            return $this->mergeEntity($entity, $attr);
50
        }
51
52
        // otherwise simply return the attributes
53
        return $attr;
54
    }
55
56
    /**
57
     * Load's and return's the product with the passed SKU.
58
     *
59
     * @param string $sku The SKU of the product to load
60
     *
61
     * @return array The product
62
     */
63 1
    protected function loadProduct($sku)
64
    {
65 1
        return $this->getSubject()->loadProduct($sku);
66
    }
67
}
68