Skip to content

Commit 212372b

Browse files
authored
ENGCOM-7489: Refactoring of the plugin responsible for the image/directory delete functionality + adjust tests #27994
2 parents ce0f437 + 4af3b0b commit 212372b

File tree

6 files changed

+99
-218
lines changed

6 files changed

+99
-218
lines changed

app/code/Magento/MediaGallery/Model/ResourceModel/DeleteAssetsByPaths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DeleteAssetsByPaths implements DeleteAssetsByPathsInterface
3232
private $logger;
3333

3434
/**
35-
* DeleteById constructor.
35+
* DeleteAssetsByPaths constructor.
3636
*
3737
* @param ResourceConnection $resourceConnection
3838
* @param LoggerInterface $logger

app/code/Magento/MediaGallery/Plugin/Wysiwyg/Images/Storage.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,22 @@
88

99
namespace Magento\MediaGallery\Plugin\Wysiwyg\Images;
1010

11-
use Magento\Framework\Exception\CouldNotDeleteException;
12-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface;
13-
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface;
14-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
11+
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
1512
use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject;
1613
use Magento\Framework\App\Filesystem\DirectoryList;
1714
use Magento\Framework\Filesystem;
1815
use Psr\Log\LoggerInterface;
19-
use Magento\Framework\Exception\ValidatorException;
2016

2117
/**
2218
* Ensures that metadata is removed from the database when a file is deleted and it is an image
2319
*/
2420
class Storage
2521
{
2622
/**
27-
* @var GetByPathInterface
28-
*/
29-
private $getMediaAssetByPath;
30-
31-
/**
32-
* @var DeleteByPathInterface
23+
* @var DeleteAssetsByPathsInterface
3324
*/
3425
private $deleteMediaAssetByPath;
3526

36-
/**
37-
* @var DeleteByDirectoryPathInterface
38-
*/
39-
private $deleteMediaAssetByDirectoryPath;
40-
4127
/**
4228
* @var Filesystem
4329
*/
@@ -51,22 +37,16 @@ class Storage
5137
/**
5238
* Storage constructor.
5339
*
54-
* @param GetByPathInterface $getMediaAssetByPath
55-
* @param DeleteByPathInterface $deleteMediaAssetByPath
56-
* @param DeleteByDirectoryPathInterface $deleteByDirectoryPath
40+
* @param DeleteAssetsByPathsInterface $deleteMediaAssetByPath
5741
* @param Filesystem $filesystem
5842
* @param LoggerInterface $logger
5943
*/
6044
public function __construct(
61-
GetByPathInterface $getMediaAssetByPath,
62-
DeleteByPathInterface $deleteMediaAssetByPath,
63-
DeleteByDirectoryPathInterface $deleteByDirectoryPath,
45+
DeleteAssetsByPathsInterface $deleteMediaAssetByPath,
6446
Filesystem $filesystem,
6547
LoggerInterface $logger
6648
) {
67-
$this->getMediaAssetByPath = $getMediaAssetByPath;
6849
$this->deleteMediaAssetByPath = $deleteMediaAssetByPath;
69-
$this->deleteMediaAssetByDirectoryPath = $deleteByDirectoryPath;
7050
$this->filesystem = $filesystem;
7151
$this->logger = $logger;
7252
}
@@ -94,7 +74,7 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result,
9474
}
9575

9676
try {
97-
$this->deleteMediaAssetByPath->execute($relativePath);
77+
$this->deleteMediaAssetByPath->execute([$relativePath]);
9878
} catch (\Exception $exception) {
9979
$this->logger->critical($exception);
10080
}
@@ -120,8 +100,12 @@ public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
120100
}
121101

122102
try {
123-
$this->deleteMediaAssetByDirectoryPath->execute($this->getMediaDirectoryRelativePath($path));
124-
} catch (ValidatorException $exception) {
103+
$this->deleteMediaAssetByPath->execute(
104+
[
105+
$this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path)
106+
]
107+
);
108+
} catch (\Exception $exception) {
125109
$this->logger->critical($exception);
126110
}
127111

@@ -136,6 +120,8 @@ public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
136120
*/
137121
private function getMediaDirectoryRelativePath(string $path): string
138122
{
139-
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
123+
$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
124+
125+
return ($relativePath && false === strpos($relativePath, '/')) ? '/' . $relativePath : $relativePath;
140126
}
141127
}

app/code/Magento/MediaGallery/Test/Unit/Plugin/Images/StorageTest.php

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)