Completed
Push — master ( bce332...18fde1 )
by Tim
9s
created

ProductWebsiteObserver::handle()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 37
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 11.1035

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 6
cts 16
cp 0.375
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 7
nop 1
crap 11.1035
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductWebsiteObserver
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\ColumnKeys;
24
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
25
26
/**
27
 * Observer that creates/updates the product's website relations.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product
33
 * @link      http://www.techdivision.com
34
 */
35
class ProductWebsiteObserver extends AbstractProductImportObserver
36
{
37
38
    /**
39
     * Will be invoked by the action on the events the listener has been registered for.
40
     *
41
     * @param array $row The row to handle
42
     *
43
     * @return array The modified row
44
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
45
     */
46 1
    public function handle(array $row)
47
    {
48
49
        // load the header information
50 1
        $headers = $this->getHeaders();
51
52
        // query whether or not, we've found a new SKU => means we've found a new product
53 1
        if ($this->isLastSku($row[$headers[ColumnKeys::SKU]])) {
54
            return $row;
55
        }
56
57
        // query whether or not, product => website relations has been specified
58 1
        if (isset($row[$headers[ColumnKeys::PRODUCT_WEBSITES]])) {
59
            $productWebsites = $row[$headers[ColumnKeys::PRODUCT_WEBSITES]];
60
        }
61
62
        // if not, we dont do anything
63 1
        if (empty($productWebsites)) {
64 1
            return $row;
65
        }
66
67
        // load the ID of the product that has been created recently
68
        $lastEntityId = $this->getLastEntityId();
69
70
        // append the product => website relations found
71
        $codes = explode(',', $productWebsites);
72
        foreach ($codes as $code) {
73
            // load the website ID to relate the product with
74
            $websiteId = $this->getStoreWebsiteIdByCode($code);
75
76
            // create the product website relation
77
            $this->persistProductWebsite(array($lastEntityId, $websiteId));
78
        }
79
80
        // returns the row
81
        return $row;
82
    }
83
84
    /**
85
     * Persist's the passed product website data and return's the ID.
86
     *
87
     * @param array $productWebsite The product website data to persist
88
     *
89
     * @return void
90
     */
91
    public function persistProductWebsite($productWebsite)
92
    {
93
        $this->getSubject()->persistProductWebsite($productWebsite);
94
    }
95
96
    /**
97
     * Return's the store website for the passed code.
98
     *
99
     * @param string $code The code of the store website to return the ID for
100
     *
101
     * @return integer The store website ID
102
     */
103
    public function getStoreWebsiteIdByCode($code)
104
    {
105
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
106
    }
107
}
108