Completed
Push — master ( 235157...2175ac )
by Tim
10s
created

BundleOptionValueObserver::process()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
crap 12
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
     * Will be invoked by the action on the events the listener has been registered for.
49
     *
50
     * @param array $row The row to handle
51
     *
52
     * @return array The modified row
53
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
54
     */
55
    public function handle(array $row)
56
    {
57
58
        // initialize the row
59
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...dleOptionValueObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        // process the functionality and return the row
62
        $this->process();
63
64
        // return the processed row
65
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...dleOptionValueObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
68
    /**
69
     * Process the observer's business logic.
70
     *
71
     * @return array The processed row
72
     */
73
    protected function process()
74
    {
75
76
        // prepare the store view code
77
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...dleOptionValueObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
79
        // prepare the attributes
80
        $attr = $this->prepareAttributes();
81
82
        // load store/option ID
83
        $storeId = $attr[MemberNames::STORE_ID];
84
        $optionId = $attr[MemberNames::OPTION_ID];
85
86
        // if the store has already been mapped, return immediately
87
        if ($this->isMapped($optionId, $storeId)) {
88
            return;
89
        }
90
91
        // initialize and save the product bundle option value
92
        if ($bundleOption = $this->initializeBundleOptionValue($attr)) {
93
            $this->persistProductBundleOptionValue($bundleOption);
94
        }
95
96
        // add the store => option mapping
97
        $this->addOptionValueStoreMapping($optionId, $storeId);
98
    }
99
100
    /**
101
     * Prepare the attributes of the entity that has to be persisted.
102
     *
103
     * @return array The prepared attributes
104
     */
105 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...
106
    {
107
108
        // load the product bundle option name
109
        $name = $this->getValue(ColumnKeys::BUNDLE_VALUE_NAME);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...dleOptionValueObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
111
        // load the actual option ID
112
        $optionId = $this->getLastOptionId();
113
114
        // load the store/website ID
115
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
116
        $storeId = $store[MemberNames::STORE_ID];
117
118
        // return the prepared product
119
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...dleOptionValueObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
            array(
121
                MemberNames::OPTION_ID => $optionId,
122
                MemberNames::STORE_ID  => $storeId,
123
                MemberNames::TITLE     => $name
124
            )
125
        );
126
    }
127
128
    /**
129
     * Initialize the bundle option value with the passed attributes and returns an instance.
130
     *
131
     * @param array $attr The bundle option value attributes
132
     *
133
     * @return array The initialized bundle option value
134
     */
135
    protected function initializeBundleOptionValue(array $attr)
136
    {
137
        return $attr;
138
    }
139
140
    /**
141
     * Add the store => option mapping.
142
     *
143
     * @param integer $optionId The option ID to map
144
     * @param integer $storeId  The store ID to map
145
     *
146
     * @return void
147
     */
148
    protected function addOptionValueStoreMapping($optionId, $storeId)
149
    {
150
        $this->optionValueStoreMapping[$optionId][] = $storeId;
151
    }
152
153
    /**
154
     * Query whether or not the passed option/store ID combination has already been mapped.
155
     *
156
     * @param integer $optionId The option ID to map
157
     * @param integer $storeId  The store ID to map
158
     *
159
     * @return boolean TRUE if the combination has already been mapped, else FALSE
160
     */
161
    protected function isMapped($optionId, $storeId)
162
    {
163
        return (isset($this->optionValueStoreMapping[$optionId]) && in_array($storeId, $this->optionValueStoreMapping[$optionId]));
164
    }
165
166
    /**
167
     * Return's the last created option ID.
168
     *
169
     * @return integer $optionId The last created option ID
170
     */
171
    protected function getLastOptionId()
172
    {
173
        return $this->getSubject()->getLastOptionId();
174
    }
175
176
    /**
177
     * Return's the option ID for the passed name.
178
     *
179
     * @param string $name The name to return the option ID for
180
     *
181
     * @return integer The option ID for the passed name
182
     * @throws \Exception Is thrown, if no option ID for the passed name is available
183
     */
184
    protected function getOptionIdForName($name)
185
    {
186
        return $this->getSubject()->getOptionIdForName($name);
187
    }
188
189
    /**
190
     * Return's the store for the passed store code.
191
     *
192
     * @param string $storeCode The store code to return the store for
193
     *
194
     * @return array The requested store
195
     * @throws \Exception Is thrown, if the requested store is not available
196
     */
197
    protected function getStoreByStoreCode($storeCode)
198
    {
199
        return $this->getSubject()->getStoreByStoreCode($storeCode);
200
    }
201
202
    /**
203
     * Persist's the passed product bundle option value data.
204
     *
205
     * @param array $productBundleOptionValue The product bundle option value data to persist
206
     *
207
     * @return void
208
     */
209
    protected function persistProductBundleOptionValue($productBundleOptionValue)
210
    {
211
        $this->getSubject()->persistProductBundleOptionValue($productBundleOptionValue);
212
    }
213
}
214