|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Media\Observers\FileUploadObserver |
|
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\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\ConfigurationKeys; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that uploads the product image files specified in a CSV file to a |
|
28
|
|
|
* configurable directory. |
|
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 |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
class FileUploadObserver extends AbstractProductImportObserver |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Process the observer's business logic. |
|
41
|
|
|
* |
|
42
|
|
|
* @return array The processed row |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function process() |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
// query whether or not we've the flag to upload the image files has been set |
|
48
|
|
|
if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) { |
|
49
|
|
|
// query whether or not we've to upload the image files |
|
50
|
|
|
if ($this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)) { |
|
51
|
|
|
// initialize the array for the actual images |
|
52
|
|
|
$actualImageNames = array(); |
|
53
|
|
|
|
|
54
|
|
|
// iterate over the available image fields |
|
55
|
|
|
foreach (array_keys($this->getImageTypes()) as $imageColumnName) { |
|
56
|
|
|
// do nothing if the column is empty |
|
57
|
|
|
if (!$this->hasValue($imageColumnName)) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// override the image path with the new one, if the image has already been processed |
|
62
|
|
|
if (isset($actualImageNames[$imageName = $this->getValue($imageColumnName)])) { |
|
63
|
|
|
$this->setValue($imageColumnName, $actualImageNames[$imageName]); |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// upload the file and set the new image path |
|
68
|
|
|
$imagePath = $this->getSubject()->uploadFile($imageName); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// log a message that the image has been copied |
|
71
|
|
|
$this->getSubject() |
|
72
|
|
|
->getSystemLogger() |
|
73
|
|
|
->debug( |
|
74
|
|
|
sprintf( |
|
75
|
|
|
'Successfully copied image type %s with name %s => %s', |
|
76
|
|
|
$imageColumnName, |
|
77
|
|
|
$imageName, |
|
78
|
|
|
$imagePath |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
// override the image path with the new one |
|
83
|
|
|
$this->setValue($imageColumnName, $imagePath); |
|
84
|
|
|
|
|
85
|
|
|
// add the image to the list with processed images |
|
86
|
|
|
$actualImageNames[$imageName] = $imagePath; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// query whether or not, we've additional images |
|
90
|
|
|
if ($additionalImages = $this->getValue(ColumnKeys::ADDITIONAL_IMAGES, null, array($this, 'explode'))) { |
|
91
|
|
|
// process all the additional images |
|
92
|
|
|
foreach ($additionalImages as $key => $additionalImageName) { |
|
93
|
|
|
// query whether or not the image has already been processed |
|
94
|
|
|
if (isset($actualImageNames[$additionalImageName])) { |
|
95
|
|
|
// if yes, override the image path |
|
96
|
|
|
$additionalImages[$key] = $imagePath; |
|
|
|
|
|
|
97
|
|
|
// and override the image path with the new one |
|
98
|
|
|
$this->setValue($imageColumnName, $actualImageNames[$additionalImageName]); |
|
|
|
|
|
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// upload the file and set the new image path |
|
103
|
|
|
$imagePath = $this->getSubject()->uploadFile($additionalImageName); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
// log a message that the image has been copied |
|
106
|
|
|
$this->getSubject() |
|
107
|
|
|
->getSystemLogger() |
|
108
|
|
|
->debug( |
|
109
|
|
|
sprintf( |
|
110
|
|
|
'Successfully copied additional image wth name %s => %s', |
|
111
|
|
|
$additionalImageName, |
|
112
|
|
|
$imagePath |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
// override the image path |
|
117
|
|
|
$additionalImages[$key] = $imagePath; |
|
118
|
|
|
|
|
119
|
|
|
// add the image to the list with processed images |
|
120
|
|
|
$actualImageNames[$additionalImageName] = $imagePath; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// override the image paths with the new one |
|
124
|
|
|
$this->setValue(ColumnKeys::ADDITIONAL_IMAGES, implode(',', $additionalImages)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return's the array with the available image types and their label columns. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array The array with the available image types |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getImageTypes() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->getSubject()->getImageTypes(); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
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: