Skip to content

Commit ef8a1a8

Browse files
Introduce SourceTypePool and remove source_type declaration from SourceProcessorPool
1 parent c543b0f commit ef8a1a8

10 files changed

+307
-249
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ImportService\Model\Import\Processor;
9+
10+
use Magento\ImportService\Api\Data\SourceInterface;
11+
use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
12+
use Magento\ImportService\Model\Import\SourceTypePool;
13+
14+
/**
15+
* Define the source type pool and process the request
16+
*/
17+
abstract class AbstractSourceProcessor implements SourceProcessorInterface
18+
{
19+
/**
20+
* @var SourceTypePool
21+
*/
22+
protected $sourceTypePool;
23+
24+
/**
25+
* @param SourceTypePool $sourceTypePool
26+
*/
27+
public function __construct(
28+
SourceTypePool $sourceTypePool
29+
) {
30+
$this->sourceTypePool = $sourceTypePool;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*
36+
* @throws ImportServiceException
37+
* @return SourceTypeInterface
38+
*/
39+
public function processUpload(SourceInterface $source, SourceUploadResponseInterface $response)
40+
{
41+
/** @var \Magento\ImportService\Model\Import\Type\SourceTypeInterface $sourceType */
42+
$sourceType = $this->sourceTypePool->getSourceType($source);
43+
44+
/** save processed content get updated source object */
45+
$source = $sourceType->save($source);
46+
47+
/** return response with details */
48+
return $response->setSource($source)->setSourceId($source->getSourceId())->setStatus($source->getStatus());
49+
}
50+
}

app/code/Magento/ImportService/Model/Import/Processor/Base64EncodedDataProcessor.php

+8-56
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,31 @@
99

1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\App\Filesystem\DirectoryList;
12-
use Magento\ImportService\Exception as ImportServiceException;
12+
use Magento\ImportService\Api\Data\SourceInterface;
13+
use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
1314

1415
/**
1516
* Base64 encoded data processor for asynchronous import
1617
*/
17-
class Base64EncodedDataProcessor implements SourceProcessorInterface
18+
class Base64EncodedDataProcessor extends AbstractSourceProcessor
1819
{
1920
/**
2021
* Import Type
2122
*/
2223
const IMPORT_TYPE = 'base64_encoded_data';
2324

24-
/**
25-
* CSV Source Type
26-
*/
27-
const SOURCE_TYPE_CSV = 'csv';
28-
29-
/**
30-
* The destination directory
31-
*/
32-
const DIR_IMPORT_DESTINATION = 'import/';
33-
34-
/**
35-
* @var \Magento\Framework\Filesystem
36-
*/
37-
private $filesystem;
38-
39-
/**
40-
* LocalPathFileProcessor constructor.
41-
* @param Filesystem $filesystem
42-
*/
43-
public function __construct(
44-
Filesystem $filesystem
45-
) {
46-
$this->filesystem = $filesystem;
47-
}
48-
4925
/**
5026
* {@inheritdoc}
5127
*/
52-
public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $source, \Magento\ImportService\Api\Data\SourceUploadResponseInterface $response)
28+
public function processUpload(SourceInterface $source, SourceUploadResponseInterface $response)
5329
{
54-
/** @var string $fileName */
55-
$fileName = rand();
56-
57-
/** @var string $contentFilePath */
58-
$contentFilePath = self::DIR_IMPORT_DESTINATION
59-
. $fileName
60-
. '.'
61-
. $source->getSourceType();
62-
6330
/** @var string $content */
6431
$content = base64_decode($source->getImportData());
6532

66-
/** @var Magento\Framework\Filesystem\Directory\Write $var */
67-
$var = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
68-
69-
if(!$var->writeFile($contentFilePath, $content))
70-
{
71-
/** @var array $lastError */
72-
$lastError = error_get_last();
73-
74-
/** @var string $errorMessage */
75-
$errorMessage = isset($lastError['message']) ? $lastError['message'] : '';
76-
77-
throw new ImportServiceException(
78-
__('Cannot copy the remote file: %1', $errorMessage)
79-
);
80-
}
81-
82-
/** Update source's import data */
83-
$source->setImportData($fileName);
33+
/** Set decoded imported data */
34+
$source->setImportData($content);
8435

85-
return $response->setSource($source)->setSourceId($fileName)->setStatus($response::STATUS_UPLOADED);
36+
/** process source and get response details */
37+
return parent::processUpload($source, $response);
8638
}
8739
}

app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php

+24-22
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77

88
namespace Magento\ImportService\Model\Import\Processor;
99

10+
use Magento\Framework\Filesystem\Io\File;
11+
use Magento\ImportService\Api\Data\SourceInterface;
12+
use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
1013
use Magento\Framework\App\Filesystem\DirectoryList;
1114
use Magento\Framework\Filesystem;
12-
use Magento\ImportService\Exception as ImportServiceException;
13-
use Magento\ImportService\Model\Import\SourceProcessorPool;
15+
use Magento\ImportService\Model\Import\SourceTypePool;
1416
use Magento\ImportService\Model\Source\Validator;
17+
use Magento\ImportService\ImportServiceException;
1518

1619
/**
1720
* CSV files processor for asynchronous import
1821
*/
19-
class ExternalFileProcessor implements SourceProcessorInterface
22+
class ExternalFileProcessor extends AbstractSourceProcessor
2023
{
24+
/**
25+
* Import Type
26+
*/
27+
const IMPORT_TYPE = 'external';
28+
2129
/**
2230
* @var \Magento\Framework\Filesystem
2331
*/
@@ -31,15 +39,18 @@ class ExternalFileProcessor implements SourceProcessorInterface
3139
/**
3240
* LocalPathFileProcessor constructor
3341
*
34-
* @param FileSystem $fileSystem
42+
* @param Filesystem $fileSystem
3543
* @param Validator $validator
44+
* @param SourceTypePool $sourceTypePool
3645
*/
3746
public function __construct(
38-
FileSystem $fileSystem,
39-
Validator $validator
47+
Filesystem $fileSystem,
48+
Validator $validator,
49+
SourceTypePool $sourceTypePool
4050
) {
4151
$this->fileSystem = $fileSystem;
4252
$this->validator = $validator;
53+
parent::__construct($sourceTypePool);
4354
}
4455

4556
/**
@@ -68,25 +79,16 @@ public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $s
6879
);
6980
}
7081

71-
/** @var string $workingDirectory */
72-
$workingDirectory = SourceProcessorPool::WORKING_DIR;
73-
74-
/** @var string $fileName */
75-
$fileName = uniqid() . '.' . $source->getSourceType();
76-
7782
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $writeInterface */
78-
$writeInterface = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
79-
80-
/** If the directory is not present, it will be created */
81-
$writeInterface->create($workingDirectory);
83+
$writeInterface = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
8284

83-
/** @var string $copyFileFullPath*/
84-
$copyFileFullPath = $writeInterface->getAbsolutePath($workingDirectory) . $fileName;
85+
/** read content from external link */
86+
$content = $writeInterface->getDriver()->fileGetContents($source->getImportData());
8587

86-
/** Attempt a copy, may throw \Magento\Framework\Exception\FileSystemException */
87-
$writeInterface->getDriver()->copy($source->getImportData(), $copyFileFullPath);
88+
/** Set downloaded data */
89+
$source->setImportData($content);
8890

89-
return $response->setSource($source->setImportData($fileName))
90-
->setStatus($response::STATUS_UPLOADED);
91+
/** process source and get response details */
92+
return parent::processUpload($source, $response);
9193
}
9294
}

0 commit comments

Comments
 (0)