Completed
Push — 19.x ( f78ed0 )
by Tim
01:55
created

EeBunchSubject   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 3
dl 0
loc 158
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultCallbackMappings() 0 4 1
A isUrlKeyOf() 0 5 2
A tearDown() 0 12 1
A setLastRowId() 0 4 1
A getLastRowId() 0 4 1
A addSkuRowIdMapping() 0 4 2
A mapSkuToRowId() 0 15 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\Utils\StoreViewCodes;
25
use TechDivision\Import\Product\Ee\Utils\MemberNames;
26
use TechDivision\Import\Product\Subjects\BunchSubject;
27
use TechDivision\Import\Product\Ee\Exceptions\MapSkuToRowIdException;
28
29
/**
30
 * A SLSB that handles the process to import product bunches.
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-ee
36
 * @link      http://www.techdivision.com
37
 */
38
class EeBunchSubject extends BunchSubject
39
{
40
41
    /**
42
     * The row ID of the product that has been created recently.
43
     *
44
     * @var integer
45
     */
46
    protected $lastRowId;
47
48
    /**
49
     * The mapping for the SKUs to the created row IDs.
50
     *
51
     * @var array
52
     */
53
    protected $skuRowIdMapping = array();
54
55
    /**
56
     * Mappings for the table column => CSV column header.
57
     *
58
     * @var array
59
     */
60
    protected $headerStockMappings = array(
61
        'qty'                              => array('qty', 'float'),
62
        'min_qty'                          => array('out_of_stock_qty', 'float'),
63
        'use_config_min_qty'               => array('use_config_min_qty', 'int'),
64
        'is_qty_decimal'                   => array('is_qty_decimal', 'int'),
65
        'backorders'                       => array('allow_backorders', 'int'),
66
        'use_config_backorders'            => array('use_config_backorders', 'int'),
67
        'min_sale_qty'                     => array('min_cart_qty', 'float'),
68
        'use_config_min_sale_qty'          => array('use_config_min_sale_qty', 'int'),
69
        'max_sale_qty'                     => array('max_cart_qty', 'float'),
70
        'use_config_max_sale_qty'          => array('use_config_max_sale_qty', 'int'),
71
        'is_in_stock'                      => array('is_in_stock', 'int'),
72
        'notify_stock_qty'                 => array('notify_on_stock_below', 'float'),
73
        'use_config_notify_stock_qty'      => array('use_config_notify_stock_qty', 'int'),
74
        'manage_stock'                     => array('manage_stock', 'int'),
75
        'use_config_manage_stock'          => array('use_config_manage_stock', 'int'),
76
        'use_config_qty_increments'        => array('use_config_qty_increments', 'int'),
77
        'qty_increments'                   => array('qty_increments', 'float'),
78
        'use_config_enable_qty_inc'        => array('use_config_enable_qty_inc', 'int'),
79
        'enable_qty_increments'            => array('enable_qty_increments', 'int'),
80
        'is_decimal_divided'               => array('is_decimal_divided', 'int'),
81
        'deferred_stock_update'            => array('deferred_stock_update', 'int'),
82
        'use_config_deferred_stock_update' => array('use_config_deferred_stock_update', 'int'),
83
    );
84
85
    /**
86
     * The default callback mappings for the Magento EE standard product attributes.
87
     *
88
     * @var array
89
     */
90
    protected $defaultEeCallbackMappings = array(
91
        'is_returnable' => array('import_product_ee.callback.rma')
92
    );
93
94
    /**
95
     * Return's the default callback mappings.
96
     *
97
     * @return array The default callback mappings
98
     */
99
    public function getDefaultCallbackMappings()
100
    {
101
        return array_merge(parent::getDefaultCallbackMappings(), $this->defaultEeCallbackMappings);
102
    }
103
104
    /**
105
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
106
     *
107
     * @param array $productVarcharAttribute The varchar value to check
108
     *
109
     * @return boolean TRUE if the URL key is related, else FALSE
110
     */
111
    public function isUrlKeyOf(array $productVarcharAttribute)
112
    {
113
        return ((integer) $productVarcharAttribute[MemberNames::ROW_ID] === (integer) $this->getLastRowId()) &&
114
               ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
115
    }
116
117
    /**
118
     * Clean up the global data after importing the bunch.
119
     *
120
     * @param string $serial The serial of the actual import
121
     *
122
     * @return void
123
     */
124
    public function tearDown($serial)
125
    {
126
127
        // load the registry processor
128
        $registryProcessor = $this->getRegistryProcessor();
129
130
        // update the status up the actual import with SKU => row ID mapping
131
        $registryProcessor->mergeAttributesRecursive(RegistryKeys::STATUS, array(RegistryKeys::SKU_ROW_ID_MAPPING => $this->skuRowIdMapping));
132
133
        // call parent method
134
        parent::tearDown($serial);
135
    }
136
137
    /**
138
     * Set's the row ID of the product that has been created recently.
139
     *
140
     * @param string $lastRowId The row ID
141
     *
142
     * @return void
143
     */
144
    public function setLastRowId($lastRowId)
145
    {
146
        $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...
147
    }
148
149
    /**
150
     * Return's the row ID of the product that has been created recently.
151
     *
152
     * @return string The row Id
153
     */
154
    public function getLastRowId()
155
    {
156
        return $this->lastRowId;
157
    }
158
159
    /**
160
     * Add the passed SKU => row ID mapping.
161
     *
162
     * @param string       $sku   The SKU
163
     * @param integer|null $rowId The optional entity ID, the last processed entity ID is used, if not set
164
     *
165
     * @return void
166
     */
167
    public function addSkuRowIdMapping($sku, $rowId = null)
168
    {
169
        $this->skuRowIdMapping[$sku] = $rowId == null ? $this->getLastRowId() : $rowId;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $rowId of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
170
    }
171
172
    /**
173
     * Return the row ID for the passed SKU.
174
     *
175
     * @param string $sku The SKU to return the row ID for
176
     *
177
     * @return integer The mapped row ID
178
     * @throws \TechDivision\Import\Product\Ee\Exceptions\MapSkuToRowIdException Is thrown if the SKU is not mapped yet
179
     */
180
    public function mapSkuToRowId($sku)
181
    {
182
183
        // query weather or not the SKU has been mapped
184
        if (isset($this->skuRowIdMapping[$sku])) {
185
            return $this->skuRowIdMapping[$sku];
186
        }
187
188
        // throw an exception if the SKU has not been mapped yet
189
        throw new MapSkuToRowIdException(
190
            $this->appendExceptionSuffix(
191
                sprintf('Found not mapped entity ID for SKU %s', $sku)
192
            )
193
        );
194
    }
195
}
196