Completed
Push — 9.x ( 2209b0 )
by Tim
03:29
created

BundleOptionValueObserver::mapSku()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 1
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
use TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface;
28
29
/**
30
 * Oberserver that provides functionality for the bundle option value replace operation.
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-product-bundle
36
 * @link      http://www.techdivision.com
37
 */
38
class BundleOptionValueObserver extends AbstractProductImportObserver
39
{
40
41
    /**
42
     * The option value store mapping.
43
     *
44
     * @var array
45
     */
46
    protected $optionValueStoreMapping = array();
47
48
    /**
49
     * The product bundle processor instance.
50
     *
51
     * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface
52
     */
53
    protected $productBundleProcessor;
54
55
    /**
56
     * Initialize the observer with the passed product bundle processor instance.
57
     *
58
     * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance
59
     */
60
    public function __construct(ProductBundleProcessorInterface $productBundleProcessor)
61
    {
62
        $this->productBundleProcessor = $productBundleProcessor;
63
    }
64
65
    /**
66
     * Return's the product bundle processor instance.
67
     *
68
     * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance
69
     */
70
    protected function getProductBundleProcessor()
71
    {
72
        return $this->productBundleProcessor;
73
    }
74
75
    /**
76
     * Process the observer's business logic.
77
     *
78
     * @return array The processed row
79
     */
80
    protected function process()
81
    {
82
83
        // prepare the store view code
84
        $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...
Deprecated Code introduced by
The method TechDivision\Import\Obse...:prepareStoreViewCode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
86
        // prepare the attributes
87
        $attr = $this->prepareAttributes();
88
89
        // load store/option ID
90
        $storeId = $attr[MemberNames::STORE_ID];
91
        $optionId = $attr[MemberNames::OPTION_ID];
92
93
        // if the store has already been mapped, return immediately
94
        if ($this->isMapped($optionId, $storeId)) {
95
            return;
96
        }
97
98
        // initialize and save the product bundle option value
99
        if ($bundleOption = $this->initializeBundleOptionValue($attr)) {
100
            $this->persistProductBundleOptionValue($bundleOption);
101
        }
102
103
        // add the store => option mapping
104
        $this->addOptionValueStoreMapping($optionId, $storeId);
105
    }
106
107
    /**
108
     * Prepare the attributes of the entity that has to be persisted.
109
     *
110
     * @return array The prepared attributes
111
     */
112
    protected function prepareAttributes()
113
    {
114
115
        // load the product bundle option name
116
        $name = $this->getValue(ColumnKeys::BUNDLE_VALUE_NAME);
117
118
        // load the actual option ID
119
        $optionId = $this->getLastOptionId();
120
121
        try {
122
            // load and map the parent SKU
123
            $parentProductId = $this->mapSku($this->getValue(ColumnKeys::BUNDLE_PARENT_SKU));
124
        } catch (\Exception $e) {
125
            throw $this->wrapException(array(ColumnKeys::BUNDLE_PARENT_SKU), $e);
0 ignored issues
show
Documentation introduced by
array(\TechDivision\Impo...eys::BUNDLE_PARENT_SKU) is of type array<integer,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method TechDivision\Import\Obse...server::wrapException() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
126
        }
127
128
        // load the store/website ID
129
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
0 ignored issues
show
Deprecated Code introduced by
The method TechDivision\Import\Obse...ver::getStoreViewCode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
130
        $storeId = $store[MemberNames::STORE_ID];
131
132
        // return the prepared product
133
        return $this->initializeEntity(
134
            array(
135
                MemberNames::OPTION_ID         => $optionId,
136
                MemberNames::PARENT_PRODUCT_ID => $parentProductId,
137
                MemberNames::STORE_ID          => $storeId,
138
                MemberNames::TITLE             => $name
139
            )
140
        );
141
    }
142
143
    /**
144
     * Initialize the bundle option value with the passed attributes and returns an instance.
145
     *
146
     * @param array $attr The bundle option value attributes
147
     *
148
     * @return array The initialized bundle option value
149
     */
150
    protected function initializeBundleOptionValue(array $attr)
151
    {
152
        return $attr;
153
    }
154
155
    /**
156
     * Add the store => option mapping.
157
     *
158
     * @param integer $optionId The option ID to map
159
     * @param integer $storeId  The store ID to map
160
     *
161
     * @return void
162
     */
163
    protected function addOptionValueStoreMapping($optionId, $storeId)
164
    {
165
        $this->optionValueStoreMapping[$optionId][] = $storeId;
166
    }
167
168
    /**
169
     * Query whether or not the passed option/store ID combination has already been mapped.
170
     *
171
     * @param integer $optionId The option ID to map
172
     * @param integer $storeId  The store ID to map
173
     *
174
     * @return boolean TRUE if the combination has already been mapped, else FALSE
175
     */
176
    protected function isMapped($optionId, $storeId)
177
    {
178
        return (isset($this->optionValueStoreMapping[$optionId]) && in_array($storeId, $this->optionValueStoreMapping[$optionId]));
179
    }
180
181
    /**
182
     * Return's the last created option ID.
183
     *
184
     * @return integer $optionId The last created option ID
185
     */
186
    protected function getLastOptionId()
187
    {
188
        return $this->getSubject()->getLastOptionId();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method getLastOptionId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
189
    }
190
191
    /**
192
     * Return's the option ID for the passed name.
193
     *
194
     * @param string $name The name to return the option ID for
195
     *
196
     * @return integer The option ID for the passed name
197
     * @throws \Exception Is thrown, if no option ID for the passed name is available
198
     */
199
    protected function getOptionIdForName($name)
200
    {
201
        return $this->getSubject()->getOptionIdForName($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method getOptionIdForName() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
202
    }
203
204
    /**
205
     * Return's the store for the passed store code.
206
     *
207
     * @param string $storeCode The store code to return the store for
208
     *
209
     * @return array The requested store
210
     * @throws \Exception Is thrown, if the requested store is not available
211
     */
212
    protected function getStoreByStoreCode($storeCode)
213
    {
214
        return $this->getSubject()->getStoreByStoreCode($storeCode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method getStoreByStoreCode() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
215
    }
216
217
    /**
218
     * Persist's the passed product bundle option value data.
219
     *
220
     * @param array $productBundleOptionValue The product bundle option value data to persist
221
     *
222
     * @return void
223
     */
224
    protected function persistProductBundleOptionValue($productBundleOptionValue)
225
    {
226
        $this->getProductBundleProcessor()->persistProductBundleOptionValue($productBundleOptionValue);
227
    }
228
229
    /**
230
     * Return the entity ID for the passed SKU.
231
     *
232
     * @param string $sku The SKU to return the entity ID for
233
     *
234
     * @return integer The mapped entity ID
235
     * @throws \Exception Is thrown if the SKU is not mapped yet
236
     */
237
    protected function mapSku($sku)
238
    {
239
        return $this->getSubject()->mapSkuToEntityId($sku);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method mapSkuToEntityId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
240
    }
241
}
242