Skip to content

Commit

Permalink
First Version
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasKotlyar committed Jan 23, 2016
1 parent d96ea0c commit da361b4
Show file tree
Hide file tree
Showing 6 changed files with 909 additions and 0 deletions.
140 changes: 140 additions & 0 deletions Console/Command/AbstractImportCommand.php
@@ -0,0 +1,140 @@
<?php
/**
* Copyright © 2016 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\FastSimpleImport2\Console\Command;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Framework\App\ObjectManager\ConfigLoader;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\App\State;
use Magento\ImportExport\Model\Import;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class TestCommand
* @package FireGento\FastSimpleImport2\Console\Command
*
*/
abstract class AbstractImportCommand extends Command
{

/**
* @var string
*/
protected $behavior;
/**
* @var string
*/
protected $entityCode;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* Object manager factory
*
* @var ObjectManagerFactory
*/
private $objectManagerFactory;

/**
* Constructor
*
* @param ObjectManagerFactory $objectManagerFactory
*/
public function __construct(ObjectManagerFactory $objectManagerFactory)
{
$this->objectManagerFactory = $objectManagerFactory;
parent::__construct();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Magento\Framework\Exception\LocalizedException
* @return null|int null or 0 if everything went fine, or an error code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$omParams = $_SERVER;
$omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
$omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
$this->objectManager = $this->objectManagerFactory->create($omParams);

$area = FrontNameResolver::AREA_CODE;

/** @var \Magento\Framework\App\State $appState */
$appState = $this->objectManager->get('Magento\Framework\App\State');
$appState->setAreaCode($area);
$configLoader = $this->objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface');
$this->objectManager->configure($configLoader->load($area));

$output->writeln('Import started');

$time = microtime(true);

/** @var \FireGento\FastSimpleImport2\Model\Importer $importerModel */
$importerModel = $this->objectManager->create('FireGento\FastSimpleImport2\Model\Importer');

$productsArray = $this->getEntities();

$importerModel->setBehavior($this->getBehavior());
$importerModel->setEntityCode($this->getEntityCode());

try {
$importerModel->processImport($productsArray);
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}

$output->write($importerModel->getLogTrace());
$output->write($importerModel->getErrorMessages());

$output->writeln('Import finished. Elapsed time: ' . round(microtime(true) - $time, 2) . 's' . "\n");
}

/**
* @return array
*/
abstract protected function getEntities();

/**
* @return string
*/
public function getBehavior()
{
return $this->behavior;
}

/**
* @param string $behavior
*/
public function setBehavior($behavior)
{
$this->behavior = $behavior;
}

/**
* @return string
*/
public function getEntityCode()
{
return $this->entityCode;
}

/**
* @param string $entityCode
*/
public function setEntityCode($entityCode)
{
$this->entityCode = $entityCode;
}

}
57 changes: 57 additions & 0 deletions Console/Command/ImportSimpleProducts.php
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © 2016 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\FastSimpleImport2\Console\Command;

use Magento\ImportExport\Model\Import;
/**
* Class TestCommand
* @package FireGento\FastSimpleImport2\Console\Command
*
*/
class ImportSimpleProducts extends AbstractImportCommand
{






protected function configure()
{
$this->setName('firegento:fastsimpleimport2demo:importsimpleproducts')
->setDescription('Import Simple Products ');

$this->setBehavior(Import::BEHAVIOR_APPEND);
$this->setEntityCode('catalog_product');

parent::configure();
}

/**
* @return array
*/
protected function getEntities()
{
$data = [];
for ($i = 1; $i <= 10; $i++) {
$data[] = array(
'sku' => 'FIREGENTO-' . $i,
'attribute_set_code' => 'Default',
'product_type' => 'simple',
'product_websites' => 'base',
'name' => 'FireGento Test Product ' . $i,
'price' => '14.0000',
//'visibility' => 'Catalog, Search',
//'tax_class_name' => 'Taxable Goods',
//'product_online' => '1',
//'weight' => '1.0000',
//'short_description' => NULL,
//'description' => '',
);
}
return $data;
}
}

0 comments on commit da361b4

Please sign in to comment.