These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface; |
||
27 | |||
28 | /** |
||
29 | * A callback implementation that converts the passed select 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 AbstractSelectCallback extends AbstractEavAwareCallback |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Will be invoked by a observer it has been registered for. |
||
42 | * |
||
43 | * @param \TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface|null $observer The observer |
||
44 | * |
||
45 | * @return mixed The modified value |
||
46 | */ |
||
47 | public function handle(AttributeCodeAndValueAwareObserverInterface $observer = null) |
||
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 | // query whether or not a value for the attibute with the diven code has been set |
||
58 | if ($attributeValue == null || $attributeValue === '') { |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | // load the store ID |
||
63 | $storeId = $this->getStoreId(StoreViewCodes::ADMIN); |
||
64 | |||
65 | // try to load the attribute option value and return the option ID |
||
66 | if ($eavAttributeOptionValue = $this->loadAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($this->getEntityTypeId(), $attributeCode, $storeId, $attributeValue)) { |
||
67 | return $eavAttributeOptionValue[MemberNames::OPTION_ID]; |
||
68 | } |
||
69 | |||
70 | // query whether or not we're in debug mode |
||
71 | View Code Duplication | if ($this->isDebugMode()) { |
|
0 ignored issues
–
show
|
|||
72 | // log a warning and return immediately |
||
73 | $this->getSystemLogger()->warning( |
||
74 | $this->appendExceptionSuffix( |
||
75 | sprintf( |
||
76 | 'Can\'t find select option value "%s" for attribute "%s"', |
||
77 | $attributeValue, |
||
78 | $attributeCode |
||
79 | ) |
||
80 | ) |
||
81 | ); |
||
82 | |||
83 | // add the missing option value to the registry |
||
84 | $this->mergeAttributesRecursive( |
||
85 | array( |
||
86 | RegistryKeys::MISSING_OPTION_VALUES => array( |
||
87 | $attributeCode => array( |
||
88 | $attributeValue => array( |
||
89 | $this->raiseCounter($attributeValue), |
||
90 | array($this->getUniqueIdentifier() => true) |
||
91 | ) |
||
92 | ) |
||
93 | ) |
||
94 | ) |
||
95 | ); |
||
96 | |||
97 | // return NULL, if the value can't be mapped to an option |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | // throw an exception if the attribute is NOT |
||
102 | // available and we're not in debug mode |
||
103 | throw new \Exception( |
||
104 | $this->appendExceptionSuffix( |
||
105 | sprintf( |
||
106 | 'Can\'t find select option value "%s" for attribute "%s"', |
||
107 | $attributeValue, |
||
108 | $attributeCode |
||
109 | ) |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | } |
||
114 |
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.