1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Link\Observers\LinkUpdateObserver |
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\MemberNames; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Oberserver that provides functionality for the product link add/update operation. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import-product-link |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
class LinkUpdateObserver extends LinkObserver |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize the product link with the passed attributes and returns an instance. |
39
|
|
|
* |
40
|
|
|
* @param array $attr The product link attributes |
41
|
|
|
* |
42
|
|
|
* @return array The initialized product link |
43
|
|
|
*/ |
44
|
|
|
protected function initializeProductLink(array $attr) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// load the product/linked product/link type ID |
48
|
|
|
$productId = $attr[MemberNames::PRODUCT_ID]; |
49
|
|
|
$linkTypeId = $attr[MemberNames::LINK_TYPE_ID]; |
50
|
|
|
$linkedProductId = $attr[MemberNames::LINKED_PRODUCT_ID]; |
51
|
|
|
|
52
|
|
|
// try to load the link with the passed product/linked product/link type ID |
53
|
|
|
if ($entity = $this->loadProductLink($productId, $linkedProductId, $linkTypeId)) { |
54
|
|
|
return $this->mergeEntity($entity, $attr); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// simply return the attributes |
58
|
|
|
return $attr; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Load's the link with the passed product/linked product/link type ID. |
63
|
|
|
* |
64
|
|
|
* @param integer $productId The product ID of the link to load |
65
|
|
|
* @param integer $linkedProductId The linked product ID of the link to load |
66
|
|
|
* @param integer $linkTypeId The link type ID of the product to load |
67
|
|
|
* |
68
|
|
|
* @return array The link |
69
|
|
|
*/ |
70
|
|
|
protected function loadProductLink($productId, $linkedProductId, $linkTypeId) |
71
|
|
|
{ |
72
|
|
|
return $this->getSubject()->loadProductLink($productId, $linkedProductId, $linkTypeId); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.