Completed
Push — master ( 404f67...9bc8e3 )
by Tim
10s
created

ProductWebsiteObserver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 129
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setCode() 0 4 1
A getCode() 0 4 1
B process() 0 31 5
A prepareAttributes() 0 17 1
A initializeProductWebsite() 0 4 1
A persistProductWebsite() 0 4 1
A getStoreWebsiteIdByCode() 0 4 1
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
use TechDivision\Import\Product\Utils\MemberNames;
26
27
/**
28
 * Observer that creates/updates the product's website relations.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 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
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductWebsiteObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The actual website code that has to be processed.
41
     *
42
     * @var string
43
     */
44
    protected $code;
45
46
    /**
47
     * Set's the actual website code that has to be processed.
48
     *
49
     * @param string $code The website code
50
     *
51
     * @return void
52
     */
53
    protected function setCode($code)
54
    {
55
        $this->code = $code;
56
    }
57
58
    /**
59
     * Return's the webiste code that has to be processed.
60
     *
61
     * @return string The website code
62
     */
63
    protected function getCode()
64
    {
65
        return $this->code;
66
    }
67
68
    /**
69
     * Process the observer's business logic.
70
     *
71
     * @return array The processed row
72
     */
73 1
    protected function process()
74
    {
75
76
        // query whether or not, we've found a new SKU => means we've found a new product
77 1
        if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) {
78
            return;
79
        }
80
81
        // query whether or not, product => website relations has been specified
82 1
        if (!$this->hasValue(ColumnKeys::PRODUCT_WEBSITES)) {
83 1
            return;
84
        }
85
86
        // append the product => website relations found
87
        $codes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode'));
88
        foreach ($codes as $code) {
89
            // set the code of the website that has to be processed
90
            $this->setCode($code);
91
            // prepare the product website relation attributes
92
            $attr = $this->prepareAttributes();
93
94
            try {
95
                // create the product website relation
96
                $productWebsite = $this->initializeProductWebsite($attr);
97
                $this->persistProductWebsite($productWebsite);
98
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
99
            } catch (\RuntimeException $re) {
100
                $this->getSystemLogger()->debug($re->getMessage());
101
            }
102
        }
103
    }
104
105
    /**
106
     * Prepare the attributes of the entity that has to be persisted.
107
     *
108
     * @return array The prepared attributes
109
     */
110
    protected function prepareAttributes()
111
    {
112
113
        // load the ID of the product that has been created recently
114
        $lastEntityId = $this->getLastEntityId();
115
116
        // load the website ID to relate the product with
117
        $websiteId = $this->getStoreWebsiteIdByCode($this->getCode());
118
119
        // return the prepared product
120
        return $this->initializeEntity(
121
            array(
122
                MemberNames::PRODUCT_ID => $lastEntityId,
123
                MemberNames::WEBSITE_ID => $websiteId
124
            )
125
        );
126
    }
127
128
    /**
129
     * Initialize the product website with the passed attributes and returns an instance.
130
     *
131
     * @param array $attr The product website attributes
132
     *
133
     * @return array The initialized product website
134
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
135
     */
136
    protected function initializeProductWebsite(array $attr)
137
    {
138
        return $attr;
139
    }
140
141
    /**
142
     * Persist's the passed product website data and return's the ID.
143
     *
144
     * @param array $productWebsite The product website data to persist
145
     *
146
     * @return void
147
     */
148
    protected function persistProductWebsite($productWebsite)
149
    {
150
        $this->getSubject()->persistProductWebsite($productWebsite);
151
    }
152
153
    /**
154
     * Return's the store website for the passed code.
155
     *
156
     * @param string $code The code of the store website to return the ID for
157
     *
158
     * @return integer The store website ID
159
     */
160
    protected function getStoreWebsiteIdByCode($code)
161
    {
162
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
163
    }
164
}
165