Completed
Pull Request — master (#81)
by Tim
03:47
created

BunchSubject::getVisibilityIdMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Subjects\BunchSubject
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Subjects;
22
23
use TechDivision\Import\Product\Utils\MemberNames;
24
use TechDivision\Import\Product\Utils\RegistryKeys;
25
use TechDivision\Import\Product\Utils\VisibilityKeys;
26
use TechDivision\Import\Subjects\ExportableSubjectInterface;
27
use TechDivision\Import\Subjects\ExportableTrait;
28
29
/**
30
 * The subject implementation that handles the business logic to persist products.
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
36
 * @link      http://www.techdivision.com
37
 */
38
class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface
39
{
40
41
    /**
42
     * The trait that implements the export functionality.
43
     *
44
     * @var \TechDivision\Import\Subjects\ExportableTrait
45
     */
46
    use ExportableTrait;
47
48
    /**
49
     * The mapping for the SKU => visibility.
50
     *
51
     * @var array
52
     */
53
    protected $entityIdVisibilityIdMapping = array();
54
55
    /**
56
     * The array with the pre-loaded entity IDs.
57
     *
58
     * @var array
59
     */
60
    protected $preLoadedEntityIds = array();
61
62
    /**
63
     * Mappings for the table column => CSV column header.
64
     *
65
     * @var array
66
     */
67
    protected $headerStockMappings = array(
68
        'qty'                         => array('qty', 'float'),
69
        'min_qty'                     => array('out_of_stock_qty', 'float'),
70
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
71
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
72
        'backorders'                  => array('allow_backorders', 'int'),
73
        'use_config_backorders'       => array('use_config_backorders', 'int'),
74
        'min_sale_qty'                => array('min_cart_qty', 'float'),
75
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
76
        'max_sale_qty'                => array('max_cart_qty', 'float'),
77
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
78
        'is_in_stock'                 => array('is_in_stock', 'int'),
79
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
80
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
81
        'manage_stock'                => array('manage_stock', 'int'),
82
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
83
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
84
        'qty_increments'              => array('qty_increments', 'float'),
85
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
86
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
87
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
88
    );
89
90
    /**
91
     * The array with the available visibility keys.
92
     *
93
     * @var array
94
     */
95
    protected $availableVisibilities = array(
96
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
97
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
98
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
99
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
100
    );
101
102
    /**
103
     * The category IDs the product is related with.
104
     *
105
     * @var array
106
     */
107
    protected $productCategoryIds = array();
108
109
    /**
110
     * The default callback mappings for the Magento standard product attributes.
111
     *
112
     * @var array
113
     */
114
    protected $defaultCallbackMappings = array(
115
        'visibility'           => array('import_product.callback.visibility'),
116
        'tax_class_id'         => array('import_product.callback.tax.class'),
117
        'bundle_price_type'    => array('import_product_bundle.callback.bundle.type'),
118
        'bundle_sku_type'      => array('import_product_bundle.callback.bundle.type'),
119
        'bundle_weight_type'   => array('import_product_bundle.callback.bundle.type'),
120
        'bundle_price_view'    => array('import_product_bundle.callback.bundle.price.view'),
121
        'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type')
122
    );
123
124
    /**
125
     * The used URL keys.
126
     *
127
     * @var array
128
     */
129
    protected $usedUrlKeys = array();
130
131
    /**
132
     * The available entity types.
133
     *
134
     * @var array
135
     */
136
    protected $entityTypes = array();
137
138
    /**
139
     * Intializes the previously loaded global data for exactly one bunch.
140
     *
141
     * @param string $serial The serial of the actual import
142
     *
143
     * @return void
144
     */
145 18
    public function setUp($serial)
146
    {
147
148
        // load the status of the actual import
149 18
        $status = $this->getRegistryProcessor()->getAttribute($serial);
150
151
        // load the global data we've prepared initially
152 18
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
153
154
        // invoke the parent method
155 18
        parent::setUp($serial);
156 18
    }
157
158
    /**
159
     * Clean up the global data after importing the bunch.
160
     *
161
     * @param string $serial The serial of the actual import
162
     *
163
     * @return void
164
     */
165
    public function tearDown($serial)
166
    {
167
168
        // invoke the parent method
169
        parent::tearDown($serial);
170
171
        // load the registry processor
172
        $registryProcessor = $this->getRegistryProcessor();
173
174
        // update the status
175
        $registryProcessor->mergeAttributesRecursive(
176
            $serial,
177
            array(
178
                RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds,
179
            )
180
        );
181
    }
182
183
    /**
184
     * Return's the default callback mappings.
185
     *
186
     * @return array The default callback mappings
187
     */
188 18
    public function getDefaultCallbackMappings()
189
    {
190 18
        return $this->defaultCallbackMappings;
191
    }
192
193
    /**
194
     * Return's the mappings for the table column => CSV column header.
195
     *
196
     * @return array The header stock mappings
197
     */
198 1
    public function getHeaderStockMappings()
199
    {
200 1
        return $this->headerStockMappings;
201
    }
202
203
    /**
204
     * Return's the visibility key for the passed visibility string.
205
     *
206
     * @param string $visibility The visibility string to return the key for
207
     *
208
     * @return integer The requested visibility key
209
     * @throws \Exception Is thrown, if the requested visibility is not available
210
     */
211
    public function getVisibilityIdByValue($visibility)
212
    {
213
214
        // query whether or not, the requested visibility is available
215
        if (isset($this->availableVisibilities[$visibility])) {
216
            // load the visibility ID, add the mapping and return the ID
217
            return $this->availableVisibilities[$visibility];
218
        }
219
220
        // throw an exception, if not
221
        throw new \Exception(
222
            $this->appendExceptionSuffix(
223
                sprintf('Found invalid visibility %s', $visibility)
224
            )
225
        );
226
    }
227
228
    /**
229
     * Add the entity ID => visibility mapping for the actual entity ID.
230
     *
231
     * @param string $visibility The visibility of the actual entity to map
232
     *
233
     * @return void
234
     */
235
    public function addEntityIdVisibilityIdMapping($visibility)
236
    {
237
        $this->entityIdVisibilityIdMapping[$this->getLastEntityId()] = $this->getVisibilityIdByValue($visibility);
238
    }
239
240
    /**
241
     * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created
242
     * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will
243
     * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>.
244
     *
245
     * @return integer The visibility ID
246
     * @throws \Exception Is thrown, if the entity ID has not been mapped
247
     * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue()
248
     */
249 View Code Duplication
    public function getEntityIdVisibilityIdMapping()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
    {
251
252
        // query whether or not the SKU has already been mapped to it's visibility
253
        if (isset($this->entityIdVisibilityIdMapping[$entityId = $this->getLastEntityId()])) {
254
            return $this->entityIdVisibilityIdMapping[$entityId];
255
        }
256
257
        // throw a new exception
258
        throw new \Exception(
259
            $this->appendExceptionSuffix(
260
                sprintf('Can\'t find visibility mapping for entity ID "%d"', $entityId)
261
            )
262
        );
263
    }
264
265
    /**
266
     * Add the passed category ID to the product's category list.
267
     *
268
     * @param integer $categoryId The category ID to add
269
     *
270
     * @return void
271
     */
272
    public function addProductCategoryId($categoryId)
273
    {
274
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
275
    }
276
277
    /**
278
     * Pre-load the entity ID for the passed product.
279
     *
280
     * @param array $product The product to be pre-loaded
281
     *
282
     * @return void
283
     */
284
    public function preLoadEntityId(array $product)
285
    {
286
        $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID];
287
    }
288
289
    /**
290
     * Return's the list with category IDs the product is related with.
291
     *
292
     * @return array The product's category IDs
293
     */
294
    public function getProductCategoryIds()
295
    {
296
297
        // initialize the array with the product's category IDs
298
        $categoryIds = array();
299
300
        // query whether or not category IDs are available for the actual product entity
301
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
302
            $categoryIds = $this->productCategoryIds[$lastEntityId];
303
        }
304
305
        // return the array with the product's category IDs
306
        return $categoryIds;
307
    }
308
309
    /**
310
     * Return's the entity type for the configured entity type code.
311
     *
312
     * @return array The requested entity type
313
     * @throws \Exception Is thrown, if the requested entity type is not available
314
     */
315 1 View Code Duplication
    public function getEntityType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317
318
        // query whether or not the entity type with the passed code is available
319 1
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
320 1
            return $this->entityTypes[$entityTypeCode];
321
        }
322
323
        // throw a new exception
324
        throw new \Exception(
325
            $this->appendExceptionSuffix(
326
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
327
            )
328
        );
329
    }
330
331
    /**
332
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
333
     *
334
     * @param array $productVarcharAttribute The varchar value to check
335
     *
336
     * @return boolean TRUE if the URL key is related, else FALSE
337
     */
338
    public function isUrlKeyOf(array $productVarcharAttribute)
339
    {
340
        return ($productVarcharAttribute[MemberNames::ENTITY_ID] === $this->getLastEntityId()) && ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === $this->getRowStoreId());
341
    }
342
}
343