45
45
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
46
46
* @since 100.0.2
47
47
*/
48
- class Product extends \ Magento \ ImportExport \ Model \ Import \ Entity \ AbstractEntity
48
+ class Product extends AbstractEntity
49
49
{
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 ' ;
51
52
52
53
/**
53
54
* Size of bunch - part of products to save in one step.
@@ -1749,43 +1750,12 @@ protected function _saveProducts()
1749
1750
$ position = 0 ;
1750
1751
foreach ($ rowImages as $ column => $ columnImages ) {
1751
1752
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 ;
1764
1756
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 ])) {
1789
1759
$ uploadedFile = $ this ->uploadMediaFiles ($ columnImage );
1790
1760
$ uploadedFile = $ uploadedFile ?: $ this ->getSystemFile ($ columnImage );
1791
1761
if ($ uploadedFile ) {
@@ -1800,7 +1770,7 @@ function ($exists, $file) use ($hash) {
1800
1770
ProcessingError::ERROR_LEVEL_NOT_CRITICAL
1801
1771
);
1802
1772
}
1803
- } else {
1773
+ } elseif ( isset ( $ uploadedImages [ $ columnImage ])) {
1804
1774
$ uploadedFile = $ uploadedImages [$ columnImage ];
1805
1775
}
1806
1776
@@ -1954,24 +1924,14 @@ function ($exists, $file) use ($hash) {
1954
1924
}
1955
1925
}
1956
1926
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 );
1975
1935
1976
1936
$ this ->_eventManager ->dispatch (
1977
1937
'catalog_product_import_bunch_save_after ' ,
@@ -1985,6 +1945,51 @@ function ($exists, $file) use ($hash) {
1985
1945
1986
1946
// phpcs:enable
1987
1947
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
+
1988
1993
/**
1989
1994
* Generate hashes for existing images for comparison with newly uploaded images.
1990
1995
*
@@ -2001,7 +2006,7 @@ private function addImageHashes(array &$images): void
2001
2006
foreach ($ files as $ path => $ file ) {
2002
2007
if ($ this ->fileDriver ->isExists ($ productMediaPath . $ file ['value ' ])) {
2003
2008
$ fileName = $ productMediaPath . $ file ['value ' ];
2004
- $ images [$ storeId ][$ sku ][$ path ]['hash ' ] = hash_file ( ' sha256 ' , $ fileName );
2009
+ $ images [$ storeId ][$ sku ][$ path ]['hash ' ] = $ this -> getFileHash ( $ fileName );
2005
2010
}
2006
2011
}
2007
2012
}
@@ -2019,9 +2024,8 @@ private function clearNoSelectionImages($rowImages, $rowData)
2019
2024
{
2020
2025
foreach ($ rowImages as $ column => $ columnImages ) {
2021
2026
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 ]);
2025
2029
}
2026
2030
}
2027
2031
}
0 commit comments