Your IP : 127.0.0.1
<?php
/**
* @copyright: Copyright © 2019 Firebear Studio. All rights reserved.
* @author : Firebear Studio <fbeardev@gmail.com>
*/
namespace Firebear\ImportExport\Model\ResourceModel\Import;
use Firebear\ImportExport\Api\Data\ImportHistoryInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
/**
* Class History
*
* @package Firebear\ImportExport\Model\ResourceModel\Import
*/
class History extends AbstractDb
{
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var MetadataPool
*/
protected $metadataPool;
/**
* ExportJob constructor.
*
* @param Context $context
* @param EntityManager $entityManager
* @param MetadataPool $metadataPool
* @param null $connectionName
*/
public function __construct(
Context $context,
EntityManager $entityManager,
MetadataPool $metadataPool,
$connectionName = null
) {
$this->entityManager = $entityManager;
$this->metadataPool = $metadataPool;
parent::__construct($context, $connectionName);
}
/**
* Resource initialization
*
* @return void
*/
protected function _construct()
{
$this->_init('firebear_import_history', 'history_id');
}
/**
* @inheritDoc
*/
public function getConnection()
{
return $this->metadataPool->getMetadata(ImportHistoryInterface::class)->getEntityConnection();
}
/**
* @param AbstractModel $object
* @param $value
* @param null $field
*
* @return bool
*/
private function getHistoryId(AbstractModel $object, $value, $field = null)
{
$entityMetadata = $this->metadataPool->getMetadata(ImportHistoryInterface::class);
if (!$field) {
$field = $entityMetadata->getIdentifierField();
}
$entityId = $value;
if ($field != $entityMetadata->getIdentifierField() || $object->getStoreId()) {
$select = $this->_getLoadSelect($field, $value, $object);
$select->reset(Select::COLUMNS)
->columns($this->getMainTable() . '.' . $entityMetadata->getIdentifierField())
->limit(1);
$result = $this->getConnection()->fetchCol($select);
$entityId = count($result) ? $result[0] : false;
}
return $entityId;
}
/**
* @param AbstractModel $object
* @param mixed $value
* @param null $field
*
* @return mixed
* @throws NoSuchEntityException
*/
public function load(AbstractModel $object, $value, $field = null)
{
$historyId = $this->getHistoryId($object, $value, $field);
if ($historyId) {
$this->entityManager->load($object, $historyId);
}
return $this;
}
/**
* @inheritDoc
*/
public function delete(AbstractModel $object)
{
$this->entityManager->delete($object);
return $this;
}
}