Completed
Push — master ( 876042...5306a5 )
by Tim
9s
created

BunchSubject::deleteUrlRewrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Category\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-category
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Category\Subjects;
22
23
use TechDivision\Import\Category\Utils\DisplayModeKeys;
24
use TechDivision\Import\Subjects\ExportableTrait;
25
use TechDivision\Import\Subjects\ExportableSubjectInterface;
26
use TechDivision\Import\Category\Utils\MemberNames;
27
use TechDivision\Import\Category\Utils\PageLayoutKeys;
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-category
36
 * @link      http://www.techdivision.com
37
 */
38
class BunchSubject extends AbstractCategorySubject 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 mapping for the supported backend types (for the category entity) => persist methods.
50
     *
51
     * @var array
52
     */
53
    protected $backendTypes = array(
54
        'datetime' => array('persistCategoryDatetimeAttribute', 'loadCategoryDatetimeAttribute'),
55
        'decimal'  => array('persistCategoryDecimalAttribute', 'loadCategoryDecimalAttribute'),
56
        'int'      => array('persistCategoryIntAttribute', 'loadCategoryIntAttribute'),
57
        'text'     => array('persistCategoryTextAttribute', 'loadCategoryTextAttribute'),
58
        'varchar'  => array('persistCategoryVarcharAttribute', 'loadCategoryVarcharAttribute'),
59
        'image'    => array('persistCategoryVarcharAttribute', 'loadCategoryVarcharAttribute')
60
    );
61
62
    /**
63
     * The array with the available display mode keys.
64
     *
65
     * @var array
66
     */
67
    protected $availableDisplayModes = array(
68
        'Products only'             => DisplayModeKeys::DISPLAY_MODE_PRODUCTS_ONLY,
69
        'Static block only'         => DisplayModeKeys::DISPLAY_MODE_STATIC_BLOCK_ONLY,
70
        'Static block and products' => DisplayModeKeys::DISPLAY_MODE_BOTH
71
    );
72
73
    /**
74
     * The array with the available page layout keys.
75
     *
76
     * @var array
77
     */
78
    protected $availablePageLayouts = array(
79
        '1 column'                 => PageLayoutKeys::PAGE_LAYOUT_1_COLUMN,
80
        '2 columns with left bar'  => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_LEFT,
81
        '2 columns with right bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_RIGHT,
82
        '3 columns'                => PageLayoutKeys::PAGE_LAYOUT_3_COLUMNS,
83
        'Empty'                    => PageLayoutKeys::PAGE_LAYOUT_EMPTY
84
    );
85
86
    /**
87
     * The default callback mappings for the Magento standard category attributes.
88
     *
89
     * @var array
90
     */
91
    protected $defaultCallbackMappings = array(
92
        'display_mode' => array('TechDivision\\Import\\Category\\Callbacks\\DisplayModeCallback'),
93
        'page_layout'  => array('TechDivision\\Import\\Category\\Callbacks\\PageLayoutCallback'),
94
    );
95
96
    /**
97
     * Return's an array with the available EAV attributes for the passed is user defined flag.
98
     *
99
     * @param integer $isUserDefined The flag itself
100
     *
101
     * @return array The array with the EAV attributes matching the passed flag
102
     */
103
    public function getEavAttributeByIsUserDefined($isUserDefined = 1)
104
    {
105
        return $this->getCategoryProcessor()->getEavAttributeByIsUserDefined($isUserDefined);
106
    }
107
108
    /**
109
     * Intializes the previously loaded global data for exactly one bunch.
110
     *
111
     * @return void
112
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
113
     */
114
    public function setUp()
115
    {
116
117
        // initialize the callback mappings with the default mappings
118
        $this->callbackMappings = array_merge($this->callbackMappings, $this->defaultCallbackMappings);
119
120
        // load the user defined attributes and add the callback mappings
121
        foreach ($this->getEavAttributeByIsUserDefined() as $eavAttribute) {
122
            // load attribute code and frontend input type
123
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
124
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
125
126
            // query whether or not the array for the mappings has been initialized
127
            if (!isset($this->callbackMappings[$attributeCode])) {
128
                $this->callbackMappings[$attributeCode] = array();
129
            }
130
131
            // set the appropriate callback mapping for the attributes input type
132
            if (isset($this->defaultFrontendInputCallbackMapping[$frontendInput])) {
133
                $this->callbackMappings[$attributeCode][] = $this->defaultFrontendInputCallbackMapping[$frontendInput];
0 ignored issues
show
Bug introduced by
The property defaultFrontendInputCallbackMapping does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
134
            }
135
        }
136
137
        // merge the callback mappings the the one from the configuration file
138
        foreach ($this->getConfiguration()->getCallbacks() as $callbackMappings) {
139
            foreach ($callbackMappings as $attributeCode => $mappings) {
140
                // write a log message, that default callback configuration will
141
                // be overwritten with the one from the configuration file
142
                if (isset($this->callbackMappings[$attributeCode])) {
143
                    $this->getSystemLogger()->notice(
144
                        sprintf('Now override callback mappings for attribute %s with values found in configuration file', $attributeCode)
145
                    );
146
                }
147
148
                // override the attributes callbacks
149
                $this->callbackMappings[$attributeCode] = $mappings;
150
            }
151
        }
152
153
        // invoke the parent method
154
        parent::setUp();
155
    }
156
157
    /**
158
     * Return's the display mode for the passed display mode string.
159
     *
160
     * @param string $displayMode The display mode string to return the key for
161
     *
162
     * @return integer The requested display mode
163
     * @throws \Exception Is thrown, if the requested display mode is not available
164
     */
165 View Code Duplication
    public function getDisplayModeByValue($displayMode)
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...
166
    {
167
168
        // query whether or not, the requested display mode is available
169
        if (isset($this->availableDisplayModes[$displayMode])) {
170
            return $this->availableDisplayModes[$displayMode];
171
        }
172
173
        // throw an exception, if not
174
        throw new \Exception(
175
            sprintf(
176
                'Found invalid display mode %s in file %s on line %d',
177
                $visibility,
178
                $this->getFilename(),
179
                $this->getLineNumber()
180
            )
181
        );
182
    }
183
184
    /**
185
     * Return's the page layout for the passed page layout string.
186
     *
187
     * @param string $pageLayout The page layout string to return the key for
188
     *
189
     * @return integer The requested page layout
190
     * @throws \Exception Is thrown, if the requested page layout is not available
191
     */
192 View Code Duplication
    public function getPageLayoutByValue($pageLayout)
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...
193
    {
194
195
        // query whether or not, the requested display mode is available
196
        if (isset($this->availablePageLayouts[$pageLayout])) {
197
            return $this->availablePageLayouts[$pageLayout];
198
        }
199
200
        // throw an exception, if not
201
        throw new \Exception(
202
            sprintf(
203
                'Found invalid page layout %s in file %s on line %d',
204
                $visibility,
205
                $this->getFilename(),
206
                $this->getLineNumber()
207
            )
208
        );
209
    }
210
211
    /**
212
     * Cast's the passed value based on the backend type information.
213
     *
214
     * @param string $backendType The backend type to cast to
215
     * @param mixed  $value       The value to be casted
216
     *
217
     * @return mixed The casted value
218
     */
219
    public function castValueByBackendType($backendType, $value)
220
    {
221
222
        // cast the value to a valid timestamp
223
        if ($backendType === 'datetime') {
224
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
225
        }
226
227
        // cast the value to a float value
228
        if ($backendType === 'float') {
229
            return (float) $value;
230
        }
231
232
        // cast the value to an integer
233
        if ($backendType === 'int') {
234
            return (int) $value;
235
        }
236
237
        // we don't need to cast strings
238
        return $value;
239
    }
240
241
    /**
242
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
243
     *
244
     * @return array The mapping for the supported backend types
245
     */
246
    public function getBackendTypes()
247
    {
248
        return $this->backendTypes;
249
    }
250
251
    /**
252
     * Return's the category with the passed ID.
253
     *
254
     * @param string $id The ID of the category to return
255
     *
256
     * @return array The category
257
     */
258
    public function loadCategory($id)
259
    {
260
        return $this->getCategoryProcessor()->loadCategory($id);
261
    }
262
263
    /**
264
     * Load's and return's the datetime attribute with the passed entity/attribute/store ID.
265
     *
266
     * @param integer $entityId    The entity ID of the attribute
267
     * @param integer $attributeId The attribute ID of the attribute
268
     * @param integer $storeId     The store ID of the attribute
269
     *
270
     * @return array|null The datetime attribute
271
     */
272
    public function loadCategoryDatetimeAttribute($entityId, $attributeId, $storeId)
273
    {
274
        return $this->getCategoryProcessor()->loadCategoryDatetimeAttribute($entityId, $attributeId, $storeId);
275
    }
276
277
    /**
278
     * Load's and return's the decimal attribute with the passed entity/attribute/store ID.
279
     *
280
     * @param integer $entityId    The entity ID of the attribute
281
     * @param integer $attributeId The attribute ID of the attribute
282
     * @param integer $storeId     The store ID of the attribute
283
     *
284
     * @return array|null The decimal attribute
285
     */
286
    public function loadCategoryDecimalAttribute($entityId, $attributeId, $storeId)
287
    {
288
        return $this->getCategoryProcessor()->loadCategoryDecimalAttribute($entityId, $attributeId, $storeId);
289
    }
290
291
    /**
292
     * Load's and return's the integer attribute with the passed entity/attribute/store ID.
293
     *
294
     * @param integer $entityId    The entity ID of the attribute
295
     * @param integer $attributeId The attribute ID of the attribute
296
     * @param integer $storeId     The store ID of the attribute
297
     *
298
     * @return array|null The integer attribute
299
     */
300
    public function loadCategoryIntAttribute($entityId, $attributeId, $storeId)
301
    {
302
        return $this->getCategoryProcessor()->loadCategoryIntAttribute($entityId, $attributeId, $storeId);
303
    }
304
305
    /**
306
     * Load's and return's the text attribute with the passed entity/attribute/store ID.
307
     *
308
     * @param integer $entityId    The entity ID of the attribute
309
     * @param integer $attributeId The attribute ID of the attribute
310
     * @param integer $storeId     The store ID of the attribute
311
     *
312
     * @return array|null The text attribute
313
     */
314
    public function loadCategoryTextAttribute($entityId, $attributeId, $storeId)
315
    {
316
        return $this->getCategoryProcessor()->loadCategoryTextAttribute($entityId, $attributeId, $storeId);
317
    }
318
319
    /**
320
     * Load's and return's the varchar attribute with the passed entity/attribute/store ID.
321
     *
322
     * @param integer $entityId    The entity ID of the attribute
323
     * @param integer $attributeId The attribute ID of the attribute
324
     * @param integer $storeId     The store ID of the attribute
325
     *
326
     * @return array|null The varchar attribute
327
     */
328
    public function loadCategoryVarcharAttribute($entityId, $attributeId, $storeId)
329
    {
330
        return $this->getCategoryProcessor()->loadCategoryVarcharAttribute($entityId, $attributeId, $storeId);
331
    }
332
333
    /**
334
     * Persist's the passed category data and return's the ID.
335
     *
336
     * @param array $category The category data to persist
337
     *
338
     * @return string The ID of the persisted entity
339
     */
340
    public function persistCategory($category)
341
    {
342
        return $this->getCategoryProcessor()->persistCategory($category);
343
    }
344
345
    /**
346
     * Persist's the passed category varchar attribute.
347
     *
348
     * @param array $attribute The attribute to persist
349
     *
350
     * @return void
351
     */
352
    public function persistCategoryVarcharAttribute($attribute)
353
    {
354
        $this->getCategoryProcessor()->persistCategoryVarcharAttribute($attribute);
355
    }
356
357
    /**
358
     * Persist's the passed category integer attribute.
359
     *
360
     * @param array $attribute The attribute to persist
361
     *
362
     * @return void
363
     */
364
    public function persistCategoryIntAttribute($attribute)
365
    {
366
        $this->getCategoryProcessor()->persistCategoryIntAttribute($attribute);
367
    }
368
369
    /**
370
     * Persist's the passed category decimal attribute.
371
     *
372
     * @param array $attribute The attribute to persist
373
     *
374
     * @return void
375
     */
376
    public function persistCategoryDecimalAttribute($attribute)
377
    {
378
        $this->getCategoryProcessor()->persistCategoryDecimalAttribute($attribute);
379
    }
380
381
    /**
382
     * Persist's the passed category datetime attribute.
383
     *
384
     * @param array $attribute The attribute to persist
385
     *
386
     * @return void
387
     */
388
    public function persistCategoryDatetimeAttribute($attribute)
389
    {
390
        $this->getCategoryProcessor()->persistCategoryDatetimeAttribute($attribute);
391
    }
392
393
    /**
394
     * Persist's the passed category text attribute.
395
     *
396
     * @param array $attribute The attribute to persist
397
     *
398
     * @return void
399
     */
400
    public function persistCategoryTextAttribute($attribute)
401
    {
402
        $this->getCategoryProcessor()->persistCategoryTextAttribute($attribute);
403
    }
404
405
    /**
406
     * Persist's the URL rewrite with the passed data.
407
     *
408
     * @param array $row The URL rewrite to persist
409
     *
410
     * @return string The ID of the persisted entity
411
     */
412
    public function persistUrlRewrite($row)
413
    {
414
        $this->getCategoryProcessor()->persistUrlRewrite($row);
415
    }
416
417
    /**
418
     * Delete's the URL rewrite(s) with the passed attributes.
419
     *
420
     * @param array       $row  The attributes of the entity to delete
421
     * @param string|null $name The name of the prepared statement that has to be executed
422
     *
423
     * @return void
424
     */
425
    public function deleteUrlRewrite($row, $name = null)
426
    {
427
        $this->getCategoryProcessor()->deleteUrlRewrite($row, $name);
428
    }
429
430
    /**
431
     * Delete's the entity with the passed attributes.
432
     *
433
     * @param array       $row  The attributes of the entity to delete
434
     * @param string|null $name The name of the prepared statement that has to be executed
435
     *
436
     * @return void
437
     */
438
    public function deleteCategory($row, $name = null)
439
    {
440
        $this->getCategoryProcessor()->deleteCategory($row, $name);
441
    }
442
}
443