Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: import and export mappings #75

Open
wants to merge 2 commits into
base: develop-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Divante\MagentoIntegrationBundle\Action\Mapper;

use Divante\MagentoIntegrationBundle\Application\Mapper\MapperExporter;
use Divante\MagentoIntegrationBundle\Responder\JsonFileResponder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* Class ExportAction
* @package Divante\MagentoIntegrationBundle\Action\Mapper
* @Route("/mappings/export/{type}/{id}")
*/
class ExportAction
{
/** @var MapperExporter */
private $domain;

/** @var JsonFileResponder */
private $responder;

/**
* SendCategoriesAction constructor.
* @param MapperExporter $domain
* @param JsonFileResponder $jsonFileResponder
*/
public function __construct(MapperExporter $domain, JsonFileResponder $jsonFileResponder)
{
$this->domain = $domain;
$this->responder = $jsonFileResponder;
}

/**
* @param Request $query
* @return JsonResponse
* @throws \Exception
*/
public function __invoke(Request $query): Response
{
return $this->responder->createResponse(
sprintf("export_%s_mapping", $query->get('type')),
$this->domain->getExportMappingData($query->get("id"), $query->get('type'))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Divante\MagentoIntegrationBundle\Action\Mapper;

use Divante\MagentoIntegrationBundle\Application\Mapper\MapperImporter;
use Divante\MagentoIntegrationBundle\Domain\Mapper\Exception\MappingImportException;
use Divante\MagentoIntegrationBundle\Responder\JsonResponder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
* Class ImportAction
* @package Divante\MagentoIntegrationBundle\Action\Mapper
* @Route("/mappings/import/{type}/{id}", methods={"POST"})
*/
class ImportAction
{
/** @var MapperImporter */
private $domain;

/** @var JsonResponder */
private $responder;

/**
* SendCategoriesAction constructor.
* @param MapperImporter $domain
* @param JsonResponder $jsonResponder
*/
public function __construct(MapperImporter $domain, JsonResponder $jsonResponder)
{
$this->domain = $domain;
$this->responder = $jsonResponder;
}

/**
* @param Request $query
* @return JsonResponse
*/
public function __invoke(Request $query): JsonResponse
{
try {
$this->domain->importMappingData(
$query->get('id'),
$query->get('type'),
$query->files->get('file')
);
return $this->responder->createResponse(
[
"success" => true,
]
);
} catch (MappingImportException $exception) {
return $this->responder->createResponse(
[
"success" => false,
"message" => $exception->getMessage()
]
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Divante\MagentoIntegrationBundle\Application\Mapper;

use Divante\MagentoIntegrationBundle\Domain\Common\ObjectTypeHelper;
use Divante\MagentoIntegrationBundle\Domain\Mapper\Exception\MappingExportException;
use Divante\MagentoIntegrationBundle\Domain\Mapper\ImporterExporterHelper;
use Pimcore\Model\DataObject\IntegrationConfiguration;

/**
* Class MapperExporter
* @package Divante\MagentoIntegrationBundle\Application\Mapper
*/
class MapperExporter
{
/**
* @var ImporterExporterHelper
*/
protected $exporterHelper;

/**
* MapperExporter constructor.
* @param ImporterExporterHelper $exporterHelper
*/
public function __construct(ImporterExporterHelper $exporterHelper)
{
$this->exporterHelper = $exporterHelper;
}

/**
* @param int $idConfig
* @param string $type
* @return array
* @throws MappingExportException
*/
public function getExportMappingData(int $idConfig, string $type): array
{
$config = IntegrationConfiguration::getById($idConfig);
if (!$config instanceof IntegrationConfiguration) {
throw new MappingExportException("Configuration not found!");
}

switch ($type) {
case ObjectTypeHelper::PRODUCT:
$mapping = $config->getProductMapping();
break;
case ObjectTypeHelper::CATEGORY:
$mapping = $config->getCategoryMapping();
break;
default:
throw new MappingExportException("Invalid type provided: " . $type);
}

return $this->exporterHelper->buildFileMapping($mapping, $type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php


namespace Divante\MagentoIntegrationBundle\Application\Mapper;

use Divante\MagentoIntegrationBundle\Domain\Common\ObjectTypeHelper;
use Divante\MagentoIntegrationBundle\Domain\Mapper\Exception\MappingImportException;
use Divante\MagentoIntegrationBundle\Domain\Mapper\ImporterExporterHelper;
use Pimcore\Model\DataObject\IntegrationConfiguration;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Class MapperImporter
* @package Divante\MagentoIntegrationBundle\Application\Mapper
*/
class MapperImporter
{
/**
* @var ImporterExporterHelper
*/
protected $importerHelper;

/**
* MapperImporter constructor.
* @param ImporterExporterHelper $importerHelper
*/
public function __construct(ImporterExporterHelper $importerHelper)
{
$this->importerHelper = $importerHelper;
}

/**
* @param string $idConfig
* @param string $type
* @param UploadedFile $file
* @return void
* @throws MappingImportException
*/
public function importMappingData(string $idConfig, string $type, UploadedFile $file): void
{
$config = IntegrationConfiguration::getById($idConfig);
if (!$config instanceof IntegrationConfiguration) {
throw new MappingImportException("Configuration not found!");
}

$mapping = $this->importerHelper->extractFileMapping($file);
if ($type !== $mapping->getType()) {
throw new MappingImportException(
sprintf(
"Wrong file, type is set to '%s' and should be '%s'",
$mapping->getType(),
$type
)
);
}

switch ($mapping->getType()) {
case ObjectTypeHelper::PRODUCT:
$config->setProductMapping($mapping->getData());
break;
case ObjectTypeHelper::CATEGORY:
$config->setCategoryMapping($mapping->getData());
break;
default:
throw new MappingImportException("Invalid type provided: " . $mapping->getType());
}

try {
$config->setOmitMandatoryCheck(true)->save();
} catch (\Exception $exception) {
throw new MappingImportException($exception->getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Divante\MagentoIntegrationBundle\Application\Product;

use Divante\MagentoIntegrationBundle\Application\Common\AbstractMappedObjectService;
use Divante\MagentoIntegrationBundle\Domain\Event\IntegratedObjectEvent;
use Divante\MagentoIntegrationBundle\Domain\Event\PostMappingObjectEvent;
use Divante\MagentoIntegrationBundle\Domain\Common\Event\IntegratedObjectEvent;
use Divante\MagentoIntegrationBundle\Domain\Common\Event\PostMappingObjectEvent;
use Divante\MagentoIntegrationBundle\Domain\IntegrationConfiguration\IntegrationHelper;
use Divante\MagentoIntegrationBundle\Domain\Mapper\MapperEventTypes;
use Divante\MagentoIntegrationBundle\Domain\DataObject\IntegrationConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Divante\MagentoIntegrationBundle\Command;

use Divante\MagentoIntegrationBundle\Application\Mapper\MapperImporter;
use Pimcore\Console\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Class ImportMappingCommand
* @package Divante\MagentoIntegrationBundle\Command
*/
class ImportMappingCommand extends AbstractCommand
{
/**
* @var MapperImporter
*/
protected $mapperImporter;

/**
* @var string
*/
protected static $defaultName = 'integration-magento:import-mappings';

/**
* ImportMappingCommand constructor.
* @param MapperImporter $mapperImporter
* @param string|null $name
*/
public function __construct(MapperImporter $mapperImporter, string $name = null)
{
parent::__construct($name);
$this->mapperImporter = $mapperImporter;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setDescription('Import product or category mappings to selected configuration object');

$this->addArgument(
"idConfiguration",
InputArgument::REQUIRED,
"Id of selected integration configuration, you want to import mappings"
);

$this->addArgument(
"type",
InputArgument::REQUIRED,
"Type 'product' or 'category' mapping"
);

$this->addArgument(
"path",
InputArgument::REQUIRED,
"Path to json file"
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$start = microtime(true);
$idConfig = $input->getArgument("idConfiguration");
$type = $input->getArgument("type");
$filePath = $input->getArgument("path");

$file = new UploadedFile($filePath, "mappings");
try {
$this->mapperImporter->importMappingData($idConfig, $type, $file);
$output->writeln("<fg=green>Import Succeed!</>");
} catch (\Exception $exception) {
$errorMsg = $exception->getMessage();
$output->writeln("<fg=red>Import Failure!</>");
$output->writeln("<fg=red>Message: " . $errorMsg . "</>");
}

$timeElapsed = microtime(true) - $start;

$output->writeln(sprintf("<fg=green>Execution time : %.2f seconds</>", $timeElapsed));

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class SendCategoriesCommand extends AbstractCommand
{

private $updateSerice;
private $updateService;
protected static $defaultName = 'integration-magento:send:category';

/**
Expand All @@ -27,7 +27,7 @@ class SendCategoriesCommand extends AbstractCommand
public function __construct(BulkUpdateService $bulkUpdateService, string $name = null)
{
parent::__construct($name);
$this->updateSerice = $bulkUpdateService;
$this->updateService = $bulkUpdateService;
}

/**
Expand All @@ -40,7 +40,7 @@ protected function configure()
$this->addArgument(
"idCategory",
InputArgument::REQUIRED,
"Id or comma separated ids of produ180cts you want to send or 'all' if you want to send all of them"
"Id or comma separated ids of products you want to send or 'all' if you want to send all of them"
);

$this->addArgument(
Expand All @@ -60,8 +60,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
$start = microtime(true);
$idCategory = $input->getArgument("idCategory");
$idConfig = $input->getArgument("idConfiguration");
$this->updateSerice->setLogger(new ConsoleLogger($output));
$objects = $this->updateSerice->updateCategories($idCategory, $idConfig);
$this->updateService->setLogger(new ConsoleLogger($output));
$objects = $this->updateService->updateCategories($idCategory, $idConfig);

$timeElapsed = microtime(true) - $start;
$output->writeln("<fg=green>Send Categories command has succeeded.</>");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Divante\MagentoIntegrationBundle\Domain\Mapper\Exception;

/**
* Class MappingExportException
* @package Divante\MagentoIntegrationBundle\Domain\Mapper\Exception
*/
class MappingExportException extends \Exception
{
}
Loading