Completed
Push — master ( a993d8...24cef3 )
by Tim
10s
created

ProductLinkObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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
     * Will be invoked by the action on the events the listener has been registered for.
59
     *
60
     * @param array $row The row to handle
61
     *
62
     * @return array The modified row
63
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
64
     */
65
    public function handle(array $row)
66
    {
67
68
        // initialize the row
69
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...rs\ProductLinkObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
71
        // process the functionality and return the row
72
        $this->process();
73
74
        // return the processed row
75
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...rs\ProductLinkObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
    }
77
78
    /**
79
     * Process the observer's business logic.
80
     *
81
     * @return array The processed row
82
     */
83
    protected function process()
84
    {
85
86
        // initialize the array for the links
87
        $artefacts = array();
88
89
        // prepare the links for the found link types and merge the found artefacts
90
        foreach ($this->getLinkTypeCodeToColumnsMapping() as $linkTypeCode => $columns) {
91
            $artefacts = array_merge($artefacts, $this->prepareArtefacts($linkTypeCode, $columns));
92
        }
93
94
        // append the links to the subject
95
        $this->addArtefacts($artefacts);
96
    }
97
98
    /**
99
     * Prepare's and return's the artefacts for the passed link type.
100
     *
101
     * @param string $linkTypeCode The link type code to prepare the artefacts for
102
     * @param array  $columns      The column names that contains the data (SKU + position)
103
     *
104
     * @return array The link artefacts assembled from the passed row
105
     */
106
    protected function prepareArtefacts($linkTypeCode, array $columns)
107
    {
108
109
        // initialize the array for the product media
110
        $artefacts = array();
111
112
        // extract the column names
113
        list ($columnNameSku, $columnNamePosition) = $columns;
114
115
        // query whether or not, we've up sell products
116
        if ($links = $this->getValue($columnNameSku, null, array($this, 'explode'))) {
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...rs\ProductLinkObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            // load the parent SKU from the row
118
            $parentSku = $this->getValue(ColumnKeys::SKU);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...rs\ProductLinkObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
            // extract the link positions, if available
120
            $linkPositions = $this->getValue($columnNamePosition, array(), array($this, 'explode'));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...rs\ProductLinkObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
122
            // load the SKUs of the related products
123
            foreach ($links as $key => $childSku) {
124
                // prepare the link position
125
                $linkPosition = $key + 1;
126
                if (isset($linkPositions[$key]) && !empty($linkPositions[$key])) {
127
                    $linkPosition = $linkPositions[$key];
128
                }
129
130
                // prepare and append the relation to the artefacts
131
                $artefacts[] = array(
132
                    ColumnKeys::LINK_PARENT_SKU  => $parentSku,
133
                    ColumnKeys::LINK_CHILD_SKU   => $childSku,
134
                    ColumnKeys::LINK_TYPE_CODE   => $linkTypeCode,
135
                    ColumnKeys::LINK_POSITION    => $linkPosition
136
                );
137
            }
138
        }
139
140
        // return the artefacts
141
        return $artefacts;
142
    }
143
144
    /**
145
     * Return's the link type code => colums mapping.
146
     *
147
     * @return array The mapping with the link type codes => colums
148
     */
149
    protected function getLinkTypeCodeToColumnsMapping()
150
    {
151
        return $this->linkTypeCodeToColumnsMapping;
152
    }
153
154
    /**
155
     * Add the passed product type artefacts to the product with the
156
     * last entity ID.
157
     *
158
     * @param array $artefacts The product type artefacts
159
     *
160
     * @return void
161
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
162
     */
163
    protected function addArtefacts(array $artefacts)
164
    {
165
        $this->getSubject()->addArtefacts(ProductLinkObserver::ARTEFACT_TYPE, $artefacts);
166
    }
167
}
168