Skip to content

Commit 449068c

Browse files
committed
ACP2E-2093: Issue when uploading image with the same name while deleting the old image
- Addressed the CR comments.
1 parent f286b19 commit 449068c

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/UpdateHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class UpdateHandler extends CreateHandler
3535
/**
3636
* @var AttributeValue
3737
*/
38-
private mixed $attributeValue;
38+
private AttributeValue $attributeValue;
3939

4040
/**
4141
* @var RemoveDeletedImagesFromCache
4242
*/
43-
private mixed $removeDeletedImagesFromCache;
43+
private RemoveDeletedImagesFromCache $removeDeletedImagesFromCache;
4444

4545
/**
4646
* @param MetadataPool $metadataPool

app/code/Magento/Catalog/Model/Product/Image/RemoveDeletedImagesFromCache.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
use Magento\Framework\App\Filesystem\DirectoryList;
1313
use Magento\Framework\Encryption\Encryptor;
1414
use Magento\Framework\Encryption\EncryptorInterface;
15-
use Magento\Framework\Exception\FileSystemException;
1615
use Magento\Framework\Filesystem;
1716
use Magento\Framework\Filesystem\Directory\WriteInterface;
1817
use Magento\Framework\View\ConfigInterface;
1918

2019
/**
2120
* Delete image from cache
22-
*
23-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2421
*/
2522
class RemoveDeletedImagesFromCache
2623
{
@@ -61,7 +58,6 @@ class RemoveDeletedImagesFromCache
6158
* @param Filesystem $filesystem
6259
* @param ParamsBuilder $imageParamsBuilder
6360
* @param ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat
64-
* @throws FileSystemException
6561
*/
6662
public function __construct(
6763
ConfigInterface $presentationConfig,
@@ -83,38 +79,40 @@ public function __construct(
8379
* Remove deleted images from cache.
8480
*
8581
* @param array $files
86-
* @throws FileSystemException
82+
*
83+
* @return void
8784
*/
88-
public function removeDeletedImagesFromCache(array $files)
85+
public function removeDeletedImagesFromCache(array $files): void
8986
{
90-
if (count($files) > 0) {
91-
$images = $this->presentationConfig
92-
->getViewConfig(['area' => \Magento\Framework\App\Area::AREA_FRONTEND])
93-
->getMediaEntities(
94-
'Magento_Catalog',
95-
Image::MEDIA_TYPE_CONFIG_NODE
96-
);
87+
if (count($files) === 0) {
88+
return;
89+
}
90+
$images = $this->presentationConfig
91+
->getViewConfig(['area' => \Magento\Framework\App\Area::AREA_FRONTEND])
92+
->getMediaEntities(
93+
'Magento_Catalog',
94+
Image::MEDIA_TYPE_CONFIG_NODE
95+
);
9796

98-
foreach ($images as $imageData) {
99-
$imageMiscParams = $this->imageParamsBuilder->build($imageData);
97+
foreach ($images as $imageData) {
98+
$imageMiscParams = $this->imageParamsBuilder->build($imageData);
10099

101-
if (isset($imageMiscParams['image_type'])) {
102-
unset($imageMiscParams['image_type']);
103-
}
100+
if (isset($imageMiscParams['image_type'])) {
101+
unset($imageMiscParams['image_type']);
102+
}
104103

105-
$cacheId = $this->encryptor->hash(
106-
implode('_', $this->convertImageMiscParamsToReadableFormat
107-
->convertImageMiscParamsToReadableFormat($imageMiscParams)),
108-
Encryptor::HASH_VERSION_MD5
109-
);
104+
$cacheId = $this->encryptor->hash(
105+
implode('_', $this->convertImageMiscParamsToReadableFormat
106+
->convertImageMiscParamsToReadableFormat($imageMiscParams)),
107+
Encryptor::HASH_VERSION_MD5
108+
);
110109

111-
$catalogPath = $this->mediaConfig->getBaseMediaPath();
110+
$catalogPath = $this->mediaConfig->getBaseMediaPath();
112111

113-
foreach ($files as $filePath) {
114-
$this->mediaDirectory->delete(
115-
$catalogPath . '/cache/' . $cacheId . '/' . $filePath
116-
);
117-
}
112+
foreach ($files as $filePath) {
113+
$this->mediaDirectory->delete(
114+
$catalogPath . '/cache/' . $cacheId . '/' . $filePath
115+
);
118116
}
119117
}
120118
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Image/RemoveDeletedImagesFromCacheTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
use PHPUnit\Framework\TestCase;
2121

2222
/**
23-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
* Test deleted images from the cache
2424
*/
2525
class RemoveDeletedImagesFromCacheTest extends TestCase
2626
{
2727
/**
28-
* @var RemoveDeletedImagesFromCache
28+
* @var RemoveDeletedImagesFromCache|MockObject
2929
*/
3030
protected $model;
3131

@@ -110,9 +110,10 @@ protected function setUp(): void
110110

111111
/**
112112
* @param array $data
113+
* @return void
113114
* @dataProvider createDataProvider
114115
*/
115-
public function testRemoveDeletedImagesFromCache($data)
116+
public function testRemoveDeletedImagesFromCache(array $data): void
116117
{
117118
$this->presentationConfig->expects($this->once())
118119
->method('getViewConfig')
@@ -165,10 +166,10 @@ private function getTestDataWithAttributes(): array
165166
'data' => [
166167
'viewImageConfig' => [
167168
'width' => 100,
168-
'height' => 50, // <===
169+
'height' => 50,
169170
'constrain_only' => false,
170171
'aspect_ratio' => false,
171-
'frame' => true, // <===
172+
'frame' => true,
172173
'transparency' => false,
173174
'background' => '255,255,255',
174175
'type' => 'thumbnail' //thumbnail,small_image,image,swatch_image,swatch_thumb

0 commit comments

Comments
 (0)