1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Callbacks\AbstractMultiselectCallback |
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 |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Callbacks; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\MemberNames; |
24
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
25
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
26
|
|
|
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A callback implementation that converts the passed multiselect value. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
abstract class AbstractMultiselectCallback extends AbstractEavAwareCallback |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Will be invoked by a observer it has been registered for. |
42
|
|
|
* |
43
|
|
|
* @param \TechDivision\Import\Observers\ObserverInterface $observer The observer |
44
|
|
|
* |
45
|
|
|
* @return mixed The modified value |
46
|
|
|
*/ |
47
|
|
|
public function handle(AttributeCodeAndValueAwareObserverInterface $observer) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
// set the observer |
51
|
|
|
$this->setObserver($observer); |
52
|
|
|
|
53
|
|
|
// load the attribute code and value |
54
|
|
|
$attributeCode = $observer->getAttributeCode(); |
55
|
|
|
$attributeValue = $observer->getAttributeValue(); |
56
|
|
|
|
57
|
|
|
// load the ID of the actual store |
58
|
|
|
$storeId = $this->getStoreId(StoreViewCodes::ADMIN); |
59
|
|
|
|
60
|
|
|
// explode the multiselect values |
61
|
|
|
$vals = explode('|', $attributeValue); |
62
|
|
|
|
63
|
|
|
// initialize the array for the mapped values |
64
|
|
|
$mappedValues = array(); |
65
|
|
|
|
66
|
|
|
// convert the option values into option value ID's |
67
|
|
|
foreach ($vals as $val) { |
68
|
|
|
// try to load the attribute option value and add the option ID |
69
|
|
|
if ($eavAttributeOptionValue = $this->loadAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($this->getEntityTypeId(), $attributeCode, $storeId, $val)) { |
70
|
|
|
$mappedValues[] = $eavAttributeOptionValue[MemberNames::OPTION_ID]; |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// query whether or not we're in debug mode |
75
|
|
View Code Duplication |
if ($this->isDebugMode()) { |
|
|
|
|
76
|
|
|
// log a warning and continue with the next value |
77
|
|
|
$this->getSystemLogger()->warning( |
78
|
|
|
$this->appendExceptionSuffix( |
79
|
|
|
sprintf( |
80
|
|
|
'Can\'t find multiselect option value "%s" for attribute %s', |
81
|
|
|
$val, |
82
|
|
|
$attributeCode |
83
|
|
|
) |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
// add the missing option value to the registry |
88
|
|
|
$this->mergeAttributesRecursive( |
89
|
|
|
array( |
90
|
|
|
RegistryKeys::MISSING_OPTION_VALUES => array( |
91
|
|
|
$attributeCode => array( |
92
|
|
|
$val => array( |
93
|
|
|
$this->raiseCounter($val), |
94
|
|
|
array($this->getUniqueIdentifier() => true) |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
) |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// continue with the next option value |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// throw an exception if the attribute is not available |
106
|
|
|
throw new \Exception( |
107
|
|
|
$this->appendExceptionSuffix( |
108
|
|
|
sprintf( |
109
|
|
|
'Can\'t find multiselect option value "%s" for attribute %s', |
110
|
|
|
$val, |
111
|
|
|
$attributeCode |
112
|
|
|
) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// return NULL, if NO value can be mapped to an option |
118
|
|
|
if (sizeof($mappedValues) === 0) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// re-concatenate and return the values |
123
|
|
|
return implode(',', $mappedValues); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.