Skip to content

Commit 819fc7c

Browse files
refactor
1 parent f16a12f commit 819fc7c

File tree

1 file changed

+65
-61
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+65
-61
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
4646
* @since 100.0.2
4747
*/
48-
class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
48+
class Product extends AbstractEntity
4949
{
50-
const CONFIG_KEY_PRODUCT_TYPES = 'global/importexport/import_product_types';
50+
public const CONFIG_KEY_PRODUCT_TYPES = 'global/importexport/import_product_types';
51+
private const HASH_ALGORITHM = 'sha256';
5152

5253
/**
5354
* Size of bunch - part of products to save in one step.
@@ -1749,43 +1750,12 @@ protected function _saveProducts()
17491750
$position = 0;
17501751
foreach ($rowImages as $column => $columnImages) {
17511752
foreach ($columnImages as $columnImageKey => $columnImage) {
1752-
$hash = '';
1753-
if (filter_var($columnImage, FILTER_VALIDATE_URL) === false) {
1754-
$filename = $importDir . DIRECTORY_SEPARATOR . $columnImage;
1755-
if ($this->fileDriver->isExists($filename)) {
1756-
$hash = hash_file(
1757-
'sha256',
1758-
$importDir . DIRECTORY_SEPARATOR . $columnImage
1759-
);
1760-
}
1761-
} else {
1762-
$hash = hash_file('sha256', $columnImage);
1763-
}
1753+
$filePath = filter_var($columnImage, FILTER_VALIDATE_URL)
1754+
? $columnImage
1755+
: $importDir . DS . $columnImage;
17641756

1765-
// Add new images
1766-
if (empty($rowExistingImages)) {
1767-
$imageAlreadyExists = false;
1768-
} else {
1769-
$imageAlreadyExists = array_reduce(
1770-
$rowExistingImages,
1771-
function ($exists, $file) use ($hash) {
1772-
if ($exists) {
1773-
return $exists;
1774-
}
1775-
1776-
if (isset($file['hash']) && $file['hash'] === $hash) {
1777-
return $file['value'];
1778-
}
1779-
1780-
return $exists;
1781-
},
1782-
''
1783-
);
1784-
}
1785-
1786-
if ($imageAlreadyExists) {
1787-
$uploadedFile = $imageAlreadyExists;
1788-
} elseif (!isset($uploadedImages[$columnImage])) {
1757+
$uploadedFile = $this->getAlreadyExistedImage($rowExistingImages, $filePath);
1758+
if (!$uploadedFile && !isset($uploadedImages[$columnImage])) {
17891759
$uploadedFile = $this->uploadMediaFiles($columnImage);
17901760
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
17911761
if ($uploadedFile) {
@@ -1800,7 +1770,7 @@ function ($exists, $file) use ($hash) {
18001770
ProcessingError::ERROR_LEVEL_NOT_CRITICAL
18011771
);
18021772
}
1803-
} else {
1773+
} elseif (isset($uploadedImages[$columnImage])) {
18041774
$uploadedFile = $uploadedImages[$columnImage];
18051775
}
18061776

@@ -1954,24 +1924,14 @@ function ($exists, $file) use ($hash) {
19541924
}
19551925
}
19561926

1957-
$this->saveProductEntity(
1958-
$entityRowsIn,
1959-
$entityRowsUp
1960-
)->_saveProductWebsites(
1961-
$this->websitesCache
1962-
)->_saveProductCategories(
1963-
$this->categoriesCache
1964-
)->_saveProductTierPrices(
1965-
$tierPrices
1966-
)->_saveMediaGallery(
1967-
$mediaGallery
1968-
)->_saveProductAttributes(
1969-
$attributes
1970-
)->updateMediaGalleryVisibility(
1971-
$imagesForChangeVisibility
1972-
)->updateMediaGalleryLabels(
1973-
$labelsForUpdate
1974-
);
1927+
$this->saveProductEntity($entityRowsIn, $entityRowsUp)
1928+
->_saveProductWebsites($this->websitesCache)
1929+
->_saveProductCategories($this->categoriesCache)
1930+
->_saveProductTierPrices($tierPrices)
1931+
->_saveMediaGallery($mediaGallery)
1932+
->_saveProductAttributes($attributes)
1933+
->updateMediaGalleryVisibility($imagesForChangeVisibility)
1934+
->updateMediaGalleryLabels($labelsForUpdate);
19751935

19761936
$this->_eventManager->dispatch(
19771937
'catalog_product_import_bunch_save_after',
@@ -1985,6 +1945,51 @@ function ($exists, $file) use ($hash) {
19851945

19861946
// phpcs:enable
19871947

1948+
/**
1949+
* Returns image hash by path
1950+
*
1951+
* @param string $filePath
1952+
* @return string
1953+
*/
1954+
private function getFileHash(string $filePath): string
1955+
{
1956+
try {
1957+
$fileExists = $this->fileDriver->isExists($filePath);
1958+
} catch (\Exception $exception) {
1959+
$fileExists = false;
1960+
}
1961+
1962+
return $fileExists ? hash_file(self::HASH_ALGORITHM, $filePath) : '';
1963+
}
1964+
1965+
/**
1966+
* Returns existed image
1967+
*
1968+
* @param array $imageRow
1969+
* @param string $filePath
1970+
* @return string
1971+
*/
1972+
private function getAlreadyExistedImage(array $imageRow, string $filePath): string
1973+
{
1974+
$hash = $this->getFileHash($filePath);
1975+
1976+
return array_reduce(
1977+
$imageRow,
1978+
function ($exists, $file) use ($hash) {
1979+
if ($exists) {
1980+
return $exists;
1981+
}
1982+
1983+
if (isset($file['hash']) && $file['hash'] === $hash) {
1984+
return $file['value'];
1985+
}
1986+
1987+
return $exists;
1988+
},
1989+
''
1990+
);
1991+
}
1992+
19881993
/**
19891994
* Generate hashes for existing images for comparison with newly uploaded images.
19901995
*
@@ -2001,7 +2006,7 @@ private function addImageHashes(array &$images): void
20012006
foreach ($files as $path => $file) {
20022007
if ($this->fileDriver->isExists($productMediaPath . $file['value'])) {
20032008
$fileName = $productMediaPath . $file['value'];
2004-
$images[$storeId][$sku][$path]['hash'] = hash_file('sha256', $fileName);
2009+
$images[$storeId][$sku][$path]['hash'] = $this->getFileHash($fileName);
20052010
}
20062011
}
20072012
}
@@ -2019,9 +2024,8 @@ private function clearNoSelectionImages($rowImages, $rowData)
20192024
{
20202025
foreach ($rowImages as $column => $columnImages) {
20212026
foreach ($columnImages as $key => $image) {
2022-
if ($image == 'no_selection') {
2023-
unset($rowImages[$column][$key]);
2024-
unset($rowData[$column]);
2027+
if ($image === 'no_selection') {
2028+
unset($rowImages[$column][$key], $rowData[$column]);
20252029
}
20262030
}
20272031
}

0 commit comments

Comments
 (0)