Completed
Pull Request — master (#5)
by Tim
04:05
created

getProductLinkAttribute()   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\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
     * Will be invoked by the action on the events the listener has been registered for.
55
     *
56
     * @param array $row The row to handle
57
     *
58
     * @return array The modified row
59
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
60
     */
61
    public function handle(array $row)
62
    {
63
64
        // initialize the row
65
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...ributePositionObserver>.

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...
66
67
        // process the functionality and return the row
68
        $this->process();
69
70
        // return the processed row
71
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...ributePositionObserver>.

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...
72
    }
73
74
    /**
75
     * Process the observer's business logic.
76
     *
77
     * @return array The processed row
78
     */
79
    protected function process()
80
    {
81
82
        // initialize the attribute code
83
        $attributeCode = LinkAttributePositionObserver::ATTRIBUTE_CODE;
84
85
        // extract the link type code from the row and load link type ID
86
        $linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($this->getValue(ColumnKeys::LINK_TYPE_CODE));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ributePositionObserver>.

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...
87
88
        // try to load the product link attribute
89
        if ($productLinkAttribute = $this->getProductLinkAttribute($linkTypeId, $attributeCode)) {
90
            $this->setProductLinkAttributeId($productLinkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_ID]);
91
        } else {
92
            return;
93
        }
94
95
        // prepare, initialize and persist the product link attribute int entity
96
        $productLink = $this->initializeProductLinkAttributeInt($this->prepareAttributes());
97
        $this->persistProductLinkAttributeInt($productLink);
98
    }
99
100
    /**
101
     * Prepare the attributes of the entity that has to be persisted.
102
     *
103
     * @return array The prepared attributes
104
     */
105
    protected function prepareAttributes()
106
    {
107
108
        // load the ID of the last link
109
        $linkId = $this->getLastLinkId();
110
111
        // load the product link attribute ID
112
        $productLinkAttributeId = $this->getProductLinkAttributeId();
113
114
        // load the position value
115
        $value = $this->getValue(ColumnKeys::LINK_POSITION);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ributePositionObserver>.

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...
116
117
        // initialize and return the entity
118
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...ributePositionObserver>.

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
            array(
120
                MemberNames::PRODUCT_LINK_ATTRIBUTE_ID => $productLinkAttributeId,
121
                MemberNames::LINK_ID                   => $linkId,
122
                MemberNames::VALUE                     => $value
123
            )
124
        );
125
    }
126
127
    /**
128
     * Temporary persist the product link attribute ID.
129
     *
130
     * @param integer $productLinkAttributeId The product link attribute ID
131
     *
132
     * @return void
133
     */
134
    protected function setProductLinkAttributeId($productLinkAttributeId)
135
    {
136
        $this->productLinktAttributeId = $productLinkAttributeId;
137
    }
138
139
    /**
140
     * Return's the temporary persisted product link attribute ID.
141
     *
142
     * @return integer The product link attribute ID
143
     */
144
    protected function getProductLinkAttributeId()
145
    {
146
        return $this->productLinktAttributeId;
147
    }
148
149
    /**
150
     * Return's the link attribute for the passed link type ID and attribute code.
151
     *
152
     * @param integer $linkTypeId    The link type
153
     * @param string  $attributeCode The attribute code
154
     *
155
     * @return array The link attribute
156
     */
157
    protected function getProductLinkAttribute($linkTypeId, $attributeCode)
158
    {
159
        return $this->getSubject()->getProductLinkAttribute($linkTypeId, $attributeCode);
160
    }
161
162
    /**
163
     * Initialize the product link attribute with the passed attributes and returns an instance.
164
     *
165
     * @param array $attr The product link attribute
166
     *
167
     * @return array The initialized product link attribute
168
     */
169
    protected function initializeProductLinkAttributeInt(array $attr)
170
    {
171
        return $attr;
172
    }
173
174
    /**
175
     * Load the temporary persisted the last link ID.
176
     *
177
     * @return integer The last link ID
178
     */
179
    protected function getLastLinkId()
180
    {
181
        return $this->getSubject()->getLastLinkId();
182
    }
183
184
    /**
185
     * Return the link type ID for the passed link type code.
186
     *
187
     * @param string $linkTypeCode The link type code to return the link type ID for
188
     *
189
     * @return integer The mapped link type ID
190
     * @throws \Exception Is thrown if the link type code is not mapped yet
191
     */
192
    protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode)
193
    {
194
        return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode);
195
    }
196
197
    /**
198
     * Persist's the passed product link attribute int data and return's the ID.
199
     *
200
     * @param array $productLinkAttributeInt The product link attribute int data to persist
201
     *
202
     * @return string The ID of the persisted entity
203
     */
204
    protected function persistProductLinkAttributeInt($productLinkAttributeInt)
205
    {
206
        $this->getSubject()->persistProductLinkAttributeInt($productLinkAttributeInt);
207
    }
208
}
209