1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Bundle\Observers\BundleOptionUpdateObserver |
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-bundle |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Bundle\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
24
|
|
|
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Product\Bundle\Utils\MemberNames; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Oberserver that provides functionality for the bundle option add/update operation. |
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-bundle |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
class BundleOptionUpdateObserver extends BundleOptionObserver |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initialize the bundle option with the passed attributes and returns an instance. |
41
|
|
|
* |
42
|
|
|
* @param array $attr The bundle option attributes |
43
|
|
|
* |
44
|
|
|
* @return array The initialized bundle option |
45
|
|
|
*/ |
46
|
|
|
protected function initializeBundleOption(array $attr) |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
// load the parent ID, the name and the store ID |
50
|
|
|
$parentId = $attr[MemberNames::PARENT_ID]; |
51
|
|
|
$name = $this->getValue(ColumnKeys::BUNDLE_VALUE_NAME); |
|
|
|
|
52
|
|
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
53
|
|
|
|
54
|
|
|
// try to load the bundle option with the passed name/store/parent ID |
55
|
|
|
if ($entity = $this->loadBundleOption($name, $storeId, $parentId)) { |
56
|
|
|
return $this->mergeEntity($entity, $attr); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// simply return the attributes |
60
|
|
|
return $attr; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Load's the bundle option with the passed name, store + parent ID. |
65
|
|
|
* |
66
|
|
|
* @param string $title The title of the bundle option to be returned |
67
|
|
|
* @param integer $storeId The store ID of the bundle option to be returned |
68
|
|
|
* @param integer $parentId The entity of the product the bundle option is related with |
69
|
|
|
* |
70
|
|
|
* @return array The bundle option |
71
|
|
|
*/ |
72
|
|
|
protected function loadBundleOption($title, $storeId, $parentId) |
73
|
|
|
{ |
74
|
|
|
return $this->getSubject()->loadBundleOption($title, $storeId, $parentId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return's the store ID of the actual row, or of the default store |
79
|
|
|
* if no store view code is set in the CSV file. |
80
|
|
|
* |
81
|
|
|
* @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
82
|
|
|
* |
83
|
|
|
* @return integer The ID of the actual store |
84
|
|
|
* @throws \Exception Is thrown, if the store with the actual code is not available |
85
|
|
|
*/ |
86
|
|
|
protected function getRowStoreId($default = null) |
87
|
|
|
{ |
88
|
|
|
return $this->getSubject()->getRowStoreId($default); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.