Completed
Push — master ( 9b3686...89ef78 )
by Bernhard
07:29
created

Magic360ColumnsObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Magic360\Observers\Magic360ImageObserver
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
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2017 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-magic360
19
 * @link      http://www.techdivision.com
20
 */
21
22
namespace TechDivision\Import\Product\Magic360\Observers;
23
24
use TechDivision\Import\Product\Magic360\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Magic360\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Adds the count of the images from within artefacts to the Magic360 tables
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @author    Bernhard Wick <[email protected]>
33
 * @copyright 2017 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-magic360
36
 * @link      http://www.techdivision.com
37
 */
38
class Magic360ColumnsObserver extends AbstractProductImportObserver
39
{
40
41
    /**
42
     * Process the observer's business logic.
43
     *
44
     * @return array The processed row
45
     *
46
     * @throws \Exception Possible error in non-debug mode
47
     */
48
    protected function process()
49
    {
50
        // extract the parent/child ID as well as the link type code from the row
51
        $sku = $this->getValue(ColumnKeys::SKU);
52
53
        // load parent/child IDs and link type ID
54
        $productId = $this->getSubject()->mapSkuToEntityId($sku);
55
56
        $images360Path = $this->getValue(ColumnKeys::IMAGES_PATH);
57
        $mediaFilePath = $this->getSubject()->getMediaDir();
58
        // iterate the images (if any) and initialize an entity for each one
59
        $images = $this->getSubject()->getFilesystem()->listContents($mediaFilePath . DIRECTORY_SEPARATOR . $images360Path);
60
        if (count($images) > 0) {
61
            $entity = $this->initializeMagic360Columns($this->initializeEntity(
62
                array(
63
                    MemberNames::PRODUCT_ID => $productId,
64
                    MemberNames::COLUMNS => count($images)
65
                )
66
            ));
67
            $this->persistMagic360Columns($entity);
68
        }
69
    }
70
71
    /**
72
     * Initialize the magic360 column with the passed attributes and returns an instance.
73
     *
74
     * @param array $attr The product media gallery attributes
75
     *
76
     * @return array The initialized product media gallery
77
     */
78
    protected function initializeMagic360Columns(array $attr)
79
    {
80
        return $attr;
81
    }
82
83
    /**
84
     * Persists the column data and returns the ID.
85
     *
86
     * @param array $productMediaGallery The product media gallery data to persist
87
     *
88
     * @return string The ID of the persisted entity
89
     */
90
    protected function persistMagic360Columns($productMediaGallery)
91
    {
92
        return $this->getSubject()->persistMagic360Columns($productMediaGallery);
93
    }
94
}
95