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

AbstractSelectCallback::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 61

Duplication

Lines 29
Ratio 47.54 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 29
loc 61
ccs 0
cts 43
cp 0
rs 8.8509
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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