Skip to content

Commit 96d3862

Browse files
committed
#27499: Corrected services implementation
1 parent 9106468 commit 96d3862

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPath.php renamed to app/code/Magento/MediaGallery/Model/Directory/Command/CreateByPaths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Create folder by provided path
1717
*/
18-
class CreateByPath implements CreateDirectoriesByPathsInterface
18+
class CreateByPaths implements CreateDirectoriesByPathsInterface
1919
{
2020
/**
2121
* @var LoggerInterface

app/code/Magento/MediaGallery/Model/Directory/Command/DeleteByPath.php renamed to app/code/Magento/MediaGallery/Model/Directory/Command/DeleteByPaths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Delete directory from media storage by path
1717
*/
18-
class DeleteByPath implements DeleteDirectoriesByPathsInterface
18+
class DeleteByPaths implements DeleteDirectoriesByPathsInterface
1919
{
2020
/**
2121
* @var LoggerInterface

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ public function execute(array $paths): void
7474
}
7575
}
7676

77-
private function deleteAssetsByDirectoryPath(string $path)
77+
/**
78+
* Delete assets from database based on the first part (beginning) of the path
79+
*
80+
* @param string $path
81+
*/
82+
private function deleteAssetsByDirectoryPath(string $path): void
7883
{
7984
// Make sure that the path has a trailing slash
8085
$path = rtrim($path, '/') . '/';

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,32 @@ public function __construct(
5757
*/
5858
public function execute(array $ids): array
5959
{
60+
$assets = [];
6061
try {
61-
$mediaAssetTable = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
62-
$connection = $this->resourceConnection->getConnection();
63-
$select = $connection->select()
64-
->from(['amg' => $mediaAssetTable])
65-
->where('amg.id IN (?)', $ids);
66-
$assetsData = $connection->query($select)->fetchAll();
62+
foreach ($this->getAssetsData($ids) as $assetData) {
63+
$assets[] = $this->assetFactory->create(['data' => $assetData]);
64+
}
6765
} catch (\Exception $exception) {
6866
$this->logger->critical($exception);
6967
throw new LocalizedException(__('Could not retrieve media assets'), $exception);
7068
}
71-
72-
$assets = [];
73-
74-
foreach ($assetsData as $assetData) {
75-
$assets[] = $this->assetFactory->create(['data' => $assetData]);
76-
}
77-
7869
return $assets;
7970
}
71+
72+
/**
73+
* Retrieve assets data from database
74+
*
75+
* @param array $ids
76+
* @return array
77+
* @throws \Zend_Db_Statement_Exception
78+
*/
79+
private function getAssetsData(array $ids): array
80+
{
81+
$mediaAssetTable = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
82+
$connection = $this->resourceConnection->getConnection();
83+
$select = $connection->select()
84+
->from(['amg' => $mediaAssetTable])
85+
->where('amg.id IN (?)', $ids);
86+
return $connection->query($select)->fetchAll();
87+
}
8088
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ public function __construct(
5858
*/
5959
public function execute(array $paths): array
6060
{
61+
$assets = [];
6162
try {
62-
$connection = $this->resourceConnection->getConnection();
63-
$select = $connection->select()
64-
->from($this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET))
65-
->where(self::MEDIA_GALLERY_ASSET_PATH . ' IN (?)', $paths);
66-
$assetsData = $connection->query($select)->fetchAll();
63+
foreach ($this->getAssetsData($paths) as $assetData) {
64+
$assets[] = $this->mediaAssetFactory->create(['data' => $assetData]);
65+
}
6766
} catch (\Exception $exception) {
6867
$this->logger->critical($exception);
6968
throw new LocalizedException(
@@ -75,13 +74,22 @@ public function execute(array $paths): array
7574
)
7675
);
7776
}
78-
79-
$assets = [];
80-
81-
foreach ($assetsData as $assetData) {
82-
$assets[] = $this->mediaAssetFactory->create(['data' => $assetData]);
83-
}
84-
8577
return $assets;
8678
}
79+
80+
/**
81+
* Retrieve assets data from database
82+
*
83+
* @param array $paths
84+
* @return array
85+
* @throws \Zend_Db_Statement_Exception
86+
*/
87+
private function getAssetsData(array $paths): array
88+
{
89+
$connection = $this->resourceConnection->getConnection();
90+
$select = $connection->select()
91+
->from($this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET))
92+
->where(self::MEDIA_GALLERY_ASSET_PATH . ' IN (?)', $paths);
93+
return $connection->query($select)->fetchAll();
94+
}
8795
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function execute(array $assets): void
7979
__(
8080
'Could not save the media assets: %assets',
8181
[
82-
'assets' => $failedAssets
82+
'assets' => implode(' ,', $failedAssets)
8383
]
8484
)
8585
);

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result,
8888
return $result;
8989
}
9090

91-
$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($target);
91+
$relativePath = $this->getMediaDirectoryRelativePath($target);
9292
if (!$relativePath) {
9393
return $result;
9494
}
@@ -120,13 +120,22 @@ public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
120120
}
121121

122122
try {
123-
$mediaDirectoryRead = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
124-
$relativePath = $mediaDirectoryRead->getRelativePath($path);
125-
$this->deleteMediaAssetByDirectoryPath->execute($relativePath);
123+
$this->deleteMediaAssetByDirectoryPath->execute($this->getMediaDirectoryRelativePath($path));
126124
} catch (ValidatorException $exception) {
127125
$this->logger->critical($exception);
128126
}
129127

130128
return $result;
131129
}
130+
131+
/**
132+
* Get path relative to media directory
133+
*
134+
* @param string $path
135+
* @return string
136+
*/
137+
private function getMediaDirectoryRelativePath(string $path): string
138+
{
139+
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
140+
}
132141
}

app/code/Magento/MediaGallery/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
<preference for="Magento\MediaGalleryApi\Model\Keyword\Command\GetAssetKeywordsInterface" type="Magento\MediaGallery\Model\Keyword\Command\GetAssetKeywords"/>
2020
<preference for="Magento\MediaGalleryApi\Model\Keyword\Command\SaveAssetKeywordsInterface" type="Magento\MediaGallery\Model\Keyword\Command\SaveAssetKeywords"/>
2121

22-
<preference for="Magento\MediaGalleryApi\Api\CreateDirectoriesByPathsInterface" type="Magento\MediaGallery\Model\Directory\Command\CreateByPath"/>
23-
<preference for="Magento\MediaGalleryApi\Api\DeleteDirectoriesByPathsInterface" type="Magento\MediaGallery\Model\Directory\Command\DeleteByPath"/>
22+
<preference for="Magento\MediaGalleryApi\Api\CreateDirectoriesByPathsInterface" type="Magento\MediaGallery\Model\Directory\Command\CreateByPaths"/>
23+
<preference for="Magento\MediaGalleryApi\Api\DeleteDirectoriesByPathsInterface" type="Magento\MediaGallery\Model\Directory\Command\DeleteByPaths"/>
2424

2525
<preference for="Magento\MediaGalleryApi\Api\IsPathBlacklistedInterface" type="Magento\MediaGallery\Model\Directory\IsBlacklisted"/>
2626

0 commit comments

Comments
 (0)