Completed
Push — 8.x ( 824af6 )
by Tim
09:11
created

AbstractSelectCallback::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\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 store ID
58
        $storeId = $this->getStoreId(StoreViewCodes::ADMIN);
59
60
        // try to load the attribute option value and return the option ID
61
        if ($eavAttributeOptionValue = $this->loadAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($this->getEntityTypeId(), $attributeCode, $storeId, $attributeValue)) {
62
            return $eavAttributeOptionValue[MemberNames::OPTION_ID];
63
        }
64
65
        // query whether or not we're in debug mode
66 View Code Duplication
        if ($this->isDebugMode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
67
            // log a warning and return immediately
68
            $this->getSystemLogger()->warning(
69
                $this->appendExceptionSuffix(
70
                    sprintf(
71
                        'Can\'t find select option value "%s" for attribute %s',
72
                        $attributeValue,
73
                        $attributeCode
74
                    )
75
                )
76
            );
77
78
            // add the missing option value to the registry
79
            $this->mergeAttributesRecursive(
80
                array(
81
                    RegistryKeys::MISSING_OPTION_VALUES => array(
82
                        $attributeCode => array(
83
                            $attributeValue => array(
84
                                $this->raiseCounter($attributeValue),
85
                                array($this->getUniqueIdentifier() => true)
86
                            )
87
                        )
88
                    )
89
                )
90
            );
91
92
            // return NULL, if the value can't be mapped to an option
93
            return;
94
        }
95
96
        // throw an exception if the attribute is NOT
97
        // available and we're not in debug mode
98
        throw new \Exception(
99
            $this->appendExceptionSuffix(
100
                sprintf(
101
                    'Can\'t find select option value "%s" for attribute %s',
102
                    $attributeValue,
103
                    $attributeCode
104
                )
105
            )
106
        );
107
    }
108
}
109