Completed
Push — master ( 4b7e30...1a1173 )
by Tim
11s
created

ProductLinkObserver::newArtefact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\MemberNames;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Prepares the artefacts for the generic link type import.
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
     * Process the observer's business logic.
48
     *
49
     * @return array The processed row
50
     */
51
    protected function process()
52
    {
53
54
        // initialize the array for the links
55
        $artefacts = array();
56
57
        // prepare the links for the found link types and merge the found artefacts
58
        foreach ($this->getLinkTypeMappings() as $linkTypeCode => $columns) {
59
            $artefacts = array_merge($artefacts, $this->prepareArtefacts($linkTypeCode, $columns));
60
        }
61
62
        // append the links to the subject
63
        $this->addArtefacts($artefacts);
64
    }
65
66
    /**
67
     * Prepare's and return's the artefacts for the passed link type.
68
     *
69
     * @param string $linkTypeCode The link type code to prepare the artefacts for
70
     * @param array  $columns      The column names that contains the data (SKU + position)
71
     *
72
     * @return array The link artefacts assembled from the passed row
73
     */
74
    protected function prepareArtefacts($linkTypeCode, array $columns)
75
    {
76
77
        // initialize the array for the product media
78
        $artefacts = array();
79
80
        // extract the column names
81
        list ($columnNameSku, $columnNamePosition) = $columns;
82
83
        // query whether or not, we've up sell products
84
        if ($links = $this->getValue($columnNameSku, null, array($this, 'explode'))) {
85
            // load the parent SKU from the row
86
            $parentSku = $this->getValue(ColumnKeys::SKU);
87
            // extract the link positions, if available
88
            $linkPositions = $this->getValue($columnNamePosition, array(), array($this, 'explode'));
89
90
            // load the SKUs of the related products
91
            foreach ($links as $key => $childSku) {
92
                // prepare the link position
93
                $linkPosition = $key + 1;
94
                if (isset($linkPositions[$key]) && !empty($linkPositions[$key])) {
95
                    $linkPosition = $linkPositions[$key];
96
                }
97
98
                // prepare and append the relation to the artefacts
99
                $artefacts[] = $this->newArtefact(
100
                    array(
101
                        ColumnKeys::LINK_PARENT_SKU => $parentSku,
102
                        ColumnKeys::LINK_CHILD_SKU  => $childSku,
103
                        ColumnKeys::LINK_TYPE_CODE  => $linkTypeCode,
104
                        ColumnKeys::LINK_POSITION   => $linkPosition
105
                    ),
106
                    array(
107
                        ColumnKeys::LINK_PARENT_SKU => ColumnKeys::SKU,
108
                        ColumnKeys::LINK_CHILD_SKU  => $columnNameSku,
109
                        ColumnKeys::LINK_POSITION   => $columnNamePosition
110
                    )
111
                );
112
            }
113
        }
114
115
        // return the artefacts
116
        return $artefacts;
117
    }
118
119
    /**
120
     * Return's the available link types.
121
     *
122
     * @return array The link types
123
     */
124
    protected function getLinkTypes()
125
    {
126
        return $this->getSubject()->getLinkTypes();
127
    }
128
129
    /**
130
     * Return's the link type code => colums mapping.
131
     *
132
     * @return array The mapping with the link type codes => colums
133
     */
134
    protected function getLinkTypeMappings()
135
    {
136
137
        // initialize the array with link type mappings
138
        $linkTypeMappings = array();
139
140
        // prepare the link type mappings
141
        foreach ($this->getLinkTypes() as $linkType) {
142
            $linkTypeMappings[$linkType[MemberNames::CODE]] = array(
143
                sprintf('%s_skus', $linkType[MemberNames::CODE]),
144
                sprintf('%s_position', $linkType[MemberNames::CODE]),
145
            );
146
        }
147
148
        // return the link type mappings
149
        return $linkTypeMappings;
150
    }
151
152
    /**
153
     * Create's and return's a new empty artefact entity.
154
     *
155
     * @param array $columns             The array with the column data
156
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
157
     *
158
     * @return array The new artefact entity
159
     */
160
    protected function newArtefact(array $columns, array $originalColumnNames)
161
    {
162
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
163
    }
164
165
    /**
166
     * Add the passed product type artefacts to the product with the
167
     * last entity ID.
168
     *
169
     * @param array $artefacts The product type artefacts
170
     *
171
     * @return void
172
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
173
     */
174
    protected function addArtefacts(array $artefacts)
175
    {
176
        $this->getSubject()->addArtefacts(ProductLinkObserver::ARTEFACT_TYPE, $artefacts);
177
    }
178
}
179