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

BundleOptionObserver::prepareAttributes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 24
loc 24
ccs 0
cts 16
cp 0
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Observers\BundleOptionObserver
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 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 BundleOptionObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * Will be invoked by the action on the events the listener has been registered for.
42
     *
43
     * @param array $row The row to handle
44
     *
45
     * @return array The modified row
46
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
47
     */
48
    public function handle(array $row)
49
    {
50
51
        // initialize the row
52
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
53
54
        // process the functionality and return the row
55
        $this->process();
56
57
        // return the processed row
58
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
59
    }
60
61
    /**
62
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66 View Code Duplication
    protected function process()
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...
67
    {
68
69
        // prepare the store view code
70
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
71
72
        // return immediately if we're have no store view code set
73
        if (StoreViewCodes::ADMIN !== $this->getStoreViewCode(StoreViewCodes::ADMIN)) {
74
            return;
75
        }
76
77
        // query whether or not the option has already been created
78
        if (!$this->exists($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...s\BundleOptionObserver>.

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...
79
            // load the bundle option
80
            $bundleOption = $this->initializeBundleOption($this->prepareAttributes());
81
82
            // persist the product bundle option
83
            $optionId = $this->persistProductBundleOption($bundleOption);
84
85
            // store the name => option ID mapping
86
            $this->addNameOptionIdMapping($name, $optionId);
87
        }
88
    }
89
90
    /**
91
     * Prepare the attributes of the entity that has to be persisted.
92
     *
93
     * @return array The prepared attributes
94
     */
95 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...
96
    {
97
98
        // reset the position counter for the bundle selection
99
        $this->resetPositionCounter();
100
101
        // load and map the parent option ID
102
        $parentId = $this->mapSku($this->getValue(ColumnKeys::BUNDLE_PARENT_SKU));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
103
104
        // extract the parent/child ID as well as type and position
105
        $required = $this->getValue(ColumnKeys::BUNDLE_VALUE_REQUIRED);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
106
        $type = $this->getValue(ColumnKeys::BUNDLE_VALUE_TYPE);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
107
        $position = 1;
108
109
        // return the prepared product
110
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...s\BundleOptionObserver>.

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...
111
            array(
112
                MemberNames::PARENT_ID => $parentId,
113
                MemberNames::REQUIRED  => $required,
114
                MemberNames::POSITION  => $position,
115
                MemberNames::TYPE      => $type
116
            )
117
        );
118
    }
119
120
    /**
121
     * Initialize the bundle option with the passed attributes and returns an instance.
122
     *
123
     * @param array $attr The bundle option attributes
124
     *
125
     * @return array The initialized bundle option
126
     */
127
    protected function initializeBundleOption(array $attr)
128
    {
129
        return $attr;
130
    }
131
132
    /**
133
     * Reset the position counter to 1.
134
     *
135
     * @return void
136
     */
137
    protected function resetPositionCounter()
138
    {
139
        $this->getSubject()->resetPositionCounter();
140
    }
141
142
    /**
143
     * Add's the mapping for the passed name => option ID.
144
     *
145
     * @param string  $name     The name of the option
146
     * @param integer $optionId The created option ID
147
     *
148
     * @return void
149
     */
150
    protected function addNameOptionIdMapping($name, $optionId)
151
    {
152
        $this->getSubject()->addNameOptionIdMapping($name, $optionId);
153
    }
154
155
    /**
156
     * Query whether or not the option with the passed name has already been created.
157
     *
158
     * @param string $name The option name to query for
159
     *
160
     * @return boolean TRUE if the option already exists, else FALSE
161
     */
162
    protected function exists($name)
163
    {
164
        return $this->getSubject()->exists($name);
165
    }
166
167
    /**
168
     * Return the entity ID for the passed SKU.
169
     *
170
     * @param string $sku The SKU to return the entity ID for
171
     *
172
     * @return integer The mapped entity ID
173
     * @throws \Exception Is thrown if the SKU is not mapped yet
174
     */
175
    protected function mapSku($sku)
176
    {
177
        return $this->getSubject()->mapSkuToEntityId($sku);
178
    }
179
180
    /**
181
     * Persist's the passed product bundle option data and return's the ID.
182
     *
183
     * @param array $productBundleOption The product bundle option data to persist
184
     *
185
     * @return string The ID of the persisted entity
186
     */
187
    protected function persistProductBundleOption($productBundleOption)
188
    {
189
        return $this->getSubject()->persistProductBundleOption($productBundleOption);
190
    }
191
}
192