1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Bundle\Ee\Observers\EeBundleOptionObserver |
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-ee |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Bundle\Ee\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
24
|
|
|
use TechDivision\Import\Observers\StateDetectorInterface; |
25
|
|
|
use TechDivision\Import\Observers\AttributeLoaderInterface; |
26
|
|
|
use TechDivision\Import\Observers\EntityMergers\EntityMergerInterface; |
27
|
|
|
use TechDivision\Import\Product\Bundle\Utils\MemberNames; |
28
|
|
|
use TechDivision\Import\Product\Bundle\Observers\BundleSelectionObserver; |
29
|
|
|
use TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface; |
30
|
|
|
use TechDivision\Import\Product\Bundle\Ee\Actions\SequenceProductBundleSelectionActionInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Oberserver that provides functionality for the bundle selection replace operation for the |
34
|
|
|
* Magento 2 EE edition. |
35
|
|
|
* |
36
|
|
|
* @author Tim Wagner <[email protected]> |
37
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
38
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
39
|
|
|
* @link https://github.com/techdivision/import-product-bundle-ee |
40
|
|
|
* @link http://www.techdivision.com |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
class EeBundleSelectionObserver extends BundleSelectionObserver |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The sequence product bundle selection action instance. |
47
|
|
|
* |
48
|
|
|
* @var \TechDivision\Import\Product\Bundle\Ee\Actions\SequenceProductBundleSelectionActionInterface |
49
|
|
|
*/ |
50
|
|
|
protected $sequenceProductBundleSelectionAction; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize the observer with the passed product bundle processor instance. |
54
|
|
|
* |
55
|
|
|
* @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
56
|
|
|
* @param \TechDivision\Import\Product\Bundle\Ee\Actions\SequenceProductBundleSelectionActionInterface $sequenceProductBundleSelectionAction The action instance |
57
|
|
|
* @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
58
|
|
|
* @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface $entityMerger The entity merger instance |
59
|
|
|
* @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
ProductBundleProcessorInterface $productBundleProcessor, |
63
|
|
|
SequenceProductBundleSelectionActionInterface $sequenceProductBundleSelectionAction, |
64
|
|
|
AttributeLoaderInterface $attributeLoader = null, |
65
|
|
|
EntityMergerInterface $entityMerger = null, |
66
|
|
|
StateDetectorInterface $stateDetector = null |
67
|
|
|
) { |
68
|
|
|
|
69
|
|
|
// initialize the parent instance |
70
|
|
|
parent::__construct($productBundleProcessor, $attributeLoader, $entityMerger, $stateDetector); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// set the passed sequence product bundle selection action instance |
73
|
|
|
$this->sequenceProductBundleSelectionAction = $sequenceProductBundleSelectionAction; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
78
|
|
|
* |
79
|
|
|
* @return array The prepared attributes |
80
|
|
|
*/ |
81
|
|
|
protected function prepareAttributes() |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
// prepare the attributes |
85
|
|
|
$attr = parent::prepareAttributes(); |
86
|
|
|
|
87
|
|
|
// query whether or not, we found a new product bundle selection |
88
|
|
|
if ($attr[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_CREATE) { |
89
|
|
|
$attr[MemberNames::SELECTION_ID] = $this->nextIdentifier(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// return the attributes |
93
|
|
|
return $attr; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the sequence product bundle selection action instance. |
98
|
|
|
* |
99
|
|
|
* @return \TechDivision\Import\Product\Bundle\Ee\Actions\SequenceProductBundleSelectionActionInterface The action instance |
100
|
|
|
*/ |
101
|
|
|
protected function getSequenceProductBundleSelectionAction() |
102
|
|
|
{ |
103
|
|
|
return $this->sequenceProductBundleSelectionAction; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the next available product bundle option ID. |
108
|
|
|
* |
109
|
|
|
* @return integer The next available product bundle option ID |
110
|
|
|
*/ |
111
|
|
|
protected function nextIdentifier() |
112
|
|
|
{ |
113
|
|
|
return $this->getSequenceProductBundleSelectionAction()->nextIdentifier(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the row ID for the passed SKU. |
118
|
|
|
* |
119
|
|
|
* @param string $sku The SKU to return the row ID for |
120
|
|
|
* |
121
|
|
|
* @return integer The mapped row ID |
122
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
123
|
|
|
*/ |
124
|
|
|
protected function mapSku($sku) |
125
|
|
|
{ |
126
|
|
|
return $this->getSubject()->mapSkuToRowId($sku); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.