Completed
Pull Request — master (#87)
by Tim
03:23
created

AbstractMultiselectCallback::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\AbstractMultiselectCallback
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 multiselect 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 AbstractMultiselectCallback 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
        // explode the multiselect values
76
        $vals = explode('|', $attributeValue);
77
78
        // initialize the array for the mapped values
79
        $mappedValues = array();
80
81
        // convert the option values into option value ID's
82
        foreach ($vals as $val) {
83
            // load the ID of the actual store
84
            $storeId = $this->getStoreId(StoreViewCodes::ADMIN);
85
86
            // try to load the attribute option value and add the option ID
87
            if ($eavAttributeOptionValue = $this->loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $val)) {
88
                $mappedValues[] = $eavAttributeOptionValue[MemberNames::OPTION_ID];
89
                continue;
90
            }
91
92
            // query whether or not we're in debug mode
93 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...
94
                // log a warning and continue with the next value
95
                $this->getSystemLogger()->warning(
96
                    $this->appendExceptionSuffix(
97
                        sprintf(
98
                            'Can\'t find multiselect option value "%s" for attribute %s',
99
                            $val,
100
                            $attributeCode
101
                        )
102
                    )
103
                );
104
105
                // add the missing option value to the registry
106
                $this->mergeAttributesRecursive(
107
                    array(
108
                        RegistryKeys::MISSING_OPTION_VALUES => array(
109
                            $attributeCode => array(
110
                                $val => array(
111
                                    $this->raiseCounter($val),
112
                                    array($this->getUniqueIdentifier() => true)
113
                                )
114
                            )
115
                        )
116
                    )
117
                );
118
119
                // continue with the next option value
120
                continue;
121
            }
122
123
            // throw an exception if the attribute is not available
124
            throw new \Exception(
125
                $this->appendExceptionSuffix(
126
                    sprintf(
127
                        'Can\'t find multiselect option value "%s" for attribute %s',
128
                        $val,
129
                        $attributeCode
130
                    )
131
                )
132
            );
133
        }
134
135
        // return NULL, if NO value can be mapped to an option
136
        if (sizeof($mappedValues) === 0) {
137
            return;
138
        }
139
140
        // re-concatenate and return the values
141
        return implode(',', $mappedValues);
142
    }
143
144
    /**
145
     * Return's the EAV aware processor instance.
146
     *
147
     * @return \TechDivision\Import\Services\EavAwareProcessorInterface The processor instance
148
     */
149
    protected function getEavAwareProcessor()
150
    {
151
        return $this->eavAwareProcessor;
152
    }
153
154
    /**
155
     * Return's the store ID of the actual row, or of the default store
156
     * if no store view code is set in the CSV file.
157
     *
158
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
159
     *
160
     * @return integer The ID of the actual store
161
     * @throws \Exception Is thrown, if the store with the actual code is not available
162
     */
163
    protected function getRowStoreId($default = null)
164
    {
165
        return $this->getSubject()->getRowStoreId($default);
166
    }
167
168
    /**
169
     * Load's and return's the EAV attribute option value with the passed code, store ID and value.
170
     *
171
     * @param string  $attributeCode The code of the EAV attribute option to load
172
     * @param integer $storeId       The store ID of the attribute option to load
173
     * @param string  $value         The value of the attribute option to load
174
     *
175
     * @return array The EAV attribute option value
176
     */
177
    protected function loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value)
178
    {
179
        return $this->getEavAwareProcessor()->loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value);
180
    }
181
}
182