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

ProductWebsiteObserver::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 0
crap 12
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
    public 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
    public function getCode()
64
    {
65
        return $this->code;
66
    }
67
68
    /**
69
     * Will be invoked by the action on the events the listener has been registered for.
70
     *
71
     * @param array $row The row to handle
72
     *
73
     * @return array The modified row
74
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
75
     */
76 1
    public function handle(array $row)
77
    {
78
79
        // initialize the row
80 1
        $this->setRow($row);
81
82
        // query whether or not, we've found a new SKU => means we've found a new product
83 1
        if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) {
84
            return $this->getRow();
85
        }
86
87
        // query whether or not, product => website relations has been specified
88 1
        if (!$this->hasValue(ColumnKeys::PRODUCT_WEBSITES)) {
89 1
            return $this->getRow();
90
        }
91
92
        // process the functionality and return the row
93
        $this->process();
94
95
        // return the processed row
96
        return $this->getRow();
97
    }
98
99
    /**
100
     * Process the observer's business logic.
101
     *
102
     * @return array The processed row
103
     */
104
    public function process()
105
    {
106
107
        // append the product => website relations found
108
        $codes = explode(',', $this->getValue(ColumnKeys::PRODUCT_WEBSITES));
109
        foreach ($codes as $code) {
110
            // set the code of the website that has to be processed
111
            $this->setCode($code);
112
            // prepare the product website relation attributes
113
            $attr = $this->prepareAttributes();
114
115
            try {
116
                // create the product website relation
117
                $productWebsite = $this->initializeProductWebsite($attr);
118
                $this->persistProductWebsite($productWebsite);
119
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
120
            } catch (\RuntimeException $re) {
121
                $this->getSystemLogger()->debug($re->getMessage());
122
            }
123
        }
124
    }
125
126
    /**
127
     * Prepare the attributes of the entity that has to be persisted.
128
     *
129
     * @return array The prepared attributes
130
     */
131
    public function prepareAttributes()
132
    {
133
134
        // load the ID of the product that has been created recently
135
        $lastEntityId = $this->getLastEntityId();
136
137
        // load the website ID to relate the product with
138
        $websiteId = $this->getStoreWebsiteIdByCode($this->getCode());
139
140
        // return the prepared product
141
        return $this->initializeEntity(
142
            array(
143
                MemberNames::PRODUCT_ID => $lastEntityId,
144
                MemberNames::WEBSITE_ID => $websiteId
145
            )
146
        );
147
    }
148
149
    /**
150
     * Initialize the product website with the passed attributes and returns an instance.
151
     *
152
     * @param array $attr The product website attributes
153
     *
154
     * @return array The initialized product website
155
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
156
     */
157
    public function initializeProductWebsite(array $attr)
158
    {
159
        return $attr;
160
    }
161
162
    /**
163
     * Persist's the passed product website data and return's the ID.
164
     *
165
     * @param array $productWebsite The product website data to persist
166
     *
167
     * @return void
168
     */
169
    public function persistProductWebsite($productWebsite)
170
    {
171
        $this->getSubject()->persistProductWebsite($productWebsite);
172
    }
173
174
    /**
175
     * Return's the store website for the passed code.
176
     *
177
     * @param string $code The code of the store website to return the ID for
178
     *
179
     * @return integer The store website ID
180
     */
181
    public function getStoreWebsiteIdByCode($code)
182
    {
183
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
184
    }
185
}
186