-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAbstractSelectCallback.php
More file actions
110 lines (97 loc) · 3.86 KB
/
AbstractSelectCallback.php
File metadata and controls
110 lines (97 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* TechDivision\Import\Callbacks\SelectCallback
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Callbacks;
use TechDivision\Import\Utils\MemberNames;
use TechDivision\Import\Utils\RegistryKeys;
use TechDivision\Import\Utils\StoreViewCodes;
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface;
/**
* A callback implementation that converts the passed select value.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
abstract class AbstractSelectCallback extends AbstractEavAwareCallback
{
/**
* Will be invoked by a observer it has been registered for.
*
* @param \TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface|null $observer The observer
*
* @return mixed The modified value
*/
public function handle(?AttributeCodeAndValueAwareObserverInterface $observer = null)
{
// set the observer
$this->setObserver($observer);
// load the attribute code and value
$attributeCode = $observer->getAttributeCode();
$attributeValue = $observer->getAttributeValue();
// query whether or not a value for the attibute with the diven code has been set
if ($attributeValue == null || $attributeValue === '') {
return;
}
// load the store ID
$storeId = $this->getStoreId(StoreViewCodes::ADMIN);
// try to load the attribute option value and return the option ID
if ($eavAttributeOptionValue = $this->loadAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($this->getEntityTypeId(), $attributeCode, $storeId, $attributeValue)) {
return $eavAttributeOptionValue[MemberNames::OPTION_ID];
}
$message = sprintf(
'Can\'t find select option value "%s" for attribute "%s"',
$attributeValue,
$attributeCode
);
// query whether or not we're in strict mode
if (!$this->isStrictMode()) {
// log a warning and return immediately
$this->getSystemLogger()->warning(
$this->appendExceptionSuffix($message)
);
// add the missing option value to the registry
$this->mergeAttributesRecursive(
array(
RegistryKeys::MISSING_OPTION_VALUES => array(
$attributeCode => array(
$attributeValue => array(
$this->raiseCounter($attributeValue),
array($this->getUniqueIdentifier() => true)
)
)
)
)
);
$this->getSubject()->mergeStatus(
array(
RegistryKeys::NO_STRICT_VALIDATIONS => array(
basename($this->getSubject()->getFilename()) => array(
$this->getSubject()->getLineNumber() => array(
$attributeCode => $message
)
)
)
)
);
// return NULL, if the value can't be mapped to an option
return;
}
// throw an exception if the attribute is NOT
// available and we're in strict mode
throw new \Exception(
$this->appendExceptionSuffix($message)
);
}
}