mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-27 17:31:09 -06:00
Split the importer and the setup routine.
This commit is contained in:
parent
5a9cf698f7
commit
d4510440b8
@ -11,6 +11,15 @@ declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Importer;
|
||||
|
||||
use FireflyIII\Crud\Account\AccountCrud;
|
||||
use FireflyIII\Import\Converter\ConverterInterface;
|
||||
use FireflyIII\Import\ImportEntry;
|
||||
use FireflyIII\Import\ImportResult;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use League\Csv\Reader;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class CsvImporter
|
||||
*
|
||||
@ -18,5 +27,99 @@ namespace FireflyIII\Import\Importer;
|
||||
*/
|
||||
class CsvImporter implements ImporterInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
public $job;
|
||||
|
||||
/** @var Account */
|
||||
public $defaultImportAccount;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual import
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function start(): bool
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$content = $this->job->uploadFileContents();
|
||||
|
||||
if ($config['import-account'] != 0) {
|
||||
$repository = app(AccountCrud::class, [$this->job->user]);
|
||||
$this->defaultImportAccount = $repository->find($config['import-account']);
|
||||
}
|
||||
|
||||
|
||||
// create CSV reader.
|
||||
$reader = Reader::createFromString($content);
|
||||
$start = $config['has-headers'] ? 1 : 0;
|
||||
$results = $reader->fetch();
|
||||
foreach ($results as $index => $row) {
|
||||
if ($index >= $start) {
|
||||
Log::debug(sprintf('Now going to import row %d.', $index));
|
||||
$this->importSingleRow($row);
|
||||
}
|
||||
}
|
||||
|
||||
Log::debug('This call should be intercepted somehow.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $row
|
||||
*
|
||||
* @return ImportResult
|
||||
*/
|
||||
private function importSingleRow(array $row): ImportResult
|
||||
{
|
||||
$object = new ImportEntry;
|
||||
$object->setUser($this->job->user);
|
||||
$config = $this->job->configuration;
|
||||
$result = new ImportResult;
|
||||
|
||||
foreach ($row as $index => $value) {
|
||||
// find the role for this column:
|
||||
$role = $config['column-roles'][$index] ?? '_ignore';
|
||||
$doMap = $config['column-do-mapping'][$index] ?? false;
|
||||
$converterClass = config('csv.import_roles.' . $role . '.converter');
|
||||
$mapping = $config['column-mapping-config'][$index] ?? [];
|
||||
/** @var ConverterInterface $converter */
|
||||
$converter = app('FireflyIII\\Import\\Converter\\' . $converterClass);
|
||||
// set some useful values for the converter:
|
||||
$converter->setMapping($mapping);
|
||||
$converter->setDoMap($doMap);
|
||||
$converter->setUser($this->job->user);
|
||||
$converter->setConfig($config);
|
||||
|
||||
// run the converter for this value:
|
||||
$convertedValue = $converter->convert($value);
|
||||
$certainty = $converter->getCertainty();
|
||||
|
||||
|
||||
// log it.
|
||||
Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]);
|
||||
|
||||
// store in import entry:
|
||||
$object->importValue($role, $value, $certainty, $convertedValue);
|
||||
// $object->fromRawValue($role, $value);
|
||||
}
|
||||
|
||||
$result = $object->import();
|
||||
if ($result->failed()) {
|
||||
Log::error('Import of row has failed.', $result->errors->toArray());
|
||||
}
|
||||
|
||||
exit;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -10,9 +10,25 @@
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Importer;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
|
||||
|
||||
/**
|
||||
* Interface ImporterInterface
|
||||
*
|
||||
* @package FireflyIII\Import\Importer
|
||||
*/
|
||||
interface ImporterInterface
|
||||
{
|
||||
/**
|
||||
* Run the actual import
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function start(): bool;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
*/
|
||||
public function setJob(ImportJob $job);
|
||||
}
|
@ -14,9 +14,6 @@ namespace FireflyIII\Import\Setup;
|
||||
|
||||
use ExpandedForm;
|
||||
use FireflyIII\Crud\Account\AccountCrud;
|
||||
use FireflyIII\Import\Converter\ConverterInterface;
|
||||
use FireflyIII\Import\ImportEntry;
|
||||
use FireflyIII\Import\ImportResult;
|
||||
use FireflyIII\Import\Mapper\MapperInterface;
|
||||
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
|
||||
use FireflyIII\Models\Account;
|
||||
@ -233,38 +230,6 @@ class CsvSetup implements SetupInterface
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual import
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function start(): bool
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$content = $this->job->uploadFileContents();
|
||||
|
||||
if ($config['import-account'] != 0) {
|
||||
$repository = app(AccountCrud::class, [auth()->user()]);
|
||||
$this->defaultImportAccount = $repository->find($config['csv_import_account']);
|
||||
}
|
||||
|
||||
|
||||
// create CSV reader.
|
||||
$reader = Reader::createFromString($content);
|
||||
$start = $config['has-headers'] ? 1 : 0;
|
||||
$results = $reader->fetch();
|
||||
foreach ($results as $index => $row) {
|
||||
if ($index >= $start) {
|
||||
Log::debug(sprintf('Now going to import row %d.', $index));
|
||||
$this->importSingleRow($row);
|
||||
}
|
||||
}
|
||||
|
||||
Log::debug('This call should be intercepted somehow.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the settings filled in by the user, if applicable.
|
||||
*
|
||||
@ -476,53 +441,4 @@ class CsvSetup implements SetupInterface
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $row
|
||||
*
|
||||
* @return ImportResult
|
||||
*/
|
||||
private function importSingleRow(array $row): ImportResult
|
||||
{
|
||||
$object = new ImportEntry;
|
||||
$object->setUser($this->job->user);
|
||||
$config = $this->job->configuration;
|
||||
$result = new ImportResult;
|
||||
|
||||
foreach ($row as $index => $value) {
|
||||
// find the role for this column:
|
||||
$role = $config['column-roles'][$index] ?? '_ignore';
|
||||
$doMap = $config['column-do-mapping'][$index] ?? false;
|
||||
$converterClass = config('csv.import_roles.' . $role . '.converter');
|
||||
$mapping = $config['column-mapping-config'][$index] ?? [];
|
||||
/** @var ConverterInterface $converter */
|
||||
$converter = app('FireflyIII\\Import\\Converter\\' . $converterClass);
|
||||
// set some useful values for the converter:
|
||||
$converter->setMapping($mapping);
|
||||
$converter->setDoMap($doMap);
|
||||
$converter->setUser($this->job->user);
|
||||
$converter->setConfig($config);
|
||||
|
||||
// run the converter for this value:
|
||||
$convertedValue = $converter->convert($value);
|
||||
$certainty = $converter->getCertainty();
|
||||
|
||||
|
||||
// log it.
|
||||
Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]);
|
||||
|
||||
// store in import entry:
|
||||
$object->importValue($role, $value, $certainty, $convertedValue);
|
||||
// $object->fromRawValue($role, $value);
|
||||
}
|
||||
|
||||
$result = $object->import();
|
||||
if ($result->failed()) {
|
||||
Log::error('Import of row has failed.', $result->errors->toArray());
|
||||
}
|
||||
|
||||
exit;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -24,13 +24,6 @@ use Symfony\Component\HttpFoundation\FileBag;
|
||||
interface SetupInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Run the actual import
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function start(): bool;
|
||||
|
||||
/**
|
||||
* After uploading, and after setJob(), prepare anything that is
|
||||
* necessary for the configure() line.
|
||||
|
Loading…
Reference in New Issue
Block a user