Completed
Push — 19.x ( 0ec2e8...4b334a )
by Tim
04:33
created

BunchSubject::getDefaultCallbackMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Utils\RegistryKeys;
26
use TechDivision\Import\Product\Utils\VisibilityKeys;
27
use TechDivision\Import\Product\Utils\ConfigurationKeys;
28
use TechDivision\Import\Subjects\ExportableTrait;
29
use TechDivision\Import\Subjects\FileUploadTrait;
30
use TechDivision\Import\Subjects\ExportableSubjectInterface;
31
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
32
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface;
33
34
/**
35
 * The subject implementation that handles the business logic to persist products.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import-product
41
 * @link      http://www.techdivision.com
42
 */
43
class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface, FileUploadSubjectInterface, UrlKeyAwareSubjectInterface
44
{
45
46
    /**
47
     * The trait that implements the export functionality.
48
     *
49
     * @var \TechDivision\Import\Subjects\ExportableTrait
50
     */
51
    use ExportableTrait;
52
53
    /**
54
     * The trait that provides file upload functionality.
55
     *
56
     * @var \TechDivision\Import\Subjects\FileUploadTrait
57
     */
58
    use FileUploadTrait;
59
60
    /**
61
     * The array with the pre-loaded entity IDs.
62
     *
63
     * @var array
64
     */
65
    protected $preLoadedEntityIds = array();
66
67
    /**
68
     * Mappings for the table column => CSV column header.
69
     *
70
     * @var array
71
     */
72
    protected $headerStockMappings = array(
73
        'qty'                         => array('qty', 'float'),
74
        'min_qty'                     => array('out_of_stock_qty', 'float'),
75
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
76
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
77
        'backorders'                  => array('allow_backorders', 'int'),
78
        'use_config_backorders'       => array('use_config_backorders', 'int'),
79
        'min_sale_qty'                => array('min_cart_qty', 'float'),
80
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
81
        'max_sale_qty'                => array('max_cart_qty', 'float'),
82
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
83
        'is_in_stock'                 => array('is_in_stock', 'int'),
84
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
85
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
86
        'manage_stock'                => array('manage_stock', 'int'),
87
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
88
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
89
        'qty_increments'              => array('qty_increments', 'float'),
90
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
91
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
92
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
93
    );
94
95
    /**
96
     * The array with the available visibility keys.
97
     *
98
     * @var array
99
     */
100
    protected $availableVisibilities = array(
101
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
102
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
103
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
104
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
105
    );
106
107
    /**
108
     * The category IDs the product is related with.
109
     *
110
     * @var array
111
     */
112
    protected $productCategoryIds = array();
113
114
    /**
115
     * The default callback mappings for the Magento standard product attributes.
116
     *
117
     * @var array
118
     */
119
    protected $defaultCallbackMappings = array(
120
        'visibility'           => array('import_product.callback.visibility'),
121
        'tax_class_id'         => array('import_product.callback.tax.class'),
122
        'bundle_price_type'    => array('import_product_bundle.callback.bundle.type'),
123
        'bundle_sku_type'      => array('import_product_bundle.callback.bundle.type'),
124
        'bundle_weight_type'   => array('import_product_bundle.callback.bundle.type'),
125
        'bundle_price_view'    => array('import_product_bundle.callback.bundle.price.view'),
126
        'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type')
127
    );
128
129
    /**
130
     * The available entity types.
131
     *
132
     * @var array
133
     */
134
    protected $entityTypes = array();
135
136
    /**
137
     * Intializes the previously loaded global data for exactly one bunch.
138
     *
139
     * @param string $serial The serial of the actual import
140
     *
141
     * @return void
142
     */
143 18
    public function setUp($serial)
144
    {
145
146
        // load the status of the actual import
147 18
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
148
149
        // load the global data we've prepared initially
150 18
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
151
152
        // initialize the flag whether to copy images or not
153 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) {
154
            $this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES));
0 ignored issues
show
Documentation introduced by
$this->getConfiguration(...ationKeys::COPY_IMAGES) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
        }
156
157
        // initialize media directory => can be absolute or relative
158 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
159
            $this->setMediaDir(
160
                $this->resolvePath(
161
                    $this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)
162
                )
163
            );
164
        }
165
166
        // initialize images directory => can be absolute or relative
167 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
168
            $this->setImagesFileDir(
169
                $this->resolvePath(
170
                    $this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)
171
                )
172
            );
173
        }
174
175
        // invoke the parent method
176 18
        parent::setUp($serial);
177
    }
178
179
    /**
180
     * Clean up the global data after importing the bunch.
181
     *
182
     * @param string $serial The serial of the actual import
183
     *
184
     * @return void
185
     */
186
    public function tearDown($serial)
187
    {
188
189
        // invoke the parent method
190
        parent::tearDown($serial);
191
192
        // load the registry processor
193
        $registryProcessor = $this->getRegistryProcessor();
194
195
        // update the status
196
        $registryProcessor->mergeAttributesRecursive(
197
            RegistryKeys::STATUS,
198
            array(
199
                RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds,
200
            )
201
        );
202
    }
203
204
    /**
205
     * Return's the default callback mappings.
206
     *
207
     * @return array The default callback mappings
208
     */
209
    public function getDefaultCallbackMappings()
210
    {
211
        return $this->defaultCallbackMappings;
212
    }
213
214
    /**
215
     * Return's the mappings for the table column => CSV column header.
216
     *
217
     * @return array The header stock mappings
218
     */
219 1
    public function getHeaderStockMappings()
220
    {
221 1
        return $this->headerStockMappings;
222
    }
223
224
    /**
225
     * Return's the visibility key for the passed visibility string.
226
     *
227
     * @param string $visibility The visibility string to return the key for
228
     *
229
     * @return integer The requested visibility key
230
     * @throws \Exception Is thrown, if the requested visibility is not available
231
     */
232
    public function getVisibilityIdByValue($visibility)
233
    {
234
235
        // query whether or not, the requested visibility is available
236
        if (isset($this->availableVisibilities[$visibility])) {
237
            // load the visibility ID, add the mapping and return the ID
238
            return $this->availableVisibilities[$visibility];
239
        }
240
241
        // throw an exception, if not
242
        throw new \Exception(
243
            $this->appendExceptionSuffix(
244
                sprintf('Found invalid visibility %s', $visibility)
245
            )
246
        );
247
    }
248
249
    /**
250
     * Add the passed category ID to the product's category list.
251
     *
252
     * @param integer $categoryId The category ID to add
253
     *
254
     * @return void
255
     */
256
    public function addProductCategoryId($categoryId)
257
    {
258
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
259
    }
260
261
    /**
262
     * Pre-load the entity ID for the passed product.
263
     *
264
     * @param array $product The product to be pre-loaded
265
     *
266
     * @return void
267
     */
268
    public function preLoadEntityId(array $product)
269
    {
270
        $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID];
271
    }
272
273
    /**
274
     * Return's the list with category IDs the product is related with.
275
     *
276
     * @return array The product's category IDs
277
     */
278
    public function getProductCategoryIds()
279
    {
280
281
        // initialize the array with the product's category IDs
282
        $categoryIds = array();
283
284
        // query whether or not category IDs are available for the actual product entity
285
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
286
            $categoryIds = $this->productCategoryIds[$lastEntityId];
287
        }
288
289
        // return the array with the product's category IDs
290
        return $categoryIds;
291
    }
292
293
    /**
294
     * Return's the entity type for the configured entity type code.
295
     *
296
     * @return array The requested entity type
297
     * @throws \Exception Is thrown, if the requested entity type is not available
298
     */
299 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...
300
    {
301
302
        // query whether or not the entity type with the passed code is available
303
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
304
            return $this->entityTypes[$entityTypeCode];
305
        }
306
307
        // throw a new exception
308
        throw new \Exception(
309
            $this->appendExceptionSuffix(
310
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
311
            )
312
        );
313
    }
314
315
    /**
316
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
317
     *
318
     * @param array $productVarcharAttribute The varchar value to check
319
     *
320
     * @return boolean TRUE if the URL key is related, else FALSE
321
     */
322
    public function isUrlKeyOf(array $productVarcharAttribute)
323
    {
324
        return ((integer) $productVarcharAttribute[MemberNames::ENTITY_ID] === (integer) $this->getLastEntityId()) &&
325
               ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
326
    }
327
}
328