Completed
Pull Request — master (#5)
by Tim
02:51
created

EeBunchSubject::nextIdentifier()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Ee\Subjects\BunchHandler
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\Subjects\BunchSubject;
25
26
/**
27
 * A SLSB that handles the process to import product bunches.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-ee
33
 * @link      http://www.techdivision.com
34
 */
35
class EeBunchSubject extends BunchSubject
36
{
37
38
    /**
39
     * The row ID of the product that has been created recently.
40
     *
41
     * @var integer
42
     */
43
    protected $lastRowId;
44
45
    /**
46
     * The mapping for the SKUs to the created row IDs.
47
     *
48
     * @var array
49
     */
50
    protected $skuRowIdMapping = array();
51
52
    /**
53
     * The mapping for the supported backend types (for the product entity) => persist methods.
54
     *
55
     * @var array
56
     */
57
    protected $backendTypes = array(
58
        'datetime' => array('persistProductDatetimeAttribute', 'loadProductDatetimeAttributeByRowIdAndAttributeIdAndStoreId'),
59
        'decimal'  => array('persistProductDecimalAttribute', 'loadProductDecimalAttributeByRowIdAndAttributeIdAndStoreId'),
60
        'int'      => array('persistProductIntAttribute', 'loadProductIntAttributeByRowIdAndAttributeIdAndStoreId'),
61
        'text'     => array('persistProductTextAttribute', 'loadProductTextAttributeByRowIdAndAttributeIdAndStoreId'),
62
        'varchar'  => array('persistProductVarcharAttribute', 'loadProductVarcharAttributeByRowIdAndAttributeIdAndStoreId')
63
    );
64
65
    /**
66
     * Mappings for the table column => CSV column header.
67
     *
68
     * @var array
69
     */
70
    protected $headerStockMappings = array(
71
        'qty'                              => array('qty', 'float'),
72
        'min_qty'                          => array('out_of_stock_qty', 'float'),
73
        'use_config_min_qty'               => array('use_config_min_qty', 'int'),
74
        'is_qty_decimal'                   => array('is_qty_decimal', 'int'),
75
        'backorders'                       => array('allow_backorders', 'int'),
76
        'use_config_backorders'            => array('use_config_backorders', 'int'),
77
        'min_sale_qty'                     => array('min_cart_qty', 'float'),
78
        'use_config_min_sale_qty'          => array('use_config_min_sale_qty', 'int'),
79
        'max_sale_qty'                     => array('max_cart_qty', 'float'),
80
        'use_config_max_sale_qty'          => array('use_config_max_sale_qty', 'int'),
81
        'is_in_stock'                      => array('is_in_stock', 'int'),
82
        'notify_stock_qty'                 => array('notify_on_stock_below', 'float'),
83
        'use_config_notify_stock_qty'      => array('use_config_notify_stock_qty', 'int'),
84
        'manage_stock'                     => array('manage_stock', 'int'),
85
        'use_config_manage_stock'          => array('use_config_manage_stock', 'int'),
86
        'use_config_qty_increments'        => array('use_config_qty_increments', 'int'),
87
        'qty_increments'                   => array('qty_increments', 'float'),
88
        'use_config_enable_qty_inc'        => array('use_config_enable_qty_inc', 'int'),
89
        'enable_qty_increments'            => array('enable_qty_increments', 'int'),
90
        'is_decimal_divided'               => array('is_decimal_divided', 'int'),
91
        'deferred_stock_update'            => array('deferred_stock_update', 'int'),
92
        'use_config_deferred_stock_update' => array('use_config_deferred_stock_update', 'int'),
93
    );
94
95
    /**
96
     * Clean up the global data after importing the bunch.
97
     *
98
     * @return void
99
     */
100
    public function tearDown()
101
    {
102
103
        // call parent method
104
        parent::tearDown();
105
106
        // load the registry processor
107
        $registryProcessor = $this->getRegistryProcessor();
108
109
        // update the status up the actual import with SKU => row ID mapping
110
        $registryProcessor->mergeAttributesRecursive($this->serial, array(RegistryKeys::SKU_ROW_ID_MAPPING => $this->skuRowIdMapping));
111
    }
112
113
    /**
114
     * Set's the row ID of the product that has been created recently.
115
     *
116
     * @param string $lastRowId The row ID
117
     *
118
     * @return void
119
     */
120
    public function setLastRowId($lastRowId)
121
    {
122
        $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...
123
    }
124
125
    /**
126
     * Return's the row ID of the product that has been created recently.
127
     *
128
     * @return string The row Id
129
     */
130
    public function getLastRowId()
131
    {
132
        return $this->lastRowId;
133
    }
134
135
    /**
136
     * Add the passed SKU => row ID mapping.
137
     *
138
     * @param string $sku The SKU
139
     *
140
     * @return void
141
     */
142
    public function addSkuRowIdMapping($sku)
143
    {
144
        $this->skuRowIdMapping[$sku] = $this->getLastRowId();
145
    }
146
147
    /**
148
     * Return's the product rows with the passed SKU.
149
     *
150
     * @param string $sku The SKU of the product rows to return
151
     *
152
     * @return array The product rows
153
     */
154
    public function getProductRowsBySku($sku)
155
    {
156
        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...
157
    }
158
159
    /**
160
     * Return's the next available product entity ID.
161
     *
162
     * @return integer The next available product entity ID
163
     */
164
    public function nextIdentifier()
165
    {
166
        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...
167
    }
168
169
    /**
170
     * Load's and return's the product with the passed SKU and timestamp.
171
     *
172
     * @param string  $sku       The SKU of the product to return
173
     * @param integer $timestamp The timestamp to find the matching scheduled update
174
     *
175
     * @return array The product
176
     */
177
    public function loadProductBySkuAndTimestamp($sku, $timestamp)
178
    {
179
        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...
180
    }
181
182
    /**
183
     * Load's and return's the datetime attribute with the passed row/attribute/store ID.
184
     *
185
     * @param integer $rowId       The row ID of the attribute
186
     * @param integer $attributeId The attribute ID of the attribute
187
     * @param integer $storeId     The store ID of the attribute
188
     *
189
     * @return array|null The datetime attribute
190
     */
191
    public function loadProductDatetimeAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
192
    {
193
        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...
194
    }
195
196
    /**
197
     * Load's and return's the decimal attribute with the passed row/attribute/store ID.
198
     *
199
     * @param integer $rowId       The row ID of the attribute
200
     * @param integer $attributeId The attribute ID of the attribute
201
     * @param integer $storeId     The store ID of the attribute
202
     *
203
     * @return array|null The decimal attribute
204
     */
205
    public function loadProductDecimalAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
206
    {
207
        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...
208
    }
209
210
    /**
211
     * Load's and return's the integer attribute with the passed row/attribute/store ID.
212
     *
213
     * @param integer $rowId       The row ID of the attribute
214
     * @param integer $attributeId The attribute ID of the attribute
215
     * @param integer $storeId     The store ID of the attribute
216
     *
217
     * @return array|null The integer attribute
218
     */
219
    public function loadProductIntAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
220
    {
221
        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...
222
    }
223
224
    /**
225
     * Load's and return's the text attribute with the passed row/attribute/store ID.
226
     *
227
     * @param integer $rowId       The row ID of the attribute
228
     * @param integer $attributeId The attribute ID of the attribute
229
     * @param integer $storeId     The store ID of the attribute
230
     *
231
     * @return array|null The text attribute
232
     */
233
    public function loadProductTextAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
234
    {
235
        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...
236
    }
237
238
    /**
239
     * Load's and return's the varchar attribute with the passed row/attribute/store ID.
240
     *
241
     * @param integer $rowId       The row ID of the attribute
242
     * @param integer $attributeId The attribute ID of the attribute
243
     * @param integer $storeId     The store ID of the attribute
244
     *
245
     * @return array|null The varchar attribute
246
     */
247
    public function loadProductVarcharAttributeByRowIdAndAttributeIdAndStoreId($rowId, $attributeId, $storeId)
248
    {
249
        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...
250
    }
251
}
252