mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 09:17:35 +00:00
86 lines
2.3 KiB
PHP
86 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
namespace Magento\AsynchronousOperations\Model\ResourceModel\Operation;
|
|
|
|
use Magento\Framework\EntityManager\MetadataPool;
|
|
use Magento\Framework\EntityManager\TypeResolver;
|
|
use Magento\Framework\App\ResourceConnection;
|
|
use Magento\Framework\Phrase;
|
|
|
|
/**
|
|
* Create operation for list of bulk operations.
|
|
*/
|
|
class Create implements \Magento\Framework\EntityManager\Operation\CreateInterface
|
|
{
|
|
/**
|
|
* @var MetadataPool
|
|
*/
|
|
private $metadataPool;
|
|
|
|
/**
|
|
* @var TypeResolver
|
|
*/
|
|
private $typeResolver;
|
|
|
|
/**
|
|
* @var ResourceConnection
|
|
*/
|
|
private $resourceConnection;
|
|
|
|
/**
|
|
* @param MetadataPool $metadataPool
|
|
* @param TypeResolver $typeResolver
|
|
* @param ResourceConnection $resourceConnection
|
|
*/
|
|
public function __construct(
|
|
MetadataPool $metadataPool,
|
|
TypeResolver $typeResolver,
|
|
ResourceConnection $resourceConnection
|
|
) {
|
|
$this->metadataPool = $metadataPool;
|
|
$this->typeResolver = $typeResolver;
|
|
$this->resourceConnection = $resourceConnection;
|
|
}
|
|
|
|
/**
|
|
* Save all operations from the list in one query.
|
|
*
|
|
* @param object $entity
|
|
* @param array $arguments
|
|
* @return object
|
|
* @throws \Exception
|
|
*
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
*/
|
|
public function execute($entity, $arguments = [])
|
|
{
|
|
$entityType = $this->typeResolver->resolve($entity);
|
|
$metadata = $this->metadataPool->getMetadata($entityType);
|
|
$connection = $this->resourceConnection->getConnection($metadata->getEntityConnectionName());
|
|
try {
|
|
$connection->beginTransaction();
|
|
$data = [];
|
|
foreach ($entity->getItems() as $operation) {
|
|
$data[] = $operation->getData();
|
|
}
|
|
$connection->insertOnDuplicate(
|
|
$metadata->getEntityTable(),
|
|
$data,
|
|
[
|
|
'status',
|
|
'error_code',
|
|
'result_message',
|
|
]
|
|
);
|
|
$connection->commit();
|
|
} catch (\Exception $e) {
|
|
$connection->rollBack();
|
|
throw $e;
|
|
}
|
|
return $entity;
|
|
}
|
|
}
|