1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Magic360\Observers\Magic360ImageObserver |
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\Product\Magic360\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Product\Magic360\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface; |
27
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Adds the images from within artefacts to the Magic360 tables |
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 Magic360GalleryObserver extends AbstractProductImportObserver |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The product magic360 processor instance. |
44
|
|
|
* |
45
|
|
|
* @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
46
|
|
|
*/ |
47
|
|
|
protected $productMagic360Processor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initialize the observer with the passed product magic360 processor instance. |
51
|
|
|
* |
52
|
|
|
* @param \TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface $productMagic360Processor The product magic360 processor instance |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ProductMagic360ProcessorInterface $productMagic360Processor) |
55
|
|
|
{ |
56
|
|
|
$this->productMagic360Processor = $productMagic360Processor; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return's the product magic360 processor instance. |
61
|
|
|
* |
62
|
|
|
* @return \TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface The product magic360 processor instance |
63
|
|
|
*/ |
64
|
|
|
protected function getProductMagic360Processor() |
65
|
|
|
{ |
66
|
|
|
return $this->productMagic360Processor; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Process the observer's business logic. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* |
74
|
|
|
* @throws \Exception Possible error in non-debug mode |
75
|
|
|
*/ |
76
|
|
|
protected function process() |
77
|
|
|
{ |
78
|
|
|
|
79
|
|
|
// extract the parent/child ID as well as the link type code from the row |
80
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
81
|
|
|
|
82
|
|
|
// load parent product ID |
83
|
|
|
$productId = $this->getSubject()->mapSkuToEntityId($sku); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// initialize the directories with the images to import |
86
|
|
|
$mediaDir = $this->getSubject()->getMediaDir(); |
|
|
|
|
87
|
|
|
$images360Path = DIRECTORY_SEPARATOR . ltrim($this->getValue(ColumnKeys::IMAGES_PATH), DIRECTORY_SEPARATOR); |
88
|
|
|
|
89
|
|
|
// log a message with the folder name containing the images |
90
|
|
|
$this->getSubject()->getSystemLogger()->debug(sprintf('Now try to load 360° images from directory %s%s', $mediaDir, $images360Path)); |
91
|
|
|
|
92
|
|
|
// iterate the images (if any) and initialize an entity for each one |
93
|
|
|
$images = $this->getSubject()->getFilesystemAdapter()->listContents($mediaDir . $images360Path); |
94
|
|
|
|
95
|
|
|
// query whether or not images are available |
96
|
|
|
if (count($images) > 0) { |
97
|
|
|
// iterate over the found images |
98
|
|
|
/** @var integer $position The image position */ |
99
|
|
|
/** @var string $image The image filename */ |
100
|
|
|
foreach ($images as $position => $image) { |
101
|
|
|
// initialize the entity |
102
|
|
|
$entity = $this->initializeMagic360Gallery( |
103
|
|
|
$this->initializeEntity( |
104
|
|
|
array( |
105
|
|
|
MemberNames::PRODUCT_ID => $productId, |
106
|
|
|
MemberNames::POSITION => $position + 1, |
107
|
|
|
MemberNames::FILE => $images360Path . basename($image) |
108
|
|
|
) |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
// persist the entity |
113
|
|
|
$this->persistMagic360Gallery($entity); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// log a debug message with the number of imported images |
117
|
|
|
$this->getSubject()->getSystemLogger()->debug(sprintf('Successfully imported %d images for product %d', count($images), $productId)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Initialize the magic360 gallery with the passed attributes and returns an instance. |
123
|
|
|
* |
124
|
|
|
* @param array $attr The gallery attributes |
125
|
|
|
* |
126
|
|
|
* @return array The initialized gallery |
127
|
|
|
*/ |
128
|
|
|
protected function initializeMagic360Gallery(array $attr) |
129
|
|
|
{ |
130
|
|
|
return $attr; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Persists the passed magic360 gallery data and returns the ID. |
135
|
|
|
* |
136
|
|
|
* @param array $magic360Gallery The gallery data to persist |
137
|
|
|
* |
138
|
|
|
* @return string The ID of the persisted entity |
139
|
|
|
*/ |
140
|
|
|
protected function persistMagic360Gallery($magic360Gallery) |
141
|
|
|
{ |
142
|
|
|
return $this->getProductMagic360Processor()->persistMagic360Gallery($magic360Gallery); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..