Completed
Pull Request — master (#80)
by Tim
03:18
created

BunchSubject::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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