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

LinkAttributePositionObserver::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\LinkAttributePositionObserver
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
 * Oberserver that provides functionality for the product link attribute position replace operation.
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 LinkAttributePositionObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The link attribute that has to be handled by this observer.
41
     *
42
     * @var string
43
     */
44
    const ATTRIBUTE_CODE = 'position';
45
46
    /**
47
     * The product link attribute ID to persist the the position attribute for.
48
     *
49
     * @var integer
50
     */
51
    protected $productLinktAttributeId;
52
53
    /**
54
     * Process the observer's business logic.
55
     *
56
     * @return array The processed row
57
     */
58
    protected function process()
59
    {
60
61
        // initialize the attribute code
62
        $attributeCode = LinkAttributePositionObserver::ATTRIBUTE_CODE;
63
64
        // extract the link type code from the row and load link type ID
65
        $linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($this->getValue(ColumnKeys::LINK_TYPE_CODE));
66
67
        // try to load the product link attribute
68
        if ($productLinkAttribute = $this->getProductLinkAttribute($linkTypeId, $attributeCode)) {
69
            $this->setProductLinkAttributeId($productLinkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_ID]);
70
        } else {
71
            return;
72
        }
73
74
        // prepare, initialize and persist the product link attribute int entity
75
        $productLink = $this->initializeProductLinkAttributeInt($this->prepareAttributes());
76
        $this->persistProductLinkAttributeInt($productLink);
77
    }
78
79
    /**
80
     * Prepare the attributes of the entity that has to be persisted.
81
     *
82
     * @return array The prepared attributes
83
     */
84
    protected function prepareAttributes()
85
    {
86
87
        // load the ID of the last link
88
        $linkId = $this->getLastLinkId();
89
90
        // load the product link attribute ID
91
        $productLinkAttributeId = $this->getProductLinkAttributeId();
92
93
        // load the position value
94
        $value = $this->getValue(ColumnKeys::LINK_POSITION);
95
96
        // initialize and return the entity
97
        return $this->initializeEntity(
98
            array(
99
                MemberNames::PRODUCT_LINK_ATTRIBUTE_ID => $productLinkAttributeId,
100
                MemberNames::LINK_ID                   => $linkId,
101
                MemberNames::VALUE                     => $value
102
            )
103
        );
104
    }
105
106
    /**
107
     * Temporary persist the product link attribute ID.
108
     *
109
     * @param integer $productLinkAttributeId The product link attribute ID
110
     *
111
     * @return void
112
     */
113
    protected function setProductLinkAttributeId($productLinkAttributeId)
114
    {
115
        $this->productLinktAttributeId = $productLinkAttributeId;
116
    }
117
118
    /**
119
     * Return's the temporary persisted product link attribute ID.
120
     *
121
     * @return integer The product link attribute ID
122
     */
123
    protected function getProductLinkAttributeId()
124
    {
125
        return $this->productLinktAttributeId;
126
    }
127
128
    /**
129
     * Return's the link attribute for the passed link type ID and attribute code.
130
     *
131
     * @param integer $linkTypeId    The link type
132
     * @param string  $attributeCode The attribute code
133
     *
134
     * @return array The link attribute
135
     */
136
    protected function getProductLinkAttribute($linkTypeId, $attributeCode)
137
    {
138
        return $this->getSubject()->getProductLinkAttribute($linkTypeId, $attributeCode);
139
    }
140
141
    /**
142
     * Initialize the product link attribute with the passed attributes and returns an instance.
143
     *
144
     * @param array $attr The product link attribute
145
     *
146
     * @return array The initialized product link attribute
147
     */
148
    protected function initializeProductLinkAttributeInt(array $attr)
149
    {
150
        return $attr;
151
    }
152
153
    /**
154
     * Load the temporary persisted the last link ID.
155
     *
156
     * @return integer The last link ID
157
     */
158
    protected function getLastLinkId()
159
    {
160
        return $this->getSubject()->getLastLinkId();
161
    }
162
163
    /**
164
     * Return the link type ID for the passed link type code.
165
     *
166
     * @param string $linkTypeCode The link type code to return the link type ID for
167
     *
168
     * @return integer The mapped link type ID
169
     * @throws \Exception Is thrown if the link type code is not mapped yet
170
     */
171
    protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode)
172
    {
173
        return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode);
174
    }
175
176
    /**
177
     * Persist's the passed product link attribute int data and return's the ID.
178
     *
179
     * @param array $productLinkAttributeInt The product link attribute int data to persist
180
     *
181
     * @return string The ID of the persisted entity
182
     */
183
    protected function persistProductLinkAttributeInt($productLinkAttributeInt)
184
    {
185
        $this->getSubject()->persistProductLinkAttributeInt($productLinkAttributeInt);
186
    }
187
}
188