Skip to content
Snippets Groups Projects
Commit 574e0d08 authored by Vadim Justus's avatar Vadim Justus
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
declare(strict_types=1);
namespace TechDivision\CsvSourceTransformationExample\Api;
interface MappingInterface
{
/**
* @param array $row
* @return array
*/
public function header(array $row): array;
/**
* @param array $row
* @return array
*/
public function row(array $row): array;
}
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
declare(strict_types=1);
namespace TechDivision\CsvSourceTransformationExample\Model\Executor;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Filesystem\File\ReadFactory;
use Magento\Framework\Filesystem\File\WriteFactory;
use Magento\Framework\Filesystem\Io\IoInterface;
use TechDivision\CsvSourceTransformationExample\Api\MappingInterface;
use TechDivision\ProcessPipelines\Api\ExecutorLoggerInterface;
use TechDivision\ProcessPipelines\Api\StepInterface;
use TechDivision\ProcessPipelines\Model\Executor\AbstractExecutor;
use TechDivision\ProcessPipelines\Model\Provider\AbsolutePathResolver;
/**
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Vadim Justus <v.justus@techdivision.com>
*/
class TransformationExecutor extends AbstractExecutor
{
/**
* @var ReadFactory
*/
private $readFactory;
/**
* @var WriteFactory
*/
private $writeFactory;
/**
* @var AbsolutePathResolver
*/
private $absolutePathResolver;
/**
* @var MappingInterface
*/
private $mapping;
/**
* @var IoInterface
*/
private $io;
/**
* @param ExecutorLoggerInterface $logger
* @param ReadFactory $readFactory
* @param WriteFactory $writeFactory
* @param AbsolutePathResolver $absolutePathResolver
* @param MappingInterface $mapping
* @param IoInterface $io
*/
public function __construct(
ExecutorLoggerInterface $logger,
ReadFactory $readFactory,
WriteFactory $writeFactory,
AbsolutePathResolver $absolutePathResolver,
MappingInterface $mapping,
IoInterface $io
) {
parent::__construct($logger);
$this->readFactory = $readFactory;
$this->writeFactory = $writeFactory;
$this->absolutePathResolver = $absolutePathResolver;
$this->mapping = $mapping;
$this->io = $io;
}
/**
* @inheritdoc
*/
public function process(StepInterface $step): void
{
$timestamp = date('Ymd-His');
$workingDirectory = $this->absolutePathResolver->get($step->getWorkingDirectory());
$importFiles = [];
$fileCount = 0;
$files = (array)$step->getArgumentValueByKey('files');
foreach ($files as $filepath) {
$fileInfo = new \SplFileInfo($filepath);
if (strtolower($fileInfo->getExtension()) === 'ok') {
continue;
}
$fileCount++;
$sourceFileName = $fileInfo->getBasename();
$source = $this->readFactory->create($workingDirectory . '/' . $sourceFileName, DriverPool::FILE);
$targetFileName = 'product-import_'
. $timestamp
. '_' . str_pad((string)$fileCount, 2, '0', STR_PAD_LEFT)
. '.csv';
$importFiles[] = $targetFileName;
$targetPath = $workingDirectory . '/' . $targetFileName;
$this->io->write($targetPath, '');
$target = $this->writeFactory->create($targetPath, DriverPool::FILE, "w");
$header = $source->readCsv();
$target->writeCsv($this->mapping->header($header));
while (($row = $source->readCsv()) !== false) {
$target->writeCsv($this->mapping->row(array_combine($header, $row)));
}
$source->close();
$target->close();
}
$okFileName = 'product-import_' . $timestamp . '.ok';
$this->io->write($workingDirectory . '/' . $okFileName, implode(PHP_EOL, $importFiles));
}
}
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
declare(strict_types=1);
namespace TechDivision\CsvSourceTransformationExample\Model\Mapping;
use TechDivision\CsvSourceTransformationExample\Api\MappingInterface;
/**
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Vadim Justus <v.justus@techdivision.com>
*/
class Categories implements MappingInterface
{
/**
* @inheritDoc
*/
public function header(array $row): array
{
$keys = array_flip($row);
$key = $keys['cat_path'];
$row[$key] = 'categories';
return $row;
}
/**
* @inheritDoc
*/
public function row(array $row): array
{
$categories = explode(',', $row['cat_path']);
foreach ($categories as $key => $category) {
$categories[$key] = 'Default Category/' . $category;
}
$row['cat_path'] = implode(',', $categories);
return $row;
}
}
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
declare(strict_types=1);
namespace TechDivision\CsvSourceTransformationExample\Model\Mapping;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;
use TechDivision\CsvSourceTransformationExample\Api\MappingInterface;
/**
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Vadim Justus <v.justus@techdivision.com>
*/
class WebsiteCode implements MappingInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var string
*/
private $defaultWebsiteCode;
/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(StoreManagerInterface $storeManager)
{
$this->storeManager = $storeManager;
}
/**
* @inheritDoc
*/
public function header(array $row): array
{
return $row;
}
/**
* @return string
* @throws LocalizedException
*/
private function getDefaultWebsiteCode(): string
{
if ($this->defaultWebsiteCode === null) {
$defaultWebsiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
$this->defaultWebsiteCode = (string)$this->storeManager->getWebsite($defaultWebsiteId)->getCode();
}
return $this->defaultWebsiteCode;
}
/**
* @inheritDoc
* @throws LocalizedException
*/
public function row(array $row): array
{
$value = $row['product_websites'];
if ($value === '_DEFAULT_') {
$row['product_websites'] = $this->getDefaultWebsiteCode();
}
return $row;
}
}
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
declare(strict_types=1);
namespace TechDivision\CsvSourceTransformationExample\Model;
use Magento\Framework\Exception\LocalizedException;
use TechDivision\CsvSourceTransformationExample\Api\MappingInterface;
/**
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Vadim Justus <v.justus@techdivision.com>
*/
class MappingChain implements MappingInterface
{
/**
* @var MappingInterface[]
*/
private $mapper;
/**
* @param MappingInterface[] $mapper
* @throws LocalizedException
*/
public function __construct(array $mapper)
{
foreach ($mapper as $mapperInstance) {
if (!($mapperInstance instanceof MappingInterface)) {
throw new LocalizedException(__(
'Mapper "%class" needs to be instance of "%interface"',
[
'class' => get_class($mapperInstance),
'interface' => MappingInterface::class
]
));
}
}
$this->mapper = $mapper;
}
/**
* @inheritDoc
*/
public function header(array $row): array
{
foreach ($this->mapper as $mapper) {
$row = $mapper->header($row);
}
return $row;
}
/**
* @inheritDoc
*/
public function row( array $row): array
{
foreach ($this->mapper as $mapper) {
$row = $mapper->row($row);
}
return $row;
}
}
{
"name": "techdivision/csv-source-transformation-example",
"description": "Example module for Pacemaker customizations",
"license": "proprietary",
"require": {
"magento/framework": "^101.0.9 | ^102.0.0 | ^103.0.0",
"techdivision/pacemaker-import-catalog": "*",
"techdivision/pacemaker-import-base": "*",
"techdivision/process-pipelines": "*"
},
"type": "magento2-module",
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"TechDivision\\CsvSourceTransformationExample\\": ""
}
},
"repositories": [
{
"type": "composer",
"url": "https://repo.met.tdintern.de/"
},
{
"type": "composer",
"url": "https://repo.magento.com/"
}
]
}
<?xml version="1.0"?>
<!--
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @author Vadim Justus <v.justus@techdivision.com>
* @link http://www.techdivision.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<techdivision_pacemaker_import>
<catalog>
<enabled>1</enabled>
<file_name_pattern><![CDATA[/pim-source_(?P<identifier>[0-9a-z\-]*)([_0-9]*?).(csv|ok)/i]]></file_name_pattern>
</catalog>
</techdivision_pacemaker_import>
</default>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @author Vadim Justus <v.justus@techdivision.com>
* @link http://www.techdivision.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\TechDivision\CsvSourceTransformationExample\Model\Executor\TransformationExecutor">
<arguments>
<argument name="io" xsi:type="object">Magento\Framework\Filesystem\Io\File</argument>
</arguments>
</type>
<preference for="TechDivision\CsvSourceTransformationExample\Api\MappingInterface" type="TechDivision\CsvSourceTransformationExample\Model\MappingChain" />
<type name="TechDivision\CsvSourceTransformationExample\Model\MappingChain">
<arguments>
<argument name="mapper" xsi:type="array">
<item name="categories" xsi:type="object">TechDivision\CsvSourceTransformationExample\Model\Mapping\Categories</item>
<item name="website_code" xsi:type="object">TechDivision\CsvSourceTransformationExample\Model\Mapping\WebsiteCode</item>
</argument>
</arguments>
</type>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @author Vadim Justus <v.justus@techdivision.com>
* @link http://www.techdivision.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="TechDivision_CsvSourceTransformationExample" setup_version="1.0.0">
<sequence>
<module name="TechDivision_PacemakerImportCatalog"/>
</sequence>
</module>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*
* @copyright Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
* @author Vadim Justus <v.justus@techdivision.com>
* @link http://www.techdivision.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:TechDivision_ProcessPipelines:etc/pipeline.xsd">
<pipeline name="pacemaker_import_catalog">
<step name="product_transformation" executorType="TechDivision\CsvSourceTransformationExample\Model\Executor\TransformationExecutor" />
</pipeline>
</config>
<?php
/**
* Copyright (c) 2020 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* license@techdivision.com
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'TechDivision_CsvSourceTransformationExample',
__DIR__
);
pim-source_20200211-120400_01.csv
sku,store_view_code,attribute_set_code,product_type,cat_path,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,related_position,crosssell_skus,crosssell_position,upsell_skus,upsell_position,additional_images,additional_image_labels,hide_from_product_page,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,bundle_shipment_type,configurable_variations,configurable_variation_labels,associated_skus
MS06-XS-Blue,,Top,simple,Men/Tops/Tees,_DEFAULT_,Zoltan Gym Tee-XS-Blue,"<p>This short-sleeve wonder works twice as hard to give you good gym days and good looks. The Zoltan Gym Tee helps you stay comfortable, while the looser sleeves and flatlock seams keep you moving in chafe-free comfort.</p>
<p>&bull; Relaxed fit. <br />&bull; Crew neckline. <br />&bull; Machine wash/dry.</p>",,1,1,Taxable Goods,Not Visible Individually,29,,,,zoltan-gym-tee-xs-blue,,,,/m/s/ms06-blue_main.jpg,,/m/s/ms06-blue_main.jpg,,/m/s/ms06-blue_main.jpg,,,,"10/24/16, 12:36 PM","10/24/16, 12:36 PM",,,Block after Info Column,,,,,,,,,,,Use config,,"color=Blue,size=XS",100,0,1,0,0,1,1,1,0,1,1,,1,0,1,1,0,1,0,0,0,,,,,,,"/m/s/ms06-blue_alt1.jpg,/m/s/ms06-blue_back.jpg,/m/s/ms06-blue_main.jpg",",,",,,,,,,,,,
MS06-XS-Green,,Top,simple,Men/Tops/Tees,_DEFAULT_,Zoltan Gym Tee-XS-Green,"<p>This short-sleeve wonder works twice as hard to give you good gym days and good looks. The Zoltan Gym Tee helps you stay comfortable, while the looser sleeves and flatlock seams keep you moving in chafe-free comfort.</p>
<p>&bull; Relaxed fit. <br />&bull; Crew neckline. <br />&bull; Machine wash/dry.</p>",,1,1,Taxable Goods,Not Visible Individually,29,,,,zoltan-gym-tee-xs-green,,,,/m/s/ms06-green_main.jpg,,/m/s/ms06-green_main.jpg,,/m/s/ms06-green_main.jpg,,,,"10/24/16, 12:36 PM","10/24/16, 12:36 PM",,,Block after Info Column,,,,,,,,,,,Use config,,"color=Green,size=XS",100,0,1,0,0,1,1,1,0,1,1,,1,0,1,1,0,1,0,0,0,,,,,,,/m/s/ms06-green_main.jpg,,,,,,,,,,,
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment