Completed
Pull Request — master (#68)
by Tim
11:07
created

BunchSubject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 261
Duplicated Lines 5.75 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 29.27%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
lcom 2
cbo 3
dl 15
loc 261
ccs 12
cts 41
cp 0.2927
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderStockMappings() 0 4 1
A getVisibilityIdByValue() 0 15 2
A isUrlKeyOf() 0 4 1
A setUp() 0 12 1
A tearDown() 0 17 1
A getDefaultCallbackMappings() 0 4 1
A addProductCategoryId() 0 4 1
A preLoadEntityId() 0 4 1
A getProductCategoryIds() 0 14 2
A getEntityType() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Subjects\ExportableTrait;
24
use TechDivision\Import\Subjects\ExportableSubjectInterface;
25
use TechDivision\Import\Product\Utils\MemberNames;
26
use TechDivision\Import\Product\Utils\RegistryKeys;
27
use TechDivision\Import\Product\Utils\VisibilityKeys;
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 array with the pre-loaded entity IDs.
50
     *
51
     * @var array
52
     */
53
    protected $preLoadedEntityIds = 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
    );
82
83
    /**
84
     * The array with the available visibility keys.
85
     *
86
     * @var array
87
     */
88
    protected $availableVisibilities = array(
89
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
90
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
91
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
92
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
93
    );
94
95
    /**
96
     * The category IDs the product is related with.
97
     *
98
     * @var array
99
     */
100
    protected $productCategoryIds = array();
101
102
    /**
103
     * The default callback mappings for the Magento standard product attributes.
104
     *
105
     * @var array
106
     */
107
    protected $defaultCallbackMappings = array(
108
        'visibility'           => array('import_product.callback.visibility'),
109
        'tax_class_id'         => array('import_product.callback.tax.class'),
110
        'bundle_price_type'    => array('import_product_bundle.callback.bundle.type'),
111
        'bundle_sku_type'      => array('import_product_bundle.callback.bundle.type'),
112
        'bundle_weight_type'   => array('import_product_bundle.callback.bundle.type'),
113
        'bundle_price_view'    => array('import_product_bundle.callback.bundle.price.view'),
114
        'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type')
115
    );
116
117
    /**
118
     * The used URL keys.
119
     *
120
     * @var array
121
     */
122
    protected $usedUrlKeys = array();
123
124
    /**
125
     * The available entity types.
126
     *
127
     * @var array
128
     */
129
    protected $entityTypes = array();
130
131
    /**
132
     * Intializes the previously loaded global data for exactly one bunch.
133
     *
134
     * @param string $serial The serial of the actual import
135
     *
136
     * @return void
137
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
138
     */
139 1
    public function setUp($serial)
140
    {
141
142
        // load the status of the actual import
143 1
        $status = $this->getRegistryProcessor()->getAttribute($serial);
144
145
        // load the global data we've prepared initially
146 1
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
147
148
        // invoke the parent method
149 1
        parent::setUp($serial);
150 1
    }
151
152
    /**
153
     * Clean up the global data after importing the bunch.
154
     *
155
     * @param string $serial The serial of the actual import
156
     *
157
     * @return void
158
     */
159
    public function tearDown($serial)
160
    {
161
162
        // invoke the parent method
163
        parent::tearDown($serial);
164
165
        // load the registry processor
166
        $registryProcessor = $this->getRegistryProcessor();
167
168
        // update the status
169
        $registryProcessor->mergeAttributesRecursive(
170
            $serial,
171
            array(
172
                RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds,
173
            )
174
        );
175
    }
176
177
    /**
178
     * Return's the default callback mappings.
179
     *
180
     * @return array The default callback mappings
181
     */
182 1
    public function getDefaultCallbackMappings()
183
    {
184 1
        return $this->defaultCallbackMappings;
185
    }
186
187
    /**
188
     * Return's the mappings for the table column => CSV column header.
189
     *
190
     * @return array The header stock mappings
191
     */
192 1
    public function getHeaderStockMappings()
193
    {
194 1
        return $this->headerStockMappings;
195
    }
196
197
    /**
198
     * Return's the visibility key for the passed visibility string.
199
     *
200
     * @param string $visibility The visibility string to return the key for
201
     *
202
     * @return integer The requested visibility key
203
     * @throws \Exception Is thrown, if the requested visibility is not available
204
     */
205
    public function getVisibilityIdByValue($visibility)
206
    {
207
208
        // query whether or not, the requested visibility is available
209
        if (isset($this->availableVisibilities[$visibility])) {
210
            return $this->availableVisibilities[$visibility];
211
        }
212
213
        // throw an exception, if not
214
        throw new \Exception(
215
            $this->appendExceptionSuffix(
216
                sprintf('Found invalid visibility %s', $visibility)
217
            )
218
        );
219
    }
220
221
    /**
222
     * Add the passed category ID to the product's category list.
223
     *
224
     * @param integer $categoryId The category ID to add
225
     *
226
     * @return void
227
     */
228
    public function addProductCategoryId($categoryId)
229
    {
230
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
231
    }
232
233
    /**
234
     * Pre-load the entity ID for the passed product.
235
     *
236
     * @param array $product The product to be pre-loaded
237
     *
238
     * @return void
239
     */
240
    public function preLoadEntityId(array $product)
241
    {
242
        $this->preLoadedEntityIds[$product[MemberNames::SKU]]= $product[MemberNames::ENTITY_ID];
243
    }
244
245
    /**
246
     * Return's the list with category IDs the product is related with.
247
     *
248
     * @return array The product's category IDs
249
     */
250
    public function getProductCategoryIds()
251
    {
252
253
        // initialize the array with the product's category IDs
254
        $categoryIds = array();
255
256
        // query whether or not category IDs are available for the actual product entity
257
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
258
            $categoryIds = $this->productCategoryIds[$lastEntityId];
259
        }
260
261
        // return the array with the product's category IDs
262
        return $categoryIds;
263
    }
264
265
    /**
266
     * Return's the entity type for the configured entity type code.
267
     *
268
     * @return array The requested entity type
269
     * @throws \Exception Is thrown, if the requested entity type is not available
270
     */
271 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...
272
    {
273
274
        // query whether or not the entity type with the passed code is available
275 1
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
276 1
            return $this->entityTypes[$entityTypeCode];
277
        }
278
279
        // throw a new exception
280
        throw new \Exception(
281
            $this->appendExceptionSuffix(
282
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
283
            )
284
        );
285
    }
286
287
    /**
288
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
289
     *
290
     * @param array $productVarcharAttribute The varchar value to check
291
     *
292
     * @return boolean TRUE if the URL key is related, else FALSE
293
     */
294
    public function isUrlKeyOf(array $productVarcharAttribute)
295
    {
296
        return $productVarcharAttribute[MemberNames::ENTITY_ID] === $this->getLastEntityId();
297
    }
298
}
299