Completed
Push — 21.x ( 065cac...7ab77c )
by Marcus
23s queued 12s
created

BunchSubject::getMediaRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\ColumnKeys;
24
use TechDivision\Import\Utils\StoreViewCodes;
25
use TechDivision\Import\Product\Utils\MemberNames;
26
use TechDivision\Import\Product\Utils\RegistryKeys;
27
use TechDivision\Import\Product\Utils\VisibilityKeys;
28
use TechDivision\Import\Product\Utils\ConfigurationKeys;
29
use TechDivision\Import\Subjects\ExportableTrait;
30
use TechDivision\Import\Subjects\FileUploadTrait;
31
use TechDivision\Import\Subjects\ExportableSubjectInterface;
32
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
33
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface;
34
35
/**
36
 * The subject implementation that handles the business logic to persist products.
37
 *
38
 * @author    Tim Wagner <[email protected]>
39
 * @copyright 2016 TechDivision GmbH <[email protected]>
40
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
41
 * @link      https://github.com/techdivision/import-product
42
 * @link      http://www.techdivision.com
43
 */
44
class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface, FileUploadSubjectInterface, UrlKeyAwareSubjectInterface
45
{
46
47
    /**
48
     * The trait that implements the export functionality.
49
     *
50
     * @var \TechDivision\Import\Subjects\ExportableTrait
51
     */
52
    use ExportableTrait;
53
54
    /**
55
     * The trait that provides file upload functionality.
56
     *
57
     * @var \TechDivision\Import\Subjects\FileUploadTrait
58
     */
59
    use FileUploadTrait;
60
61
    /**
62
     * The array with the pre-loaded entity IDs.
63
     *
64
     * @var array
65
     */
66
    protected $preLoadedEntityIds = array();
67
68
    /**
69
     * Mappings for the table column => CSV column header.
70
     *
71
     * @var array
72
     */
73
    protected $headerStockMappings = array(
74
        'qty'                         => array('qty', 'float'),
75
        'min_qty'                     => array('out_of_stock_qty', 'float'),
76
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
77
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
78
        'backorders'                  => array('allow_backorders', 'int'),
79
        'use_config_backorders'       => array('use_config_backorders', 'int'),
80
        'min_sale_qty'                => array('min_cart_qty', 'float'),
81
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
82
        'max_sale_qty'                => array('max_cart_qty', 'float'),
83
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
84
        'is_in_stock'                 => array('is_in_stock', 'int'),
85
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
86
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
87
        'manage_stock'                => array('manage_stock', 'int'),
88
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
89
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
90
        'qty_increments'              => array('qty_increments', 'float'),
91
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
92
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
93
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
94
    );
95
96
    /**
97
     * The array with the available visibility keys.
98
     *
99
     * @var array
100
     */
101
    protected $availableVisibilities = array(
102
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
103
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
104
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
105
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
106
    );
107
108
    /**
109
     * The default callback mappings for the Magento standard product attributes.
110
     *
111
     * @var array
112
     */
113
    protected $defaultCallbackMappings = array(
114
        'visibility'           => array('import_product.callback.visibility'),
115
        'tax_class_id'         => array('import_product.callback.tax.class'),
116
        'bundle_price_type'    => array('import_product_bundle.callback.bundle.type'),
117
        'bundle_sku_type'      => array('import_product_bundle.callback.bundle.type'),
118
        'bundle_weight_type'   => array('import_product_bundle.callback.bundle.type'),
119
        'bundle_price_view'    => array('import_product_bundle.callback.bundle.price.view'),
120
        'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type')
121
    );
122
123
    /**
124
     * The available entity types.
125
     *
126
     * @var array
127
     */
128
    protected $entityTypes = array();
129
130
    /**
131
     * The media roles array (default: ['base', 'small', 'thumbnail', 'swatch']).
132
     *
133
     * @var array
134
     */
135
    protected $mediaRoles = array();
136
137
    /**
138
     * Intializes the previously loaded global data for exactly one bunch.
139
     *
140
     * @param string $serial The serial of the actual import
141
     *
142
     * @return void
143
     */
144 18
    public function setUp($serial)
145
    {
146
147
        // load the status of the actual import
148 18
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
149
150
        // load the global data we've prepared initially
151 18
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
152
153
        // initialize the flag whether to copy images or not
154 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) {
155
            $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...
156
        }
157
158
        // initialize media directory => can be absolute or relative
159 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
160
            $this->setMediaDir(
161
                $this->resolvePath(
162
                    $this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)
163
                )
164
            );
165
        }
166
167
        // initialize images directory => can be absolute or relative
168 18
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
169
            $this->setImagesFileDir(
170
                $this->resolvePath(
171
                    $this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)
172
                )
173
            );
174
        }
175
176
        // invoke the parent method
177 18
        parent::setUp($serial);
178
179
        // create the media roles after parent setup
180 18
        $this->mediaRoles = $this->createMediaRoles();
181 18
    }
182
183
    /**
184
     * Clean up the global data after importing the bunch.
185
     *
186
     * @param string $serial The serial of the actual import
187
     *
188
     * @return void
189
     */
190
    public function tearDown($serial)
191
    {
192
193
        // invoke the parent method
194
        parent::tearDown($serial);
195
196
        // load the registry processor
197
        $registryProcessor = $this->getRegistryProcessor();
198
199
        // update the status
200
        $registryProcessor->mergeAttributesRecursive(
201
            RegistryKeys::STATUS,
202
            array(
203
                RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds,
204
            )
205
        );
206
    }
207
208
    /**
209
     * Return's the default callback mappings.
210
     *
211
     * @return array The default callback mappings
212
     */
213
    public function getDefaultCallbackMappings()
214
    {
215
        return $this->defaultCallbackMappings;
216
    }
217
218
    /**
219
     * Return's the mappings for the table column => CSV column header.
220
     *
221
     * @return array The header stock mappings
222
     */
223 1
    public function getHeaderStockMappings()
224
    {
225 1
        return $this->headerStockMappings;
226
    }
227
228
    /**
229
     * Return's the visibility key for the passed visibility string.
230
     *
231
     * @param string $visibility The visibility string to return the key for
232
     *
233
     * @return integer The requested visibility key
234
     * @throws \Exception Is thrown, if the requested visibility is not available
235
     */
236
    public function getVisibilityIdByValue($visibility)
237
    {
238
239
        // query whether or not, the requested visibility is available
240
        if (isset($this->availableVisibilities[$visibility])) {
241
            // load the visibility ID, add the mapping and return the ID
242
            return $this->availableVisibilities[$visibility];
243
        }
244
245
        // throw an exception, if not
246
        throw new \Exception(
247
            $this->appendExceptionSuffix(
248
                sprintf('Found invalid visibility %s', $visibility)
249
            )
250
        );
251
    }
252
253
    /**
254
     * Pre-load the entity ID for the passed product.
255
     *
256
     * @param array $product The product to be pre-loaded
257
     *
258
     * @return void
259
     */
260
    public function preLoadEntityId(array $product)
261
    {
262
        $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID];
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 ((integer) $productVarcharAttribute[MemberNames::ENTITY_ID] === (integer) $this->getLastEntityId()) &&
297
               ((integer) $productVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
298
    }
299
    /**
300
     * Creates media roles from available image types.
301
     *
302
     * @return array
303
     */
304 18
    public function createMediaRoles()
305
    {
306
307
        // initialize default values
308 18
        $mediaRoles = $this->getMediaRoles();
309
310
        // derive media roles form image types
311 18
        foreach ($this->getImageTypes() as $imageColumnName => $imageLabelColumnName) {
312
            // create the role based prefix for the image columns
313
            $role = str_replace('_image', null, $imageColumnName);
314
315
            // initialize the values for the corresponding media role
316
            $mediaRoles[$role] = array(
317
                ColumnKeys::IMAGE_PATH        => $imageColumnName,
318
                ColumnKeys::IMAGE_LABEL       => $imageLabelColumnName,
319
                ColumnKeys::IMAGE_POSITION    => sprintf('%s_image_position', $role)
320
            );
321
        }
322
323 18
        return $mediaRoles;
324
    }
325
326
    /**
327
     * Returns the media roles.
328
     *
329
     * @return array
330
     */
331 18
    public function getMediaRoles(): array
332
    {
333 18
        return $this->mediaRoles;
334
    }
335
}
336