Completed
Pull Request — master (#73)
by Tim
03:32
created

BunchSubject::preLoadEntityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 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
     */
138 1
    public function setUp($serial)
139
    {
140
141
        // load the status of the actual import
142 1
        $status = $this->getRegistryProcessor()->getAttribute($serial);
143
144
        // load the global data we've prepared initially
145 1
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
146
147
        // invoke the parent method
148 1
        parent::setUp($serial);
149 1
    }
150
151
    /**
152
     * Clean up the global data after importing the bunch.
153
     *
154
     * @param string $serial The serial of the actual import
155
     *
156
     * @return void
157
     */
158
    public function tearDown($serial)
159
    {
160
161
        // invoke the parent method
162
        parent::tearDown($serial);
163
164
        // load the registry processor
165
        $registryProcessor = $this->getRegistryProcessor();
166
167
        // update the status
168
        $registryProcessor->mergeAttributesRecursive(
169
            $serial,
170
            array(
171
                RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds,
172
            )
173
        );
174
    }
175
176
    /**
177
     * Return's the default callback mappings.
178
     *
179
     * @return array The default callback mappings
180
     */
181 1
    public function getDefaultCallbackMappings()
182
    {
183 1
        return $this->defaultCallbackMappings;
184
    }
185
186
    /**
187
     * Return's the mappings for the table column => CSV column header.
188
     *
189
     * @return array The header stock mappings
190
     */
191 1
    public function getHeaderStockMappings()
192
    {
193 1
        return $this->headerStockMappings;
194
    }
195
196
    /**
197
     * Return's the visibility key for the passed visibility string.
198
     *
199
     * @param string $visibility The visibility string to return the key for
200
     *
201
     * @return integer The requested visibility key
202
     * @throws \Exception Is thrown, if the requested visibility is not available
203
     */
204
    public function getVisibilityIdByValue($visibility)
205
    {
206
207
        // query whether or not, the requested visibility is available
208
        if (isset($this->availableVisibilities[$visibility])) {
209
            return $this->availableVisibilities[$visibility];
210
        }
211
212
        // throw an exception, if not
213
        throw new \Exception(
214
            $this->appendExceptionSuffix(
215
                sprintf('Found invalid visibility %s', $visibility)
216
            )
217
        );
218
    }
219
220
    /**
221
     * Add the passed category ID to the product's category list.
222
     *
223
     * @param integer $categoryId The category ID to add
224
     *
225
     * @return void
226
     */
227
    public function addProductCategoryId($categoryId)
228
    {
229
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
230
    }
231
232
    /**
233
     * Pre-load the entity ID for the passed product.
234
     *
235
     * @param array $product The product to be pre-loaded
236
     *
237
     * @return void
238
     */
239
    public function preLoadEntityId(array $product)
240
    {
241
        $this->preLoadedEntityIds[$product[MemberNames::SKU]]= $product[MemberNames::ENTITY_ID];
242
    }
243
244
    /**
245
     * Return's the list with category IDs the product is related with.
246
     *
247
     * @return array The product's category IDs
248
     */
249
    public function getProductCategoryIds()
250
    {
251
252
        // initialize the array with the product's category IDs
253
        $categoryIds = array();
254
255
        // query whether or not category IDs are available for the actual product entity
256
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
257
            $categoryIds = $this->productCategoryIds[$lastEntityId];
258
        }
259
260
        // return the array with the product's category IDs
261
        return $categoryIds;
262
    }
263
264
    /**
265
     * Return's the entity type for the configured entity type code.
266
     *
267
     * @return array The requested entity type
268
     * @throws \Exception Is thrown, if the requested entity type is not available
269
     */
270 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...
271
    {
272
273
        // query whether or not the entity type with the passed code is available
274 1
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
275 1
            return $this->entityTypes[$entityTypeCode];
276
        }
277
278
        // throw a new exception
279
        throw new \Exception(
280
            $this->appendExceptionSuffix(
281
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
282
            )
283
        );
284
    }
285
286
    /**
287
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
288
     *
289
     * @param array $productVarcharAttribute The varchar value to check
290
     *
291
     * @return boolean TRUE if the URL key is related, else FALSE
292
     */
293
    public function isUrlKeyOf(array $productVarcharAttribute)
294
    {
295
        return $productVarcharAttribute[MemberNames::ENTITY_ID] === $this->getLastEntityId();
296
    }
297
}
298