|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stockbase\Integration\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
|
7
|
|
|
use Magento\Framework\Filesystem\Io\File; |
|
8
|
|
|
use Magento\Catalog\Model\Product as ProductModel; |
|
9
|
|
|
use Stockbase\Integration\Model\ProductImage; |
|
10
|
|
|
use Stockbase\Integration\Model\ResourceModel\ProductImage as ProductImageResource; |
|
11
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* Stockbase images helper. |
|
16
|
|
|
* |
|
17
|
|
|
* Class Images |
|
18
|
|
|
* @package Stockbase\Integration\Helper |
|
19
|
|
|
*/ |
|
20
|
|
|
class Images |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var DirectoryList |
|
29
|
|
|
*/ |
|
30
|
|
|
private $directoryList; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var File |
|
33
|
|
|
*/ |
|
34
|
|
|
private $file; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var ProductImage |
|
37
|
|
|
*/ |
|
38
|
|
|
private $productImage; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var ProductImageResource |
|
41
|
|
|
*/ |
|
42
|
|
|
private $productImageResource; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var ProductModel |
|
45
|
|
|
*/ |
|
46
|
|
|
private $product; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var StockbaseConfiguration |
|
49
|
|
|
*/ |
|
50
|
|
|
private $config; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Images constructor. |
|
54
|
|
|
* @param LoggerInterface $logger |
|
55
|
|
|
* @param StockbaseConfiguration $config |
|
56
|
|
|
* @param ProductImage $productImage |
|
57
|
|
|
* @param ProductImageResource $productImageResource |
|
58
|
|
|
* @param ProductModel $product |
|
59
|
|
|
* @param DirectoryList $directoryList |
|
60
|
|
|
* @param File $file |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
LoggerInterface $logger, |
|
64
|
|
|
StockbaseConfiguration $config, |
|
65
|
|
|
ProductImage $productImage, |
|
66
|
|
|
ProductImageResource $productImageResource, |
|
67
|
|
|
ProductModel $product, |
|
68
|
|
|
DirectoryList $directoryList, |
|
69
|
|
|
File $file |
|
70
|
|
|
) { |
|
71
|
|
|
$this->logger = $logger; |
|
72
|
|
|
$this->config = $config; |
|
73
|
|
|
$this->productImage = $productImage; |
|
74
|
|
|
$this->productImageResource = $productImageResource; |
|
75
|
|
|
$this->product = $product; |
|
76
|
|
|
$this->directoryList = $directoryList; |
|
77
|
|
|
$this->file = $file; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Saves images array from stockbase for given $ean |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $images |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function saveProductImages($images) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->logger->debug('Save images process:'); |
|
90
|
|
|
$newImagesCount = 0; |
|
91
|
|
|
// get product model: |
|
92
|
|
|
$productModel = $this->product; |
|
93
|
|
|
// get ean attribute: |
|
94
|
|
|
$eanField = $this->config->getEanFieldName(); |
|
95
|
|
|
// loop images: |
|
96
|
|
|
foreach ($images as $image) { |
|
97
|
|
|
$this->logger->debug('Image URL: '.$image->{'Url'}.' - EAN: '.$image->EAN); |
|
98
|
|
|
// load product by ean: |
|
99
|
|
|
$product = $productModel->loadByAttribute($eanField, $image->EAN); |
|
100
|
|
|
// continue looping if we do not have product: |
|
101
|
|
|
if (!$product) { |
|
102
|
|
|
$this->logger->debug('Product not found for EAN: '.$image->EAN); |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
$this->logger->debug('Loaded product: '.$product->getId()); |
|
106
|
|
|
if(!$product->getData('stockbase_product')) { |
|
107
|
|
|
$this->logger->debug('The Product is not mark as Stockbase product: '.$product->getId()); |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
// stockbase image: |
|
111
|
|
|
$stockbaseImage = (string)$image->{'Url'}; |
|
112
|
|
|
// image name: |
|
113
|
|
|
$imageName = baseName($image->{'Url'}); |
|
114
|
|
|
// check if the image exists: |
|
115
|
|
|
$imageCollection = $this->productImageResource->imageExists($imageName, $product->getId(), $image->EAN); |
|
116
|
|
|
if(count($imageCollection)>0) { |
|
117
|
|
|
$this->logger->debug('The image '.$imageName.' is already synchronized for product '.$product->getId()); |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
// create temporal folder if it is not exists: |
|
121
|
|
|
$tmpDir = $this->getMediaDirTmpDir(); |
|
122
|
|
|
$this->file->checkAndCreateFolder($tmpDir); |
|
123
|
|
|
// get new file path: |
|
124
|
|
|
$newFileName = $tmpDir . $imageName; |
|
125
|
|
|
// if the image file is not in our system: |
|
126
|
|
|
if(!file_exists($newFileName)) { |
|
127
|
|
|
// read file from URL and copy it to the new destination: |
|
128
|
|
|
$this->file->read($stockbaseImage, $newFileName); |
|
129
|
|
|
$this->logger->debug('New image saved: '. $newFileName); |
|
130
|
|
|
} |
|
131
|
|
|
// if the process worked then the file should be there now: |
|
132
|
|
|
if (file_exists($newFileName)) { |
|
133
|
|
|
// if product gallery is empty the set the default values: |
|
134
|
|
|
$mediaGallery = $product->getMediaGallery(); |
|
135
|
|
|
if (!$mediaGallery) { |
|
136
|
|
|
$product->setMediaGallery(array('images' => array(), 'values' => array())); |
|
137
|
|
|
} else { |
|
138
|
|
|
// if the product has a gallery and the image is already there then continue with the next one: |
|
139
|
|
|
if(is_array($mediaGallery['images']) && in_array($imageName, $mediaGallery['images'])) { |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
// add saved file into the product gallery: |
|
144
|
|
|
$product->addImageToMediaGallery( |
|
145
|
|
|
$newFileName, |
|
146
|
|
|
array('image', 'small_image', 'thumbnail'), |
|
147
|
|
|
false, |
|
148
|
|
|
false |
|
149
|
|
|
); |
|
150
|
|
|
// save product: |
|
151
|
|
|
$product->save(); |
|
152
|
|
|
// save image in relation table: |
|
153
|
|
|
$imageModel = clone($this->productImage); |
|
154
|
|
|
// image data: |
|
155
|
|
|
$imageData = [ |
|
156
|
|
|
'ean' => $image->EAN, |
|
157
|
|
|
'product_id' => $product->getId(), |
|
158
|
|
|
'image' => $imageName, |
|
159
|
|
|
'timestamp' => date('Y-m-d H:i:s') |
|
160
|
|
|
]; |
|
161
|
|
|
// save: |
|
162
|
|
|
$imageModel->setData($imageData)->save(); |
|
163
|
|
|
// end process. |
|
164
|
|
|
$this->logger->debug('Product saved.'); |
|
165
|
|
|
$newImagesCount++; |
|
166
|
|
|
} else { |
|
167
|
|
|
$this->logger->debug('There is an issue with the image: '.$newFileName); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
return $newImagesCount; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Media directory name for the temporary file storage |
|
175
|
|
|
* pub/media/tmp |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
private function getMediaDirTmpDir() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|