|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Magic360\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
|
|
|
* @author Bernhard Wick <[email protected]> |
|
16
|
|
|
* @copyright 2017 TechDivision GmbH <[email protected]> |
|
17
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
18
|
|
|
* @link https://github.com/techdivision/import-product-magic360 |
|
19
|
|
|
* @link http://www.techdivision.com |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace TechDivision\Import\Product\Magic360\Observers; |
|
23
|
|
|
|
|
24
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
25
|
|
|
use TechDivision\Import\Observers\AbstractObserver; |
|
26
|
|
|
use TechDivision\Import\Product\Magic360\Utils\ColumnKeys; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Observer that uploads the file specified in a CSV file's column 'image_path' to a |
|
30
|
|
|
* configurable directoy. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Tim Wagner <[email protected]> |
|
33
|
|
|
* @author Bernhard Wick <[email protected]> |
|
34
|
|
|
* @copyright 2017 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/techdivision/import-product-magic360 |
|
37
|
|
|
* @link http://www.techdivision.com |
|
38
|
|
|
*/ |
|
39
|
|
|
class FileUploadObserver extends AbstractObserver |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
|
46
|
|
|
* |
|
47
|
|
|
* @return array The modified row |
|
48
|
|
|
* @see \TechDivision\Import\Observers\ObserverInterface::handle() |
|
49
|
|
|
*/ |
|
50
|
|
|
public function handle(SubjectInterface $subject) |
|
51
|
|
|
{ |
|
52
|
|
|
|
|
53
|
|
|
// initialize the row |
|
54
|
|
|
$this->setSubject($subject); |
|
55
|
|
|
$this->setRow($subject->getRow()); |
|
56
|
|
|
|
|
57
|
|
|
// process the functionality and return the row |
|
58
|
|
|
$this->process(); |
|
59
|
|
|
|
|
60
|
|
|
// return the processed row |
|
61
|
|
|
return $this->getRow(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Process the observer's business logic. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array The processed row |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function process() |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
// query whether or not, the image changed |
|
73
|
|
|
if ($this->isParentImage($image = $this->getValue($this->getSourceColumn()))) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// initialize the image path |
|
78
|
|
|
$imagePath = $this->getValue($this->getSourceColumn()); |
|
79
|
|
|
|
|
80
|
|
|
// load the subjet |
|
81
|
|
|
$subject = $this->getSubject(); |
|
82
|
|
|
|
|
83
|
|
|
// query whether or not we've to upload the image files |
|
84
|
|
|
if ($subject->hasCopyImages()) { |
|
85
|
|
|
// upload the file and set the new image path |
|
86
|
|
|
$imagePath = $subject->uploadFile($image); |
|
87
|
|
|
|
|
88
|
|
|
// log a message that the image has been copied |
|
89
|
|
|
$subject->getSystemLogger()->debug( |
|
90
|
|
|
sprintf('Successfully copied image %s => %s', $image, $imagePath) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// write the real image path to the target column |
|
95
|
|
|
$this->setValue($this->getTargetColumn(), $imagePath); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return's TRUE if the passed image is the parent one. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $image The imageD to check |
|
102
|
|
|
* |
|
103
|
|
|
* @return boolean TRUE if the passed image is the parent one |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function isParentImage($image) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getSubject()->getParentImage() === $image; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return's the name of the source column with the image path. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string The image path |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getSourceColumn() |
|
116
|
|
|
{ |
|
117
|
|
|
return ColumnKeys::IMAGE_PATH; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Return's the target column with the path of the copied image. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string The path to the copied image |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getTargetColumn() |
|
126
|
|
|
{ |
|
127
|
|
|
return ColumnKeys::IMAGE_PATH_NEW; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
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.