Skip to content

Introduce SearchAssets service #29071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model\ResourceModel;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ResourceConnection;
use Psr\Log\LoggerInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\Search\SearchResultFactory;
use Magento\Framework\DB\Select;
use Magento\MediaGalleryApi\Api\Data\AssetInterface;

/**
* Get assets data by searchCriteria
*/
class GetAssetsBySearchCriteria
{
private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset';

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var SearchResultFactory
*/
private $searchResultFactory;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param SearchResultFactory $searchResultFactory
* @param ResourceConnection $resourceConnection
* @param LoggerInterface $logger
*/
public function __construct(
SearchResultFactory $searchResultFactory,
ResourceConnection $resourceConnection,
LoggerInterface $logger
) {
$this->searchResultFactory = $searchResultFactory;
$this->resourceConnection = $resourceConnection;
$this->logger = $logger;
}

/**
* Retrieve assets data from database
*
* @param SearchCriteriaInterface $searchCriteria
* @return SearchResultInterface
*/
public function execute(SearchCriteriaInterface $searchCriteria): SearchResultInterface
{
$searchResult = $this->searchResultFactory->create();
$fields = [];
$conditions = [];

foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
foreach ($filterGroup->getFilters() as $filter) {
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$fields[] = $filter->getField();

if ($condition === 'fulltext') {
$condition = 'like';
$filter->setValue('%' . $filter->getValue() . '%');
}

$conditions[] = [$condition => $filter->getValue()];
}
}

if ($fields) {
$resultCondition = $this->getResultCondition($fields, $conditions);
$select = $this->resourceConnection->getConnection()->select()
->from(
$this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET)
)
->where($resultCondition, null, Select::TYPE_CONDITION);

if ($searchCriteria->getPageSize() || $searchCriteria->getCurrentPage()) {
$select->limit(
$searchCriteria->getPageSize(),
$searchCriteria->getCurrentPage() * $searchCriteria->getPageSize()
);
}

$data = $this->resourceConnection->getConnection()->fetchAll($select);
}

$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setItems($data);

return $searchResult;
}

/**
* Get conditions data by searchCriteria
*
* @param string|array $field
* @param null|string|array $condition
*/
public function getResultCondition($field, $condition = null)
{
$resourceConnection = $this->resourceConnection->getConnection();
if (is_array($field)) {
$conditions = [];
foreach ($field as $key => $value) {
$conditions[] = $resourceConnection->prepareSqlCondition(
$resourceConnection->quoteIdentifier($value),
isset($condition[$key]) ? $condition[$key] : null
);
}

$resultCondition = '(' . implode(') ' . Select::SQL_OR . ' (', $conditions) . ')';
} else {
$resultCondition = $resourceConnection->prepareSqlCondition(
$resourceConnection->quoteIdentifier($field),
$condition
);
}
return $resultCondition;
}
}
83 changes: 83 additions & 0 deletions app/code/Magento/MediaGallery/Model/SearchAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Psr\Log\LoggerInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\MediaGallery\Model\ResourceModel\GetAssetsBySearchCriteria;
use Magento\MediaGalleryApi\Api\SearchAssetsInterface;

/**
* Get media assets by searchCriteria
*/
class SearchAssets implements SearchAssetsInterface
{
/**
* @var GetAssetsBySearchCriteria
*/
private $getAssetsBySearchCriteria;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var AssetInterfaceFactory
*/
private $mediaAssetFactory;

/**
* @param GetAssetsBySearchCriteria $getAssetsBySearchCriteria
* @param AssetInterfaceFactory $mediaAssetFactory
* @param LoggerInterface $logger
*/
public function __construct(
GetAssetsBySearchCriteria $getAssetsBySearchCriteria,
AssetInterfaceFactory $mediaAssetFactory,
LoggerInterface $logger
) {
$this->getAssetsBySearchCriteria = $getAssetsBySearchCriteria;
$this->mediaAssetFactory = $mediaAssetFactory;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute(SearchCriteriaInterface $searchCriteria): array
{
$assets = [];
try {
foreach ($this->getAssetsBySearchCriteria->execute($searchCriteria)->getItems() as $assetData) {
$assets[] = $this->mediaAssetFactory->create(
[
'id' => $assetData['id'],
'path' => $assetData['path'],
'title' => $assetData['title'],
'description' => $assetData['description'],
'source' => $assetData['source'],
'hash' => $assetData['hash'],
'contentType' => $assetData['content_type'],
'width' => $assetData['width'],
'height' => $assetData['height'],
'size' => $assetData['size'],
'createdAt' => $assetData['created_at'],
'updatedAt' => $assetData['updated_at'],
]
);
}
} catch (\Exception $exception) {
$this->logger->critical($exception);
throw new LocalizedException(__('Could not retrieve media assets'), $exception->getMessage());
}
return $assets;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/MediaGallery/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<preference for="Magento\MediaGalleryApi\Api\SaveAssetsInterface" type="Magento\MediaGallery\Model\ResourceModel\SaveAssets"/>
<preference for="Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface" type="Magento\MediaGallery\Model\ResourceModel\Keyword\GetAssetsKeywords"/>
<preference for="Magento\MediaGalleryApi\Api\SaveAssetsKeywordsInterface" type="Magento\MediaGallery\Model\ResourceModel\Keyword\SaveAssetsKeywords"/>
<preference for="Magento\MediaGalleryApi\Api\SearchAssetsInterface" type="Magento\MediaGallery\Model\SearchAssets"/>

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
<plugin name="media_gallery_image_remove_metadata_after_wysiwyg" type="Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage"
Expand Down
27 changes: 27 additions & 0 deletions app/code/Magento/MediaGalleryApi/Api/SearchAssetsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryApi\Api;

use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* Search media gallery assets by search criteria
*/
interface SearchAssetsInterface
{
/**
* Search media gallery assets
*
* @param SearchCriteriaInterface $searchCriteria
* @return AssetsSearchResultInterface[]
* @throws LocalizedException
*/
public function execute(SearchCriteriaInterface $searchCriteria): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\MediaGalleryApi\Api\SearchAssetsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Verify SearchAssets By searchCriteria
*/
class SearchAssetsTest extends TestCase
{
private const FIXTURE_ASSET_PATH = 'testDirectory/path.jpg';

/**
* @var SearchAssetsInterfcae
*/
private $searchAssets;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;

/**
* @var FilterBuilder
*/
private $filterBuilder;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->filterBuilder = Bootstrap::getObjectManager()->get(FilterBuilder::class);
$this->filterGroupBuilder = Bootstrap::getObjectManager()->get(FilterGroupBuilder::class);
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
$this->searchAssets = Bootstrap::getObjectManager()->get(SearchAssetsInterface::class);
}

/**
* Verify search asstes by searching with search criteria
*
* @dataProvider searchCriteriaProvider
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
*/
public function testExecute(array $searchCriteriaData): void
{
$titleFilter = $this->filterBuilder->setField($searchCriteriaData['field'])
->setConditionType($searchCriteriaData['conditionType'])
->setValue($searchCriteriaData['value'])
->create();
$searchCriteria = $this->searchCriteriaBuilder
->setFilterGroups([$this->filterGroupBuilder->setFilters([$titleFilter])->create()])
->create();

$assets = $this->searchAssets->execute($searchCriteria);

$this->assertCount(1, $assets);
$this->assertEquals($assets[0]->getPath(), self::FIXTURE_ASSET_PATH);
}

/**
* Search criteria params provider
*
* @return array
*/
public function searchCriteriaProvider(): array
{
return [
[
['field' => 'id', 'conditionType' => 'eq', 'value' => 2020],
],
[
['field' => 'title', 'conditionType' => 'fulltext', 'value' => 'Img'],
],
[
['field' => 'content_type', 'conditionType' => 'eq', 'value' => 'image']
],
[
['field' => 'description', 'conditionType' => 'fulltext', 'value' => 'description']
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[
'id' => 2020,
'path' => 'testDirectory/path.jpg',
'description' => 'Description of an image',
'contentType' => 'image',
'title' => 'Img',
'source' => 'Local',
Expand Down