Completed
Pull Request — master (#8)
by Tim
02:57
created

addOptionValueStoreMapping()   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
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Observers\BundleOptionValueObserver
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-product-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Bundle\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Oberserver that provides functionality for the bundle option value replace operation.
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-product-bundle
35
 * @link      http://www.techdivision.com
36
 */
37
class BundleOptionValueObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The option value store mapping.
42
     *
43
     * @var array
44
     */
45
    protected $optionValueStoreMapping = array();
46
47
    /**
48
     * Process the observer's business logic.
49
     *
50
     * @return array The processed row
51
     */
52
    protected function process()
53
    {
54
55
        // prepare the store view code
56
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Unused Code introduced by
The call to BundleOptionValueObserver::prepareStoreViewCode() has too many arguments starting with $this->getRow().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
58
        // prepare the attributes
59
        $attr = $this->prepareAttributes();
60
61
        // load store/option ID
62
        $storeId = $attr[MemberNames::STORE_ID];
63
        $optionId = $attr[MemberNames::OPTION_ID];
64
65
        // if the store has already been mapped, return immediately
66
        if ($this->isMapped($optionId, $storeId)) {
67
            return;
68
        }
69
70
        // initialize and save the product bundle option value
71
        if ($bundleOption = $this->initializeBundleOptionValue($attr)) {
72
            $this->persistProductBundleOptionValue($bundleOption);
73
        }
74
75
        // add the store => option mapping
76
        $this->addOptionValueStoreMapping($optionId, $storeId);
77
    }
78
79
    /**
80
     * Prepare the attributes of the entity that has to be persisted.
81
     *
82
     * @return array The prepared attributes
83
     */
84 View Code Duplication
    protected function prepareAttributes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
86
87
        // load the product bundle option name
88
        $name = $this->getValue(ColumnKeys::BUNDLE_VALUE_NAME);
89
90
        // load the actual option ID
91
        $optionId = $this->getLastOptionId();
92
93
        // load the store/website ID
94
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
95
        $storeId = $store[MemberNames::STORE_ID];
96
97
        // return the prepared product
98
        return $this->initializeEntity(
99
            array(
100
                MemberNames::OPTION_ID => $optionId,
101
                MemberNames::STORE_ID  => $storeId,
102
                MemberNames::TITLE     => $name
103
            )
104
        );
105
    }
106
107
    /**
108
     * Initialize the bundle option value with the passed attributes and returns an instance.
109
     *
110
     * @param array $attr The bundle option value attributes
111
     *
112
     * @return array The initialized bundle option value
113
     */
114
    protected function initializeBundleOptionValue(array $attr)
115
    {
116
        return $attr;
117
    }
118
119
    /**
120
     * Add the store => option mapping.
121
     *
122
     * @param integer $optionId The option ID to map
123
     * @param integer $storeId  The store ID to map
124
     *
125
     * @return void
126
     */
127
    protected function addOptionValueStoreMapping($optionId, $storeId)
128
    {
129
        $this->optionValueStoreMapping[$optionId][] = $storeId;
130
    }
131
132
    /**
133
     * Query whether or not the passed option/store ID combination has already been mapped.
134
     *
135
     * @param integer $optionId The option ID to map
136
     * @param integer $storeId  The store ID to map
137
     *
138
     * @return boolean TRUE if the combination has already been mapped, else FALSE
139
     */
140
    protected function isMapped($optionId, $storeId)
141
    {
142
        return (isset($this->optionValueStoreMapping[$optionId]) && in_array($storeId, $this->optionValueStoreMapping[$optionId]));
143
    }
144
145
    /**
146
     * Return's the last created option ID.
147
     *
148
     * @return integer $optionId The last created option ID
149
     */
150
    protected function getLastOptionId()
151
    {
152
        return $this->getSubject()->getLastOptionId();
153
    }
154
155
    /**
156
     * Return's the option ID for the passed name.
157
     *
158
     * @param string $name The name to return the option ID for
159
     *
160
     * @return integer The option ID for the passed name
161
     * @throws \Exception Is thrown, if no option ID for the passed name is available
162
     */
163
    protected function getOptionIdForName($name)
164
    {
165
        return $this->getSubject()->getOptionIdForName($name);
166
    }
167
168
    /**
169
     * Return's the store for the passed store code.
170
     *
171
     * @param string $storeCode The store code to return the store for
172
     *
173
     * @return array The requested store
174
     * @throws \Exception Is thrown, if the requested store is not available
175
     */
176
    protected function getStoreByStoreCode($storeCode)
177
    {
178
        return $this->getSubject()->getStoreByStoreCode($storeCode);
179
    }
180
181
    /**
182
     * Persist's the passed product bundle option value data.
183
     *
184
     * @param array $productBundleOptionValue The product bundle option value data to persist
185
     *
186
     * @return void
187
     */
188
    protected function persistProductBundleOptionValue($productBundleOptionValue)
189
    {
190
        $this->getSubject()->persistProductBundleOptionValue($productBundleOptionValue);
191
    }
192
}
193