Completed
Pull Request — master (#88)
by Tim
03:01
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
use TechDivision\Import\Utils\StoreViewCodes;
29
30
/**
31
 * The subject implementation that handles the business logic to persist products.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product
37
 * @link      http://www.techdivision.com
38
 */
39
class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface
40
{
41
42
    /**
43
     * The trait that implements the export functionality.
44
     *
45
     * @var \TechDivision\Import\Subjects\ExportableTrait
46
     */
47
    use ExportableTrait;
48
49
    /**
50
     * The array with the pre-loaded entity IDs.
51
     *
52
     * @var array
53
     */
54
    protected $preLoadedEntityIds = array();
55
56
    /**
57
     * Mappings for the table column => CSV column header.
58
     *
59
     * @var array
60
     */
61
    protected $headerStockMappings = array(
62
        'qty'                         => array('qty', 'float'),
63
        'min_qty'                     => array('out_of_stock_qty', 'float'),
64
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
65
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
66
        'backorders'                  => array('allow_backorders', 'int'),
67
        'use_config_backorders'       => array('use_config_backorders', 'int'),
68
        'min_sale_qty'                => array('min_cart_qty', 'float'),
69
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
70
        'max_sale_qty'                => array('max_cart_qty', 'float'),
71
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
72
        'is_in_stock'                 => array('is_in_stock', 'int'),
73
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
74
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
75
        'manage_stock'                => array('manage_stock', 'int'),
76
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
77
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
78
        'qty_increments'              => array('qty_increments', 'float'),
79
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
80
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
81
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
82
    );
83
84
    /**
85
     * The array with the available visibility keys.
86
     *
87
     * @var array
88
     */
89
    protected $availableVisibilities = array(
90
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
91
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
92
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
93
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
94
    );
95
96
    /**
97
     * The category IDs the product is related with.
98
     *
99
     * @var array
100
     */
101
    protected $productCategoryIds = array();
102
103
    /**
104
     * The default callback mappings for the Magento standard product attributes.
105
     *
106
     * @var array
107
     */
108
    protected $defaultCallbackMappings = array(
109
        'visibility'           => array('import_product.callback.visibility'),
110
        'tax_class_id'         => array('import_product.callback.tax.class'),
111
        'bundle_price_type'    => array('import_product_bundle.callback.bundle.type'),
112
        'bundle_sku_type'      => array('import_product_bundle.callback.bundle.type'),
113
        'bundle_weight_type'   => array('import_product_bundle.callback.bundle.type'),
114
        'bundle_price_view'    => array('import_product_bundle.callback.bundle.price.view'),
115
        'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type')
116
    );
117
118
    /**
119
     * The used URL keys.
120
     *
121
     * @var array
122
     */
123
    protected $usedUrlKeys = array();
124
125
    /**
126
     * The available entity types.
127
     *
128
     * @var array
129
     */
130
    protected $entityTypes = array();
131
132
    /**
133
     * Intializes the previously loaded global data for exactly one bunch.
134
     *
135
     * @param string $serial The serial of the actual import
136
     *
137
     * @return void
138
     */
139 18
    public function setUp($serial)
140
    {
141
142
        // load the status of the actual import
143 18
        $status = $this->getRegistryProcessor()->getAttribute($serial);
144
145
        // load the global data we've prepared initially
146 18
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
147
148
        // invoke the parent method
149 18
        parent::setUp($serial);
150 18
    }
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 18
    public function getDefaultCallbackMappings()
183
    {
184 18
        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
            // load the visibility ID, add the mapping and return the ID
211
            return $this->availableVisibilities[$visibility];
212
        }
213
214
        // throw an exception, if not
215
        throw new \Exception(
216
            $this->appendExceptionSuffix(
217
                sprintf('Found invalid visibility %s', $visibility)
218
            )
219
        );
220
    }
221
222
    /**
223
     * Add the passed category ID to the product's category list.
224
     *
225
     * @param integer $categoryId The category ID to add
226
     *
227
     * @return void
228
     */
229
    public function addProductCategoryId($categoryId)
230
    {
231
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
232
    }
233
234
    /**
235
     * Pre-load the entity ID for the passed product.
236
     *
237
     * @param array $product The product to be pre-loaded
238
     *
239
     * @return void
240
     */
241
    public function preLoadEntityId(array $product)
242
    {
243
        $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID];
244
    }
245
246
    /**
247
     * Return's the list with category IDs the product is related with.
248
     *
249
     * @return array The product's category IDs
250
     */
251
    public function getProductCategoryIds()
252
    {
253
254
        // initialize the array with the product's category IDs
255
        $categoryIds = array();
256
257
        // query whether or not category IDs are available for the actual product entity
258
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
259
            $categoryIds = $this->productCategoryIds[$lastEntityId];
260
        }
261
262
        // return the array with the product's category IDs
263
        return $categoryIds;
264
    }
265
266
    /**
267
     * Return's the entity type for the configured entity type code.
268
     *
269
     * @return array The requested entity type
270
     * @throws \Exception Is thrown, if the requested entity type is not available
271
     */
272 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...
273
    {
274
275
        // query whether or not the entity type with the passed code is available
276 1
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
277 1
            return $this->entityTypes[$entityTypeCode];
278
        }
279
280
        // throw a new exception
281
        throw new \Exception(
282
            $this->appendExceptionSuffix(
283
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
284
            )
285
        );
286
    }
287
288
    /**
289
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
290
     *
291
     * @param array $productVarcharAttribute The varchar value to check
292
     *
293
     * @return boolean TRUE if the URL key is related, else FALSE
294
     */
295
    public function isUrlKeyOf(array $productVarcharAttribute)
296
    {
297
        return ((integer) $productVarcharAttribute[MemberNames::ENTITY_ID] === (integer) $this->getLastEntityId()) &&
298
               ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
299
    }
300
}
301