|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Observers\PreLoadEntityIdObserver |
|
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\Services\ProductBunchProcessorInterface; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\ColumnKeys; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that pre-loads the entity ID of the product with the SKU found in the CSV file. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import-product |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class PreLoadEntityIdObserver extends AbstractProductImportObserver |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The product bunch processor instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $productBunchProcessor; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initialize the observer with the passed product bunch processor instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function __construct(ProductBunchProcessorInterface $productBunchProcessor) |
|
51
|
|
|
{ |
|
52
|
3 |
|
$this->productBunchProcessor = $productBunchProcessor; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return's the product bunch processor instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
|
59
|
|
|
*/ |
|
60
|
2 |
|
protected function getProductBunchProcessor() |
|
61
|
|
|
{ |
|
62
|
2 |
|
return $this->productBunchProcessor; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Process the observer's business logic. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array The processed row |
|
69
|
|
|
* @throws \Exception Is thrown, if the product with the SKU can not be loaded |
|
70
|
|
|
*/ |
|
71
|
3 |
|
protected function process() |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// query whether or not, we've found a new SKU => means we've found a new product |
|
75
|
3 |
|
if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) { |
|
76
|
1 |
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// preserve the entity ID for the product with the passed SKU |
|
80
|
2 |
|
if ($product = $this->loadProduct($sku)) { |
|
81
|
1 |
|
$this->preLoadEntityId($product); |
|
82
|
1 |
|
} else { |
|
83
|
|
|
// initialize the error message |
|
84
|
1 |
|
$message = sprintf('Can\'t pre-load product with SKU %s', $sku); |
|
85
|
|
|
// load the subject |
|
86
|
1 |
|
$subject = $this->getSubject(); |
|
87
|
|
|
// query whether or not debug mode has been enabled |
|
88
|
1 |
|
if ($subject->isDebugMode()) { |
|
89
|
|
|
$subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message)); |
|
90
|
|
|
} else { |
|
91
|
1 |
|
throw new \Exception($message); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Pre-load the entity ID for the passed product. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $product The product to be pre-loaded |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
1 |
|
protected function preLoadEntityId(array $product) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->getSubject()->preLoadEntityId($product); |
|
|
|
|
|
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Load's and return's the product with the passed SKU. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $sku The SKU of the product to load |
|
112
|
|
|
* |
|
113
|
|
|
* @return array The product |
|
114
|
|
|
*/ |
|
115
|
2 |
|
protected function loadProduct($sku) |
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->getProductBunchProcessor()->loadProduct($sku); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: