-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabriel Galvao da Gama
committed
Jul 14, 2020
1 parent
500acc3
commit acf425b
Showing
5 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/code/Magento/MediaContentCms/Model/ResourceModel/GetAssetIdsByBlockStore.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaContentCms\Model\ResourceModel; | ||
|
||
use Magento\Cms\Api\BlockRepositoryInterface; | ||
use Magento\Cms\Api\Data\BlockInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface; | ||
|
||
/** | ||
* Class responsible to return Asset id by content field | ||
*/ | ||
class GetAssetIdsByBlockStore implements GetAssetIdsByContentFieldInterface | ||
{ | ||
private const TABLE_CONTENT_ASSET = 'media_content_asset'; | ||
private const ENTITY_TYPE = 'cms_block'; | ||
private const STORE_FIELD = 'store_id'; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var BlockRepositoryInterface | ||
*/ | ||
private $blockRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* GetAssetIdsByContentField constructor. | ||
* | ||
* @param ResourceConnection $resource | ||
* @param BlockRepositoryInterface $blockRepository | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
BlockRepositoryInterface $blockRepository, | ||
SearchCriteriaBuilder $searchCriteriaBuilder | ||
) { | ||
$this->connection = $resource; | ||
$this->blockRepository = $blockRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(string $value): array | ||
{ | ||
$sql = $this->connection->getConnection()->select()->from( | ||
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)], | ||
['asset_id'] | ||
)->where( | ||
'entity_type = ?', | ||
self::ENTITY_TYPE | ||
)->where( | ||
'entity_id IN (?)', | ||
$this->getBlockIdsByStore((int) $value) | ||
); | ||
|
||
return $this->connection->getConnection()->fetchCol($sql); | ||
} | ||
|
||
/** | ||
* @param int $storeId | ||
* @return array | ||
* @throws LocalizedException | ||
*/ | ||
private function getBlockIdsByStore(int $storeId): array | ||
{ | ||
$searchCriteria = $this->searchCriteriaBuilder | ||
->addFilter(self::STORE_FIELD, $storeId) | ||
->create(); | ||
|
||
$searchResult = $this->blockRepository->getList($searchCriteria); | ||
|
||
return array_map(function (BlockInterface $block) { | ||
return $block->getId(); | ||
}, $searchResult->getItems()); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
app/code/Magento/MediaContentCms/Model/ResourceModel/GetAssetIdsByPageStore.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaContentCms\Model\ResourceModel; | ||
|
||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Framework\Api\FilterBuilder; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface; | ||
|
||
/** | ||
* Class responsible to return Asset id by content field | ||
*/ | ||
class GetAssetIdsByPageStore implements GetAssetIdsByContentFieldInterface | ||
{ | ||
private const TABLE_CONTENT_ASSET = 'media_content_asset'; | ||
private const ENTITY_TYPE = 'cms_page'; | ||
private const STORE_FIELD = 'store_id'; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var PageRepositoryInterface | ||
*/ | ||
private $pageRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* GetAssetIdsByContentField constructor. | ||
* | ||
* @param ResourceConnection $resource | ||
* @param PageRepositoryInterface $pageRepository | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
PageRepositoryInterface $pageRepository, | ||
SearchCriteriaBuilder $searchCriteriaBuilder | ||
) { | ||
$this->connection = $resource; | ||
$this->pageRepository = $pageRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(string $value): array | ||
{ | ||
$sql = $this->connection->getConnection()->select()->from( | ||
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)], | ||
['asset_id'] | ||
)->where( | ||
'entity_type = ?', | ||
self::ENTITY_TYPE | ||
)->where( | ||
'entity_id IN (?)', | ||
$this->getPageIdsByStore((int) $value) | ||
); | ||
|
||
return $this->connection->getConnection()->fetchCol($sql); | ||
} | ||
|
||
/** | ||
* @param int $storeId | ||
* @return array | ||
* @throws LocalizedException | ||
*/ | ||
private function getPageIdsByStore(int $storeId): array | ||
{ | ||
$searchCriteria = $this->searchCriteriaBuilder | ||
->addFilter(self::STORE_FIELD, $storeId) | ||
->create(); | ||
|
||
$searchResult = $this->pageRepository->getList($searchCriteria); | ||
|
||
return array_map(function (PageInterface $page) { | ||
return $page->getId(); | ||
}, $searchResult->getItems()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters