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

getChildSkuSelectionMapping()   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\BundleSelectionPriceObserver
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 selection price 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 BundleSelectionPriceObserver extends AbstractProductImportObserver
39
{
40
41
    /**
42
     * The product bundle processor instance.
43
     *
44
     * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface
45
     */
46
    protected $productBundleProcessor;
47
48
    /**
49
     * Initialize the observer with the passed product bundle processor instance.
50
     *
51
     * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance
52
     */
53
    public function __construct(ProductBundleProcessorInterface $productBundleProcessor)
54
    {
55
        $this->productBundleProcessor = $productBundleProcessor;
56
    }
57
58
    /**
59
     * Return's the product bundle processor instance.
60
     *
61
     * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance
62
     */
63
    protected function getProductBundleProcessor()
64
    {
65
        return $this->productBundleProcessor;
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
Unused Code introduced by
The call to BundleSelectionPriceObse...: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...
78
79
        // load the store view code
80
        $storeViewCode = $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...
81
82
        // check if we're in default store
83
        if (!$this->isDefaultStore($storeViewCode)) {
84
            // prepare and initialize the bundle selection price
85
            $productBundleSelectionPrice = $this->initializeBundleSelectionPrice($this->prepareAttributes());
86
87
            // persist the bundle selection price
88
            $this->persistProductBundleSelectionPrice($productBundleSelectionPrice);
89
        }
90
    }
91
92
    /**
93
     * Prepare the attributes of the entity that has to be persisted.
94
     *
95
     * @return array The prepared attributes
96
     */
97
    protected function prepareAttributes()
98
    {
99
100
        // load the store view code
101
        $storeViewCode = $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...
102
103
        // load the store/website ID
104
        $store = $this->getStoreByStoreCode($storeViewCode);
105
        $websiteId = $store[MemberNames::WEBSITE_ID];
106
107
        // load parent/option ID
108
        $parentId = $this->mapSku($this->getValue(ColumnKeys::BUNDLE_PARENT_SKU));
109
110
        // load the default values
111
        $selectionPriceValue = $this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE);
112
113
        try {
114
            // try to load the selection price type
115
            $selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE));
116
        } catch (\Exception $e) {
117
            throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE), $e);
0 ignored issues
show
Documentation introduced by
array(\TechDivision\Impo...UNDLE_VALUE_PRICE_TYPE) 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...
118
        }
119
120
        try {
121
            // try to load the selection ID for the child SKU
122
            $selectionId = $this->getChildSkuSelectionMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU));
123
        } catch (\Exception $e) {
124
            throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_SKU), $e);
0 ignored issues
show
Documentation introduced by
array(\TechDivision\Impo...Keys::BUNDLE_VALUE_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...
125
        }
126
127
        // return the prepared bundle selection price
128
        return $this->initializeEntity(
129
            array(
130
                MemberNames::SELECTION_ID          => $selectionId,
131
                MemberNames::PARENT_PRODUCT_ID     => $parentId,
132
                MemberNames::WEBSITE_ID            => $websiteId,
133
                MemberNames::SELECTION_PRICE_TYPE  => $selectionPriceType,
134
                MemberNames::SELECTION_PRICE_VALUE => $selectionPriceValue
135
            )
136
        );
137
    }
138
139
    /**
140
     * Initialize the bundle selection price with the passed attributes and returns an instance.
141
     *
142
     * @param array $attr The bundle selection price attributes
143
     *
144
     * @return array The initialized bundle selection price
145
     */
146
    protected function initializeBundleSelectionPrice(array $attr)
147
    {
148
        return $attr;
149
    }
150
151
    /**
152
     * Return's the mapping for the passed price type.
153
     *
154
     * @param string $priceType The price type to map
155
     *
156
     * @return integer The mapped price type
157
     * @throws \Exception Is thrown, if the passed price type can't be mapped
158
     */
159
    protected function mapPriceType($priceType)
160
    {
161
        return $this->getSubject()->mapPriceType($priceType);
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 mapPriceType() 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...
162
    }
163
164
    /**
165
     * Return's the selection ID for the passed child SKU.
166
     *
167
     * @param string $childSku The child SKU to return the selection ID for
168
     *
169
     * @return integer The last created selection ID
170
     */
171
    protected function getChildSkuSelectionMapping($childSku)
172
    {
173
        return $this->getSubject()->getChildSkuSelectionMapping($childSku);
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 getChildSkuSelectionMapping() 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...
174
    }
175
176
    /**
177
     * Query whether or not the passed store view code is the default one.
178
     *
179
     * @param string $storeViewCode The store view code to be queried
180
     *
181
     * @return boolean TRUE if the passed store view code is the default one, else FALSE
182
     */
183
    protected function isDefaultStore($storeViewCode)
184
    {
185
        return StoreViewCodes::ADMIN === strtolower($storeViewCode);
186
    }
187
188
    /**
189
     * Return's the store for the passed store code.
190
     *
191
     * @param string $storeCode The store code to return the store for
192
     *
193
     * @return array The requested store
194
     * @throws \Exception Is thrown, if the requested store is not available
195
     */
196
    protected function getStoreByStoreCode($storeCode)
197
    {
198
        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...
199
    }
200
201
    /**
202
     * Persist's the passed product bundle selection price data and return's the ID.
203
     *
204
     * @param array $productBundleSelectionPrice The product bundle selection price data to persist
205
     *
206
     * @return void
207
     */
208
    protected function persistProductBundleSelectionPrice($productBundleSelectionPrice)
209
    {
210
        $this->getProductBundleProcessor()->persistProductBundleSelectionPrice($productBundleSelectionPrice);
211
    }
212
213
    /**
214
     * Return the entity ID for the passed SKU.
215
     *
216
     * @param string $sku The SKU to return the entity ID for
217
     *
218
     * @return integer The mapped entity ID
219
     * @throws \Exception Is thrown if the SKU is not mapped yet
220
     */
221
    protected function mapSku($sku)
222
    {
223
        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...
224
    }
225
}
226