Completed
Push — master ( b6f365...cf46d1 )
by Tim
12s
created

EeBunchSubject::isUrlKeyOf()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Ee\Subjects\EeBunchSubject
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-ee
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Ee\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Product\Ee\Utils\MemberNames;
25
use TechDivision\Import\Product\Subjects\BunchSubject;
26
27
/**
28
 * A SLSB that handles the process to import product bunches.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-ee
34
 * @link      http://www.techdivision.com
35
 */
36
class EeBunchSubject extends BunchSubject
37
{
38
39
    /**
40
     * The row ID of the product that has been created recently.
41
     *
42
     * @var integer
43
     */
44
    protected $lastRowId;
45
46
    /**
47
     * The mapping for the SKUs to the created row IDs.
48
     *
49
     * @var array
50
     */
51
    protected $skuRowIdMapping = array();
52
53
    /**
54
     * The mapping for the supported backend types (for the product entity) => persist methods.
55
     *
56
     * @var array
57
     */
58
    protected $backendTypes = array(
59
        'datetime' => array('persistDatetimeAttribute', 'loadProductDatetimeAttributeByRowIdAndAttributeIdAndStoreId'),
60
        'decimal'  => array('persistDecimalAttribute', 'loadProductDecimalAttributeByRowIdAndAttributeIdAndStoreId'),
61
        'int'      => array('persistIntAttribute', 'loadProductIntAttributeByRowIdAndAttributeIdAndStoreId'),
62
        'text'     => array('persistTextAttribute', 'loadProductTextAttributeByRowIdAndAttributeIdAndStoreId'),
63
        'varchar'  => array('persistVarcharAttribute', 'loadProductVarcharAttributeByRowIdAndAttributeIdAndStoreId')
64
    );
65
66
    /**
67
     * Mappings for the table column => CSV column header.
68
     *
69
     * @var array
70
     */
71
    protected $headerStockMappings = array(
72
        'qty'                              => array('qty', 'float'),
73
        'min_qty'                          => array('out_of_stock_qty', 'float'),
74
        'use_config_min_qty'               => array('use_config_min_qty', 'int'),
75
        'is_qty_decimal'                   => array('is_qty_decimal', 'int'),
76
        'backorders'                       => array('allow_backorders', 'int'),
77
        'use_config_backorders'            => array('use_config_backorders', 'int'),
78
        'min_sale_qty'                     => array('min_cart_qty', 'float'),
79
        'use_config_min_sale_qty'          => array('use_config_min_sale_qty', 'int'),
80
        'max_sale_qty'                     => array('max_cart_qty', 'float'),
81
        'use_config_max_sale_qty'          => array('use_config_max_sale_qty', 'int'),
82
        'is_in_stock'                      => array('is_in_stock', 'int'),
83
        'notify_stock_qty'                 => array('notify_on_stock_below', 'float'),
84
        'use_config_notify_stock_qty'      => array('use_config_notify_stock_qty', 'int'),
85
        'manage_stock'                     => array('manage_stock', 'int'),
86
        'use_config_manage_stock'          => array('use_config_manage_stock', 'int'),
87
        'use_config_qty_increments'        => array('use_config_qty_increments', 'int'),
88
        'qty_increments'                   => array('qty_increments', 'float'),
89
        'use_config_enable_qty_inc'        => array('use_config_enable_qty_inc', 'int'),
90
        'enable_qty_increments'            => array('enable_qty_increments', 'int'),
91
        'is_decimal_divided'               => array('is_decimal_divided', 'int'),
92
        'deferred_stock_update'            => array('deferred_stock_update', 'int'),
93
        'use_config_deferred_stock_update' => array('use_config_deferred_stock_update', 'int'),
94
    );
95
96
    /**
97
     * Return's TRUE, if the passed URL key varchar value IS related with the passed PK.
98
     *
99
     * @param array   $productVarcharAttribute The varchar value to check
100
     * @param integer $pk                      The primary key to check
101
     *
102
     * @return boolean TRUE if the URL key is related, else FALSE
103
     */
104
    protected function isUrlKeyOf($productVarcharAttribute, $pk)
105
    {
106
        return $productVarcharAttribute[MemberNames::ROW_ID] === $pk;
107
    }
108
109
    /**
110
     * Clean up the global data after importing the bunch.
111
     *
112
     * @param string $serial The serial of the actual import
113
     *
114
     * @return void
115
     */
116
    public function tearDown($serial)
117
    {
118
119
        // call parent method
120
        parent::tearDown($serial);
121
122
        // load the registry processor
123
        $registryProcessor = $this->getRegistryProcessor();
124
125
        // update the status up the actual import with SKU => row ID mapping
126
        $registryProcessor->mergeAttributesRecursive($this->serial, array(RegistryKeys::SKU_ROW_ID_MAPPING => $this->skuRowIdMapping));
127
    }
128
129
    /**
130
     * Set's the row ID of the product that has been created recently.
131
     *
132
     * @param string $lastRowId The row ID
133
     *
134
     * @return void
135
     */
136
    public function setLastRowId($lastRowId)
137
    {
138
        $this->lastRowId = $lastRowId;
0 ignored issues
show
Documentation Bug introduced by
The property $lastRowId was declared of type integer, but $lastRowId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
139
    }
140
141
    /**
142
     * Return's the row ID of the product that has been created recently.
143
     *
144
     * @return string The row Id
145
     */
146
    public function getLastRowId()
147
    {
148
        return $this->lastRowId;
149
    }
150
151
    /**
152
     * Add the passed SKU => row ID mapping.
153
     *
154
     * @param string $sku The SKU
155
     *
156
     * @return void
157
     */
158
    public function addSkuRowIdMapping($sku)
159
    {
160
        $this->skuRowIdMapping[$sku] = $this->getLastRowId();
161
    }
162
163
    /**
164
     * Return's the product rows with the passed SKU.
165
     *
166
     * @param string $sku The SKU of the product rows to return
167
     *
168
     * @return array The product rows
169
     */
170
    public function getProductRowsBySku($sku)
171
    {
172
        return $this->getProductProcessor()->getProductRowsBySku($sku);
0 ignored issues
show
Bug introduced by
The method getProductRowsBySku() does not seem to exist on object<TechDivision\Impo...ductProcessorInterface>.

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...
173
    }
174
175
    /**
176
     * Return's the next available product entity ID.
177
     *
178
     * @return integer The next available product entity ID
179
     */
180
    public function nextIdentifier()
181
    {
182
        return $this->getProductProcessor()->nextIdentifier();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method nextIdentifier() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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...
183
    }
184
185
    /**
186
     * Load's and return's the product with the passed SKU and timestamp.
187
     *
188
     * @param string  $sku       The SKU of the product to return
189
     * @param integer $timestamp The timestamp to find the matching scheduled update
190
     *
191
     * @return array The product
192
     */
193
    public function loadProductBySkuAndTimestamp($sku, $timestamp)
194
    {
195
        return $this->getProductProcessor()->loadProductBySkuAndTimestamp($sku, $timestamp);
0 ignored issues
show
Bug introduced by
The method loadProductBySkuAndTimestamp() does not seem to exist on object<TechDivision\Impo...ductProcessorInterface>.

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...
196
    }
197
198
    /**
199
     * Load's and return's the datetime attribute with the passed row/attribute/store ID.
200
     *
201
     * @param integer $rowId       The row ID of the attribute
202
     * @param integer $attributeId The attribute ID of the attribute
203
     * @param integer $storeId     The store ID of the attribute
204
     *
205
     * @return array|null The datetime attribute
206
     */
207
    public function loadProductDatetimeAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
208
    {
209
        return  $this->getProductProcessor()->loadProductDatetimeAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductDatetimeAttri...AttributeIdAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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...
210
    }
211
212
    /**
213
     * Load's and return's the decimal attribute with the passed row/attribute/store ID.
214
     *
215
     * @param integer $rowId       The row ID of the attribute
216
     * @param integer $attributeId The attribute ID of the attribute
217
     * @param integer $storeId     The store ID of the attribute
218
     *
219
     * @return array|null The decimal attribute
220
     */
221
    public function loadProductDecimalAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
222
    {
223
        return  $this->getProductProcessor()->loadProductDecimalAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductDecimalAttrib...AttributeIdAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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
    /**
227
     * Load's and return's the integer attribute with the passed row/attribute/store ID.
228
     *
229
     * @param integer $rowId       The row ID of the attribute
230
     * @param integer $attributeId The attribute ID of the attribute
231
     * @param integer $storeId     The store ID of the attribute
232
     *
233
     * @return array|null The integer attribute
234
     */
235
    public function loadProductIntAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
236
    {
237
        return $this->getProductProcessor()->loadProductIntAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductIntAttributeB...AttributeIdAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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...
238
    }
239
240
    /**
241
     * Load's and return's the text attribute with the passed row/attribute/store ID.
242
     *
243
     * @param integer $rowId       The row ID of the attribute
244
     * @param integer $attributeId The attribute ID of the attribute
245
     * @param integer $storeId     The store ID of the attribute
246
     *
247
     * @return array|null The text attribute
248
     */
249
    public function loadProductTextAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
250
    {
251
        return $this->getProductProcessor()->loadProductTextAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductTextAttribute...AttributeIdAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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...
252
    }
253
254
    /**
255
     * Load's and return's the varchar attribute with the passed row/attribute/store ID.
256
     *
257
     * @param integer $rowId       The row ID of the attribute
258
     * @param integer $attributeId The attribute ID of the attribute
259
     * @param integer $storeId     The store ID of the attribute
260
     *
261
     * @return array|null The varchar attribute
262
     */
263
    public function loadProductVarcharAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
264
    {
265
        return $this->getProductProcessor()->loadProductVarcharAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductVarcharAttrib...AttributeIdAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductBunchProcessor.

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...
266
    }
267
}
268