Skip to content

Commit 47ddb3b

Browse files
committed
Code review suggestions
1 parent 6577709 commit 47ddb3b

File tree

4 files changed

+84
-50
lines changed

4 files changed

+84
-50
lines changed

app/code/Magento/MediaGallery/Model/ResourceModel/SearchAssets.php renamed to app/code/Magento/MediaGallery/Model/ResourceModel/GetAssetsBySearchCriteria.php

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
namespace Magento\MediaGallery\Model\ResourceModel;
99

1010
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
12-
use Magento\MediaGalleryApi\Api\SearchAssetsInterface;
1311
use Magento\Framework\App\ResourceConnection;
1412
use Psr\Log\LoggerInterface;
1513
use Magento\Framework\Api\Search\SearchResultInterface;
@@ -19,9 +17,9 @@
1917
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
2018

2119
/**
22-
* Get media assets by searchCriteria
20+
* Get assets data by searchCriteria
2321
*/
24-
class SearchAssets implements SearchAssetsInterface
22+
class GetAssetsBySearchCriteria
2523
{
2624
private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset';
2725

@@ -30,11 +28,6 @@ class SearchAssets implements SearchAssetsInterface
3028
*/
3129
private $resourceConnection;
3230

33-
/**
34-
* @var AssetInterfaceFactory
35-
*/
36-
private $mediaAssetFactory;
37-
3831
/**
3932
* @var SearchResultFactory
4033
*/
@@ -48,60 +41,25 @@ class SearchAssets implements SearchAssetsInterface
4841
/**
4942
* @param SearchResultFactory $searchResultFactory
5043
* @param ResourceConnection $resourceConnection
51-
* @param AssetInterfaceFactory $mediaAssetFactory
5244
* @param LoggerInterface $logger
5345
*/
5446
public function __construct(
5547
SearchResultFactory $searchResultFactory,
5648
ResourceConnection $resourceConnection,
57-
AssetInterfaceFactory $mediaAssetFactory,
5849
LoggerInterface $logger
5950
) {
6051
$this->searchResultFactory = $searchResultFactory;
6152
$this->resourceConnection = $resourceConnection;
62-
$this->mediaAssetFactory = $mediaAssetFactory;
6353
$this->logger = $logger;
6454
}
6555

66-
/**
67-
* @inheritdoc
68-
*/
69-
public function execute(SearchCriteriaInterface $searchCriteria): array
70-
{
71-
$assets = [];
72-
try {
73-
foreach ($this->getAssetsData($searchCriteria)->getItems() as $assetData) {
74-
$assets[] = $this->mediaAssetFactory->create(
75-
[
76-
'id' => $assetData['id'],
77-
'path' => $assetData['path'],
78-
'title' => $assetData['title'],
79-
'description' => $assetData['description'],
80-
'source' => $assetData['source'],
81-
'hash' => $assetData['hash'],
82-
'contentType' => $assetData['content_type'],
83-
'width' => $assetData['width'],
84-
'height' => $assetData['height'],
85-
'size' => $assetData['size'],
86-
'createdAt' => $assetData['created_at'],
87-
'updatedAt' => $assetData['updated_at'],
88-
]
89-
);
90-
}
91-
} catch (\Exception $exception) {
92-
$this->logger->critical($exception);
93-
throw new LocalizedException(__('Could not retrieve media assets'), $exception->getMessage());
94-
}
95-
return $assets;
96-
}
97-
9856
/**
9957
* Retrieve assets data from database
10058
*
10159
* @param SearchCriteriaInterface $searchCriteria
10260
* @return SearchResultInterface
10361
*/
104-
private function getAssetsData(SearchCriteriaInterface $searchCriteria): SearchResultInterface
62+
public function execute(SearchCriteriaInterface $searchCriteria): SearchResultInterface
10563
{
10664
$searchResult = $this->searchResultFactory->create();
10765
$fields = [];
@@ -164,7 +122,7 @@ public function getResultCondition($field, $condition = null)
164122
$resultCondition = '(' . implode(') ' . Select::SQL_OR . ' (', $conditions) . ')';
165123
} else {
166124
$resultCondition = $resourceConnection->prepareSqlCondition(
167-
$resourceConnection->quoteIdentifier($value),
125+
$resourceConnection->quoteIdentifier($field),
168126
$condition
169127
);
170128
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
12+
use Psr\Log\LoggerInterface;
13+
use Magento\Framework\Api\SearchCriteriaInterface;
14+
use Magento\MediaGallery\Model\ResourceModel\GetAssetsBySearchCriteria;
15+
use Magento\MediaGalleryApi\Api\SearchAssetsInterface;
16+
17+
/**
18+
* Get media assets by searchCriteria
19+
*/
20+
class SearchAssets implements SearchAssetsInterface
21+
{
22+
/**
23+
* @var GetAssetsBySearchCriteria
24+
*/
25+
private $getAssetsBySearchCriteria;
26+
27+
/**
28+
* @var LoggerInterface
29+
*/
30+
private $logger;
31+
32+
/**
33+
* @param GetAssetsBySearchCriteria $getAssetsBySearchCriteria
34+
* @param AssetInterfaceFactory $mediaAssetFactory
35+
* @param LoggerInterface $logger
36+
*/
37+
public function __construct(
38+
GetAssetsBySearchCriteria $getAssetsBySearchCriteria,
39+
AssetInterfaceFactory $mediaAssetFactory,
40+
LoggerInterface $logger
41+
) {
42+
$this->getAssetsBySearchCriteria = $getAssetsBySearchCriteria;
43+
$this->mediaAssetFactory = $mediaAssetFactory;
44+
$this->logger = $logger;
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function execute(SearchCriteriaInterface $searchCriteria): array
51+
{
52+
$assets = [];
53+
try {
54+
foreach ($this->getAssetsBySearchCriteria->execute($searchCriteria)->getItems() as $assetData) {
55+
$assets[] = $this->mediaAssetFactory->create(
56+
[
57+
'id' => $assetData['id'],
58+
'path' => $assetData['path'],
59+
'title' => $assetData['title'],
60+
'description' => $assetData['description'],
61+
'source' => $assetData['source'],
62+
'hash' => $assetData['hash'],
63+
'contentType' => $assetData['content_type'],
64+
'width' => $assetData['width'],
65+
'height' => $assetData['height'],
66+
'size' => $assetData['size'],
67+
'createdAt' => $assetData['created_at'],
68+
'updatedAt' => $assetData['updated_at'],
69+
]
70+
);
71+
}
72+
} catch (\Exception $exception) {
73+
$this->logger->critical($exception);
74+
throw new LocalizedException(__('Could not retrieve media assets'), $exception->getMessage());
75+
}
76+
return $assets;
77+
}
78+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<preference for="Magento\MediaGalleryApi\Api\SaveAssetsInterface" type="Magento\MediaGallery\Model\ResourceModel\SaveAssets"/>
3030
<preference for="Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface" type="Magento\MediaGallery\Model\ResourceModel\Keyword\GetAssetsKeywords"/>
3131
<preference for="Magento\MediaGalleryApi\Api\SaveAssetsKeywordsInterface" type="Magento\MediaGallery\Model\ResourceModel\Keyword\SaveAssetsKeywords"/>
32-
<preference for="Magento\MediaGalleryApi\Api\SearchAssetsInterface" type="Magento\MediaGallery\Model\ResourceModel\SearchAssets"/>
32+
<preference for="Magento\MediaGalleryApi\Api\SearchAssetsInterface" type="Magento\MediaGallery\Model\SearchAssets"/>
3333

3434
<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
3535
<plugin name="media_gallery_image_remove_metadata_after_wysiwyg" type="Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage"

app/code/Magento/MediaGalleryApi/Api/SearchAssetsInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
use Magento\Framework\Api\Search\SearchResultInterface;
1111
use Magento\Framework\Api\SearchCriteriaInterface;
1212
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1413

1514
/**
1615
* Search media gallery assets by search criteria
17-
* @api
1816
*/
1917
interface SearchAssetsInterface
2018
{
2119
/**
2220
* Search media gallery assets
2321
*
2422
* @param SearchCriteriaInterface $searchCriteria
25-
* @return AssetInterface[]
23+
* @return AssetsSearchResultInterface[]
2624
* @throws LocalizedException
2725
*/
2826
public function execute(SearchCriteriaInterface $searchCriteria): array;

0 commit comments

Comments
 (0)