Completed
Push — master ( 5f77a6...813b3f )
by Tim
12s
created

AbstractSelectCallback::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 61
Code Lines 28

Duplication

Lines 29
Ratio 47.54 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 29
loc 61
ccs 0
cts 43
cp 0
rs 9.5147
cc 3
eloc 28
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\Services\EavAwareProcessorInterface;
27
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface;
28
29
/**
30
 * A callback implementation that converts the passed select value.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import
36
 * @link      http://www.techdivision.com
37
 */
38
abstract class AbstractSelectCallback extends AbstractCallback
39
{
40
41
    /**
42
     * The EAV aware processor.
43
     *
44
     * @var \TechDivision\Import\Services\EavAwareProcessorInterface
45
     */
46
    protected $eavAwareProcessor;
47
48
    /**
49
     * Initialize the callback with the passed processor instance.
50
     *
51
     * @param \TechDivision\Import\Services\EavAwareProcessorInterface $eavAwareProcessor The processor instance
52
     */
53
    public function __construct(EavAwareProcessorInterface $eavAwareProcessor)
54
    {
55
        $this->eavAwareProcessor = $eavAwareProcessor;
56
    }
57
58
    /**
59
     * Will be invoked by a observer it has been registered for.
60
     *
61
     * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer
62
     *
63
     * @return mixed The modified value
64
     */
65
    public function handle(AttributeCodeAndValueAwareObserverInterface $observer)
66
    {
67
68
        // set the observer
69
        $this->setObserver($observer);
70
71
        // load the attribute code and value
72
        $attributeCode = $observer->getAttributeCode();
73
        $attributeValue = $observer->getAttributeValue();
74
75
        // load the store ID
76
        $storeId = $this->getStoreId(StoreViewCodes::ADMIN);
77
78
        // try to load the attribute option value and return the option ID
79
        if ($eavAttributeOptionValue = $this->loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $attributeValue)) {
80
            return $eavAttributeOptionValue[MemberNames::OPTION_ID];
81
        }
82
83
        // query whether or not we're in debug mode
84 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...
85
            // log a warning and return immediately
86
            $this->getSystemLogger()->warning(
87
                $this->appendExceptionSuffix(
88
                    sprintf(
89
                        'Can\'t find select option value "%s" for attribute %s',
90
                        $attributeValue,
91
                        $attributeCode
92
                    )
93
                )
94
            );
95
96
            // add the missing option value to the registry
97
            $this->mergeAttributesRecursive(
98
                array(
99
                    RegistryKeys::MISSING_OPTION_VALUES => array(
100
                        $attributeCode => array(
101
                            $attributeValue => array(
102
                                $this->raiseCounter($attributeValue),
103
                                array($this->getUniqueIdentifier() => true)
104
                            )
105
                        )
106
                    )
107
                )
108
            );
109
110
            // return NULL, if the value can't be mapped to an option
111
            return;
112
        }
113
114
        // throw an exception if the attribute is NOT
115
        // available and we're not in debug mode
116
        throw new \Exception(
117
            $this->appendExceptionSuffix(
118
                sprintf(
119
                    'Can\'t find select option value "%s" for attribute %s',
120
                    $attributeValue,
121
                    $attributeCode
122
                )
123
            )
124
        );
125
    }
126
127
    /**
128
     * Return's the EAV aware processor instance.
129
     *
130
     * @return \TechDivision\Import\Services\EavAwareProcessorInterface The processor instance
131
     */
132
    protected function getEavAwareProcessor()
133
    {
134
        return $this->eavAwareProcessor;
135
    }
136
137
    /**
138
     * Load's and return's the EAV attribute option value with the passed code, store ID and value.
139
     *
140
     * @param string  $attributeCode The code of the EAV attribute option to load
141
     * @param integer $storeId       The store ID of the attribute option to load
142
     * @param string  $value         The value of the attribute option to load
143
     *
144
     * @return array The EAV attribute option value
145
     */
146
    protected function loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value)
147
    {
148
        return $this->getEavAwareProcessor()->loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value);
149
    }
150
}
151