1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stockbase\Integration\Cron; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\ObjectManagerInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory; |
8
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
9
|
|
|
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource; |
10
|
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection; |
11
|
|
|
use Magento\Catalog\Model\Product as ProductModel; |
12
|
|
|
use Magento\Framework\Url as UrlHelper; |
13
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
14
|
|
|
use Magento\Framework\Filesystem\Io\File; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Stockbase images synchronization cron job. |
18
|
|
|
*/ |
19
|
|
|
class Images |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var LoggerInterface |
23
|
|
|
*/ |
24
|
|
|
private $logger; |
25
|
|
|
/** |
26
|
|
|
* @var StockbaseClientFactory |
27
|
|
|
*/ |
28
|
|
|
private $stockbaseClientFactory; |
29
|
|
|
/** |
30
|
|
|
* @var ObjectManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $objectManager; |
33
|
|
|
/** |
34
|
|
|
* @var StockbaseConfiguration |
35
|
|
|
*/ |
36
|
|
|
private $config; |
37
|
|
|
/** |
38
|
|
|
* @var ProductCollection |
39
|
|
|
*/ |
40
|
|
|
private $productCollection; |
41
|
|
|
/** |
42
|
|
|
* @var ProductModel |
43
|
|
|
*/ |
44
|
|
|
private $product; |
45
|
|
|
/** |
46
|
|
|
* @var UrlHelper |
47
|
|
|
*/ |
48
|
|
|
private $urlHelper; |
49
|
|
|
/** |
50
|
|
|
* Directory List |
51
|
|
|
* |
52
|
|
|
* @var DirectoryList |
53
|
|
|
*/ |
54
|
|
|
protected $directoryList; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* File interface |
58
|
|
|
* |
59
|
|
|
* @var File |
60
|
|
|
*/ |
61
|
|
|
protected $file; |
62
|
|
|
/** |
63
|
|
|
* Images constructor. |
64
|
|
|
* @param LoggerInterface $logger |
65
|
|
|
* @param ObjectManagerInterface $objectManager |
66
|
|
|
* @param StockbaseClientFactory $stockbaseClientFactory |
67
|
|
|
* @param StockbaseConfiguration $config |
68
|
|
|
* @param ProductCollection $productCollection |
69
|
|
|
* @param ProductModel $product |
70
|
|
|
* @param UrlHelper $urlHelper |
71
|
|
|
* @param DirectoryList $directoryList |
72
|
|
|
* @param File $file |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
LoggerInterface $logger, |
76
|
|
|
ObjectManagerInterface $objectManager, |
77
|
|
|
StockbaseClientFactory $stockbaseClientFactory, |
78
|
|
|
StockbaseConfiguration $config, |
79
|
|
|
ProductCollection $productCollection, |
80
|
|
|
ProductModel $product, |
81
|
|
|
UrlHelper $urlHelper, |
82
|
|
|
DirectoryList $directoryList, |
83
|
|
|
File $file |
84
|
|
|
) { |
85
|
|
|
$this->logger = $logger; |
86
|
|
|
$this->stockbaseClientFactory = $stockbaseClientFactory; |
87
|
|
|
$this->objectManager = $objectManager; |
88
|
|
|
$this->config = $config; |
89
|
|
|
$this->productCollection = $productCollection; |
90
|
|
|
$this->product = $product; |
91
|
|
|
$this->urlHelper = $urlHelper; |
92
|
|
|
$this->directoryList = $directoryList; |
93
|
|
|
$this->file = $file; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Executes the job. |
98
|
|
|
*/ |
99
|
|
|
public function execute() |
100
|
|
|
{ |
101
|
|
|
// validate configuration: |
102
|
|
|
if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncEnabled()) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
// start process: |
106
|
|
|
$this->logger->info('Synchronizing Stockbase images...'); |
107
|
|
|
// get all the eans: |
108
|
|
|
$allEans = $this->getAllEans(); |
109
|
|
|
// get processed eans: |
110
|
|
|
$processedEans = json_decode($this->getImagesEans()) ?: array(); |
111
|
|
|
// process 100 unprocessed eans at a time: |
112
|
|
|
$eans = array_slice(array_diff($allEans, $processedEans), 0, 100); |
113
|
|
|
try { |
114
|
|
|
$client = $this->stockbaseClientFactory->create(); |
115
|
|
|
$images = $client->getImages($eans); |
116
|
|
|
} catch (Exception $e) { |
|
|
|
|
117
|
|
|
$this->logger->info('Cron runImageImport error: '.$e->getMessage()); |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
// download and save the images locally: |
121
|
|
|
$this->saveImageForProduct($images); |
|
|
|
|
122
|
|
|
// update the processed images configuration: |
123
|
|
|
$processedEans = array_merge($processedEans, $eans); |
124
|
|
|
$encodedEans = json_encode($processedEans); |
125
|
|
|
$this->saveImagesEans($encodedEans); |
126
|
|
|
$this->logger->info('Stockbase images synchronization complete.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getAllEans() |
133
|
|
|
{ |
134
|
|
|
// get ean attribute: |
135
|
|
|
$attribute = $this->config->getEanFieldName(); |
136
|
|
|
// apply filters and paginate by 100: |
137
|
|
|
$collection = $this->productCollection->addAttributeToSelect($attribute) |
138
|
|
|
->addAttributeToFilter($attribute, array('notnull' => true, 'neq' => '')) // not null and not empty. |
139
|
|
|
->setPageSize(100); |
140
|
|
|
// iterate over the pages: |
141
|
|
|
$eans = array(); |
142
|
|
|
$currentPage = 0; |
143
|
|
|
$lastPage = $collection->getLastPageNumber(); |
144
|
|
|
while ($currentPage < $lastPage) { |
145
|
|
|
// load the data of this page in a single query: |
146
|
|
|
$collection->setCurPage(++$currentPage); |
147
|
|
|
$collection->load(); |
148
|
|
|
// iterate over the products: |
149
|
|
|
foreach ($collection as $product) { |
150
|
|
|
if ($ean = $product->{$attribute}) { |
151
|
|
|
// add the ean if this product has one: |
152
|
|
|
$eans[] = $ean; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
return $eans; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Saves images array from stockbase for given $ean |
161
|
|
|
* |
162
|
|
|
* @param array $images |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function saveImageForProduct($images) |
167
|
|
|
{ |
168
|
|
|
// get product model: |
169
|
|
|
$productModel = $this->product; |
170
|
|
|
// get ean attribute: |
171
|
|
|
$eanField = $this->config->getEanFieldName(); |
172
|
|
|
// get client: |
173
|
|
|
$client = $this->stockbaseClientFactory->create(); |
174
|
|
|
// loop images: |
175
|
|
|
foreach ($images as $image) { |
176
|
|
|
// load product by ean: |
177
|
|
|
$product = $productModel->loadByAttribute($eanField, $image->EAN); |
178
|
|
|
// continue looping if we do not have product: |
179
|
|
|
if (!$product) { |
180
|
|
|
continue; |
181
|
|
|
} |
182
|
|
|
// get image from stockbase: |
183
|
|
|
$protectedImage = $client->getImageFile($image->{'Url'}); |
184
|
|
|
// create temporal folder if it is not exists: |
185
|
|
|
$tmpDir = $this->getMediaDirTmpDir(); |
186
|
|
|
$this->file->checkAndCreateFolder($tmpDir); |
187
|
|
|
// get new file path: |
188
|
|
|
$newFileName = $tmpDir . baseName($image->{'Url'}); |
189
|
|
|
// read file from URL and copy it to the new destination: |
190
|
|
|
$result = $this->file->read($protectedImage, $newFileName); |
191
|
|
|
if ($result) { |
192
|
|
|
if ($product->getMediaGallery() == null) { |
193
|
|
|
$product->setMediaGallery(array('images' => array(), 'values' => array())); |
194
|
|
|
} |
195
|
|
|
// add saved file to the $product gallery: |
196
|
|
|
$product->addImageToMediaGallery( |
197
|
|
|
$newFileName, |
198
|
|
|
array('image', 'small_image', 'thumbnail'), |
199
|
|
|
false, |
200
|
|
|
false |
201
|
|
|
); |
202
|
|
|
// save product: |
203
|
|
|
$product->{'save'}(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Media directory name for the temporary file storage |
211
|
|
|
* pub/media/tmp |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
protected function getMediaDirTmpDir() |
216
|
|
|
{ |
217
|
|
|
return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
|
|
public function getImagesEans() |
224
|
|
|
{ |
225
|
|
|
return $this->config->getImagesEans(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param $eans |
230
|
|
|
*/ |
231
|
|
|
public function saveImagesEans($eans) |
232
|
|
|
{ |
233
|
|
|
$this->config->saveImagesEans($eans); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.