Completed
Push — 14.x ( 185684 )
by Tim
09:28
created

EeBunchSubject::mapSkuToRowId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 2
nc 2
nop 1
crap 6
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
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
87
     *
88
     * @param array $productVarcharAttribute The varchar value to check
89
     *
90
     * @return boolean TRUE if the URL key is related, else FALSE
91
     */
92
    public function isUrlKeyOf(array $productVarcharAttribute)
93
    {
94
        return ((integer) $productVarcharAttribute[MemberNames::ROW_ID] === (integer) $this->getLastRowId()) &&
95
               ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
96
    }
97
98
    /**
99
     * Clean up the global data after importing the bunch.
100
     *
101
     * @param string $serial The serial of the actual import
102
     *
103
     * @return void
104
     */
105
    public function tearDown($serial)
106
    {
107
108
        // load the registry processor
109
        $registryProcessor = $this->getRegistryProcessor();
110
111
        // update the status up the actual import with SKU => row ID mapping
112
        $registryProcessor->mergeAttributesRecursive(RegistryKeys::STATUS, array(RegistryKeys::SKU_ROW_ID_MAPPING => $this->skuRowIdMapping));
113
114
        // call parent method
115
        parent::tearDown($serial);
116
    }
117
118
    /**
119
     * Set's the row ID of the product that has been created recently.
120
     *
121
     * @param string $lastRowId The row ID
122
     *
123
     * @return void
124
     */
125
    public function setLastRowId($lastRowId)
126
    {
127
        $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...
128
    }
129
130
    /**
131
     * Return's the row ID of the product that has been created recently.
132
     *
133
     * @return string The row Id
134
     */
135
    public function getLastRowId()
136
    {
137
        return $this->lastRowId;
138
    }
139
140
    /**
141
     * Add the passed SKU => row ID mapping.
142
     *
143
     * @param string       $sku   The SKU
144
     * @param integer|null $rowId The optional entity ID, the last processed entity ID is used, if not set
145
     *
146
     * @return void
147
     */
148
    public function addSkuRowIdMapping($sku, $rowId = null)
149
    {
150
        $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...
151
    }
152
153
    /**
154
     * Return the row ID for the passed SKU.
155
     *
156
     * @param string $sku The SKU to return the row ID for
157
     *
158
     * @return integer The mapped row ID
159
     * @throws \TechDivision\Import\Product\Ee\Exceptions\MapSkuToRowIdException Is thrown if the SKU is not mapped yet
160
     */
161
    public function mapSkuToRowId($sku)
162
    {
163
164
        // query weather or not the SKU has been mapped
165
        if (isset($this->skuRowIdMapping[$sku])) {
166
            return $this->skuRowIdMapping[$sku];
167
        }
168
169
        // throw an exception if the SKU has not been mapped yet
170
        throw new MapSkuToRowIdException(
171
            $this->appendExceptionSuffix(
172
                sprintf('Found not mapped entity ID for SKU %s', $sku)
173
            )
174
        );
175
    }
176
}
177