|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Callbacks\SelectCallback |
|
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
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A callback implementation that converts the passed select value. |
|
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 |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract class AbstractSelectCallback extends AbstractCallback |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Will be invoked by a observer it has been registered for. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $attributeCode The code of the attribute the passed value is for |
|
43
|
|
|
* @param mixed $attributeValue The value to handle |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed|null The modified value |
|
46
|
|
|
* @see \TechDivision\Import\Callbacks\CallbackInterface::handle() |
|
47
|
|
|
*/ |
|
48
|
|
|
public function handle($attributeCode, $attributeValue) |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
// load the store ID |
|
52
|
|
|
$storeId = $this->getStoreId(StoreViewCodes::ADMIN); |
|
53
|
|
|
|
|
54
|
|
|
// try to load the attribute option value and return the option ID |
|
55
|
|
|
if ($eavAttributeOptionValue = $this->getEavAttributeOptionValueByOptionValueAndStoreId($attributeValue, $storeId)) { |
|
56
|
|
|
return $eavAttributeOptionValue[MemberNames::OPTION_ID]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// query whether or not we're in debug mode |
|
60
|
|
View Code Duplication |
if ($this->isDebugMode()) { |
|
|
|
|
|
|
61
|
|
|
// log a warning and return immediately |
|
62
|
|
|
$this->getSystemLogger()->warning( |
|
63
|
|
|
$this->appendExceptionSuffix( |
|
64
|
|
|
sprintf( |
|
65
|
|
|
'Can\'t find select option value "%s" for attribute %s', |
|
66
|
|
|
$attributeValue, |
|
67
|
|
|
$attributeCode |
|
68
|
|
|
) |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
// add the missing option value to the registry |
|
73
|
|
|
$this->mergeAttributesRecursive( |
|
74
|
|
|
array( |
|
75
|
|
|
RegistryKeys::MISSING_OPTION_VALUES => array( |
|
76
|
|
|
$attributeCode => array( |
|
77
|
|
|
$attributeValue => array( |
|
78
|
|
|
$this->raiseCounter($attributeValue), |
|
79
|
|
|
array($this->getUniqueIdentifier() => true) |
|
80
|
|
|
) |
|
81
|
|
|
) |
|
82
|
|
|
) |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
// return NULL, if the value can't be mapped to an option |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// throw an exception if the attribute is NOT |
|
91
|
|
|
// available and we're not in debug mode |
|
92
|
|
|
throw new \Exception( |
|
93
|
|
|
$this->appendExceptionSuffix( |
|
94
|
|
|
sprintf( |
|
95
|
|
|
'Can\'t find select option value "%s" for attribute %s', |
|
96
|
|
|
$attributeValue, |
|
97
|
|
|
$attributeCode |
|
98
|
|
|
) |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return's the attribute option value with the passed value and store ID. |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $value The option value |
|
107
|
|
|
* @param integer $storeId The ID of the store |
|
108
|
|
|
* |
|
109
|
|
|
* @return array|boolean The attribute option value instance |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->getSubject()->getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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.