1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Observers\ProductInventoryUpdateObserver |
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 |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Observer that creates/updates the product's inventory. |
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 |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class ProductInventoryUpdateObserver extends ProductInventoryObserver |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize the stock status with the passed attributes and returns an instance. |
39
|
|
|
* |
40
|
|
|
* @param array $attr The stock status attributes |
41
|
|
|
* |
42
|
|
|
* @return array The initialized stock status |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function initializeStockStatus(array $attr) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// load the stock status with the passed product/website/stock ID |
48
|
|
|
$entity = $this->loadStockStatus( |
49
|
|
|
$attr[MemberNames::PRODUCT_ID], |
50
|
|
|
$attr[MemberNames::WEBSITE_ID], |
51
|
|
|
$attr[MemberNames::STOCK_ID] |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
// merge the attributes with the entity, if available |
55
|
|
|
if ($entity) { |
|
|
|
|
56
|
|
|
return $this->mergeEntity($entity, $attr); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// otherwise simply return the attributes |
60
|
|
|
return $attr; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize the stock item with the passed attributes and returns an instance. |
65
|
|
|
* |
66
|
|
|
* @param array $attr The stock item attributes |
67
|
|
|
* |
68
|
|
|
* @return array The initialized stock item |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function initializeStockItem(array $attr) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
// load the stock item with the passed item/product/stock ID |
74
|
|
|
$entity = $this->loadStockItem( |
75
|
|
|
$attr[MemberNames::PRODUCT_ID], |
76
|
|
|
$attr[MemberNames::WEBSITE_ID], |
77
|
|
|
$attr[MemberNames::STOCK_ID] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
// merge the attributes with the entity, if available |
81
|
|
|
if ($entity) { |
|
|
|
|
82
|
|
|
return $this->mergeEntity($entity, $attr); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// otherwise simply return the attributes |
86
|
|
|
return $attr; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load's and return's the stock status with the passed product/website/stock ID. |
91
|
|
|
* |
92
|
|
|
* @param integer $productId The product ID of the stock status to load |
93
|
|
|
* @param integer $websiteId The website ID of the stock status to load |
94
|
|
|
* @param integer $stockId The stock ID of the stock status to load |
95
|
|
|
* |
96
|
|
|
* @return array The stock status |
97
|
|
|
*/ |
98
|
|
|
public function loadStockStatus($productId, $websiteId, $stockId) |
99
|
|
|
{ |
100
|
|
|
return $this->getSubject()->loadStockStatus($productId, $websiteId, $stockId); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Load's and return's the stock status with the passed product/website/stock ID. |
105
|
|
|
* |
106
|
|
|
* @param integer $productId The product ID of the stock item to load |
107
|
|
|
* @param integer $websiteId The website ID of the stock item to load |
108
|
|
|
* @param integer $stockId The stock ID of the stock item to load |
109
|
|
|
* |
110
|
|
|
* @return array The stock item |
111
|
|
|
*/ |
112
|
|
|
public function loadStockItem($productId, $websiteId, $stockId) |
113
|
|
|
{ |
114
|
|
|
return $this->getSubject()->loadStockItem($productId, $websiteId, $stockId); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.