Skip to content

Commit

Permalink
magento/adobe-stock-integration#1724: Support batches processing for …
Browse files Browse the repository at this point in the history
…synchronization queue messages - added method to sync and save media content to database
  • Loading branch information
yolouiese committed Aug 21, 2020
1 parent d98fcf3 commit 16dc1fa
Showing 1 changed file with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\MediaContentSynchronization\Model;

use Magento\Framework\App\ResourceConnection;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
Expand All @@ -17,12 +18,24 @@ class SynchronizeIdentities implements SynchronizeIdentitiesInterface
private const ENTITY_TYPE = 'entityType';
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

private const MEDIA_CONTENT_TYPE = 'entity_type';
private const MEDIA_CONTENT_ENTITY_ID = 'entity_id';
private const MEDIA_CONTENT_FIELD = 'field';

private const FIELD_CMS_PAGE = 'cms_page';
private const FIELD_CMS_BLOCK = 'cms_block';

private const ID_CMS_PAGE = 'page_id';
private const ID_CMS_BLOCK = 'block_id';

private const COLUMN_CMS_CONTENT = 'content';

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

/**
* @var ContentIdentityInterfaceFactory
*/
Expand All @@ -39,26 +52,21 @@ class SynchronizeIdentities implements SynchronizeIdentitiesInterface
private $getEntityContents;

/**
* @var array
*/
private $fields;

/**
* @param ResourceConnection $resourceConnection
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
* @param GetEntityContentsInterface $getEntityContents
* @param array $fields
*/
public function __construct(
ResourceConnection $resourceConnection,
ContentIdentityInterfaceFactory $contentIdentityFactory,
UpdateContentAssetLinksInterface $updateContentAssetLinks,
GetEntityContentsInterface $getEntityContents,
array $fields = []
GetEntityContentsInterface $getEntityContents
) {
$this->resourceConnection = $resourceConnection;
$this->contentIdentityFactory = $contentIdentityFactory;
$this->updateContentAssetLinks = $updateContentAssetLinks;
$this->getEntityContents = $getEntityContents;
$this->fields = $fields;
}

/**
Expand All @@ -78,7 +86,7 @@ public function execute(array $mediaContentIdentities): void
if ($identity[self::MEDIA_CONTENT_TYPE] === self::FIELD_CMS_PAGE
|| $identity[self::MEDIA_CONTENT_TYPE] === self::FIELD_CMS_BLOCK
) {
$content = (string) $identity[self::MEDIA_CONTENT_FIELD];
$content = $this->getCmsMediaContent($identity[self::MEDIA_CONTENT_TYPE], $identity[self::MEDIA_CONTENT_ENTITY_ID]);
} else {
$content = implode(PHP_EOL, $this->getEntityContents->execute($contentIdentity));
}
Expand All @@ -89,4 +97,25 @@ public function execute(array $mediaContentIdentities): void
);
}
}

/**
* Get cms media content from database
*
* @param string $tableName
* @param string $cmsId
* @return string
*/
private function getCmsMediaContent(string $tableName, string $cmsId): string
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName($tableName);
$idField = $tableName == self::FIELD_CMS_BLOCK ? $idField = self::ID_CMS_BLOCK : self::ID_CMS_PAGE;

$select = $connection->select()
->from($tableName, self::COLUMN_CMS_CONTENT)
->where($idField . '= ?', $cmsId);
$data = $connection->fetchOne($select);

return (string)$data;
}
}

0 comments on commit 16dc1fa

Please sign in to comment.