Completed
Push — master ( 71e73c...f5c524 )
by Tim
14s
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
        error_log(__METHOD__ . ':' . __LINE__);
215
216
        // query whether or not, the requested visibility is available
217
        if (isset($this->availableVisibilities[$visibility])) {
218
            // load the visibility ID, add the mapping and return the ID
219
            $visibilityId = $this->availableVisibilities[$visibility];
220
            $this->entityIdVisibilityIdMapping[$this->getLastEntityId()] = $visibilityId;
221
            return $visibilityId;
222
        }
223
224
        // throw an exception, if not
225
        throw new \Exception(
226
            $this->appendExceptionSuffix(
227
                sprintf('Found invalid visibility %s', $visibility)
228
            )
229
        );
230
    }
231
232
    /**
233
     * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created
234
     * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will
235
     * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>.
236
     *
237
     * @return integer The visibility ID
238
     * @throws \Exception Is thrown, if the entity ID has not been mapped
239
     * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue()
240
     */
241 View Code Duplication
    public function getVisibilityIdMapping()
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...
242
    {
243
244
        // query whether or not the SKU has already been mapped to it's visibility
245
        if (isset($this->entityIdVisibilityIdMapping[$entityId = $this->getLastEntityId()])) {
246
            return $this->entityIdVisibilityIdMapping[$entityId];
247
        }
248
249
        // throw a new exception
250
        throw new \Exception(
251
            $this->appendExceptionSuffix(
252
                sprintf('Can\'t find visibility mapping for entity ID "%d"', $entityId)
253
            )
254
        );
255
    }
256
257
    /**
258
     * Add the passed category ID to the product's category list.
259
     *
260
     * @param integer $categoryId The category ID to add
261
     *
262
     * @return void
263
     */
264
    public function addProductCategoryId($categoryId)
265
    {
266
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
267
    }
268
269
    /**
270
     * Pre-load the entity ID for the passed product.
271
     *
272
     * @param array $product The product to be pre-loaded
273
     *
274
     * @return void
275
     */
276
    public function preLoadEntityId(array $product)
277
    {
278
        $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID];
279
    }
280
281
    /**
282
     * Return's the list with category IDs the product is related with.
283
     *
284
     * @return array The product's category IDs
285
     */
286
    public function getProductCategoryIds()
287
    {
288
289
        // initialize the array with the product's category IDs
290
        $categoryIds = array();
291
292
        // query whether or not category IDs are available for the actual product entity
293
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
294
            $categoryIds = $this->productCategoryIds[$lastEntityId];
295
        }
296
297
        // return the array with the product's category IDs
298
        return $categoryIds;
299
    }
300
301
    /**
302
     * Return's the entity type for the configured entity type code.
303
     *
304
     * @return array The requested entity type
305
     * @throws \Exception Is thrown, if the requested entity type is not available
306
     */
307 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...
308
    {
309
310
        // query whether or not the entity type with the passed code is available
311 1
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
312 1
            return $this->entityTypes[$entityTypeCode];
313
        }
314
315
        // throw a new exception
316
        throw new \Exception(
317
            $this->appendExceptionSuffix(
318
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
319
            )
320
        );
321
    }
322
323
    /**
324
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
325
     *
326
     * @param array $productVarcharAttribute The varchar value to check
327
     *
328
     * @return boolean TRUE if the URL key is related, else FALSE
329
     */
330
    public function isUrlKeyOf(array $productVarcharAttribute)
331
    {
332
        return $productVarcharAttribute[MemberNames::ENTITY_ID] === $this->getLastEntityId();
333
    }
334
}
335