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

ProductUpdateObserver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 34
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeProduct() 0 11 2
A loadProduct() 0 4 1
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