Completed
Pull Request — master (#7)
by Tim
09:05
created

ProductLinkObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Link\Observers\ProductLinkObserver
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-link
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Link\Observers;
22
23
use TechDivision\Import\Product\Link\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Link\Utils\LinkTypeCodes;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * A SLSB that handles the process to import product links.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-link
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductLinkObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The artefact type.
41
     *
42
     * @var string
43
     */
44
    const ARTEFACT_TYPE = 'links';
45
46
    /**
47
     * The mapping with the link type codes => column names.
48
     *
49
     * @var array
50
     */
51
    protected $linkTypeCodeToColumnsMapping = array(
52
         LinkTypeCodes::RELATION   => array(ColumnKeys::RELATED_SKUS, ColumnKeys::RELATED_POSITION),
53
         LinkTypeCodes::UP_SELL    => array(ColumnKeys::UPSELL_SKUS, ColumnKeys::UPSELL_POSITION),
54
         LinkTypeCodes::CROSS_SELL => array(ColumnKeys::CROSSSELL_SKUS, ColumnKeys::CROSSSELL_POSITION)
55
    );
56
57
    /**
58
     * Process the observer's business logic.
59
     *
60
     * @return array The processed row
61
     */
62
    protected function process()
63
    {
64
65
        // initialize the array for the links
66
        $artefacts = array();
67
68
        // prepare the links for the found link types and merge the found artefacts
69
        foreach ($this->getLinkTypeCodeToColumnsMapping() as $linkTypeCode => $columns) {
70
            $artefacts = array_merge($artefacts, $this->prepareArtefacts($linkTypeCode, $columns));
71
        }
72
73
        // append the links to the subject
74
        $this->addArtefacts($artefacts);
75
    }
76
77
    /**
78
     * Prepare's and return's the artefacts for the passed link type.
79
     *
80
     * @param string $linkTypeCode The link type code to prepare the artefacts for
81
     * @param array  $columns      The column names that contains the data (SKU + position)
82
     *
83
     * @return array The link artefacts assembled from the passed row
84
     */
85
    protected function prepareArtefacts($linkTypeCode, array $columns)
86
    {
87
88
        // initialize the array for the product media
89
        $artefacts = array();
90
91
        // extract the column names
92
        list ($columnNameSku, $columnNamePosition) = $columns;
93
94
        // query whether or not, we've up sell products
95
        if ($links = $this->getValue($columnNameSku, null, array($this, 'explode'))) {
96
            // load the parent SKU from the row
97
            $parentSku = $this->getValue(ColumnKeys::SKU);
98
            // extract the link positions, if available
99
            $linkPositions = $this->getValue($columnNamePosition, array(), array($this, 'explode'));
100
101
            // load the SKUs of the related products
102
            foreach ($links as $key => $childSku) {
103
                // prepare the link position
104
                $linkPosition = $key + 1;
105
                if (isset($linkPositions[$key]) && !empty($linkPositions[$key])) {
106
                    $linkPosition = $linkPositions[$key];
107
                }
108
109
                // prepare and append the relation to the artefacts
110
                $artefacts[] = array(
111
                    ColumnKeys::LINK_PARENT_SKU  => $parentSku,
112
                    ColumnKeys::LINK_CHILD_SKU   => $childSku,
113
                    ColumnKeys::LINK_TYPE_CODE   => $linkTypeCode,
114
                    ColumnKeys::LINK_POSITION    => $linkPosition
115
                );
116
            }
117
        }
118
119
        // return the artefacts
120
        return $artefacts;
121
    }
122
123
    /**
124
     * Return's the link type code => colums mapping.
125
     *
126
     * @return array The mapping with the link type codes => colums
127
     */
128
    protected function getLinkTypeCodeToColumnsMapping()
129
    {
130
        return $this->linkTypeCodeToColumnsMapping;
131
    }
132
133
    /**
134
     * Add the passed product type artefacts to the product with the
135
     * last entity ID.
136
     *
137
     * @param array $artefacts The product type artefacts
138
     *
139
     * @return void
140
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
141
     */
142
    protected function addArtefacts(array $artefacts)
143
    {
144
        $this->getSubject()->addArtefacts(ProductLinkObserver::ARTEFACT_TYPE, $artefacts);
145
    }
146
}
147