|
| 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\ResourceModel; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; |
| 12 | +use Magento\MediaGalleryApi\Api\SearchAssetsInterface; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Magento\Framework\Api\Search\SearchResultInterface; |
| 16 | +use Magento\Framework\Api\SearchCriteriaInterface; |
| 17 | +use Magento\Framework\Api\Search\SearchResultFactory; |
| 18 | +use Magento\Framework\DB\Select; |
| 19 | +use Magento\MediaGalleryApi\Api\Data\AssetInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Get media assets by searchCriteria |
| 23 | + */ |
| 24 | +class SearchAssets implements SearchAssetsInterface |
| 25 | +{ |
| 26 | + private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ResourceConnection |
| 30 | + */ |
| 31 | + private $resourceConnection; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var AssetInterfaceFactory |
| 35 | + */ |
| 36 | + private $mediaAssetFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var SearchResultFactory |
| 40 | + */ |
| 41 | + protected $searchResultFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var LoggerInterface |
| 45 | + */ |
| 46 | + private $logger; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param SearchResultFactory $searchResultFactory |
| 50 | + * @param ResourceConnection $resourceConnection |
| 51 | + * @param AssetInterfaceFactory $mediaAssetFactory |
| 52 | + * @param LoggerInterface $logger |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + SearchResultFactory $searchResultFactory, |
| 56 | + ResourceConnection $resourceConnection, |
| 57 | + AssetInterfaceFactory $mediaAssetFactory, |
| 58 | + LoggerInterface $logger |
| 59 | + ) { |
| 60 | + $this->searchResultFactory = $searchResultFactory; |
| 61 | + $this->resourceConnection = $resourceConnection; |
| 62 | + $this->mediaAssetFactory = $mediaAssetFactory; |
| 63 | + $this->logger = $logger; |
| 64 | + } |
| 65 | + |
| 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 | + |
| 98 | + /** |
| 99 | + * Retrieve assets data from database |
| 100 | + * |
| 101 | + * @param SearchCriteriaInterface $searchCriteria |
| 102 | + * @return SearchResultInterface |
| 103 | + */ |
| 104 | + private function getAssetsData(SearchCriteriaInterface $searchCriteria): SearchResultInterface |
| 105 | + { |
| 106 | + $searchResult = $this->searchResultFactory->create(); |
| 107 | + $fields = []; |
| 108 | + $conditions = []; |
| 109 | + |
| 110 | + foreach ($searchCriteria->getFilterGroups() as $filterGroup) { |
| 111 | + foreach ($filterGroup->getFilters() as $filter) { |
| 112 | + $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; |
| 113 | + $fields[] = $filter->getField(); |
| 114 | + |
| 115 | + if ($condition === 'fulltext') { |
| 116 | + $condition = 'like'; |
| 117 | + $filter->setValue('%' . $filter->getValue() . '%'); |
| 118 | + } |
| 119 | + |
| 120 | + $conditions[] = [$condition => $filter->getValue()]; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if ($fields) { |
| 125 | + $resultCondition = $this->getResultCondition($fields, $conditions); |
| 126 | + $select = $this->resourceConnection->getConnection()->select() |
| 127 | + ->from( |
| 128 | + $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET) |
| 129 | + ) |
| 130 | + ->where($resultCondition, null, Select::TYPE_CONDITION); |
| 131 | + |
| 132 | + if ($searchCriteria->getPageSize() || $searchCriteria->getCurrentPage()) { |
| 133 | + $select->limit($searchCriteria->getPageSize(), $searchCriteria->getCurrentPage()); |
| 134 | + } |
| 135 | + |
| 136 | + $data = $this->resourceConnection->getConnection()->fetchAll($select); |
| 137 | + } |
| 138 | + |
| 139 | + $searchResult->setSearchCriteria($searchCriteria); |
| 140 | + $searchResult->setItems($data); |
| 141 | + |
| 142 | + |
| 143 | + return $searchResult; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get conditions data by searchCriteria |
| 148 | + * |
| 149 | + * @param string|array $field |
| 150 | + * @param null|string|array $condition |
| 151 | + */ |
| 152 | + public function getResultCondition($field, $condition = null) |
| 153 | + { |
| 154 | + $resourceConnection = $this->resourceConnection->getConnection(); |
| 155 | + if (is_array($field)) { |
| 156 | + $conditions = []; |
| 157 | + foreach ($field as $key => $value) { |
| 158 | + $conditions[] = $resourceConnection->prepareSqlCondition( |
| 159 | + $resourceConnection->quoteIdentifier($value), |
| 160 | + isset($condition[$key]) ? $condition[$key] : null |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + $resultCondition = '(' . implode(') ' . Select::SQL_OR . ' (', $conditions) . ')'; |
| 165 | + } else { |
| 166 | + $resultCondition = $resourceConnection->prepareSqlCondition( |
| 167 | + $resourceConnection->quoteIdentifier($value), |
| 168 | + $condition |
| 169 | + ); |
| 170 | + } |
| 171 | + return $resultCondition; |
| 172 | + } |
| 173 | +} |
0 commit comments