Refactor some code to handle command line imports.

This commit is contained in:
James Cole
2018-05-12 19:09:34 +02:00
parent 07da2fdda3
commit 9c507f7f62
10 changed files with 248 additions and 85 deletions

View File

@@ -55,14 +55,15 @@ class FakeRoutine implements RoutineInterface
public function run(): void
{
Log::debug(sprintf('Now in run() for fake routine with status: %s', $this->importJob->status));
if ($this->importJob->status !== 'running') {
throw new FireflyException('This fake job should not be started.'); // @codeCoverageIgnore
if ($this->importJob->status !== 'ready_to_run') {
throw new FireflyException(sprintf('Fake job should have status "ready_to_run", not "%s"', $this->importJob->status)); // @codeCoverageIgnore
}
switch ($this->importJob->stage) {
default:
throw new FireflyException(sprintf('Fake routine cannot handle stage "%s".', $this->importJob->stage)); // @codeCoverageIgnore
case 'new':
$this->repository->setStatus($this->importJob, 'running');
/** @var StageNewHandler $handler */
$handler = app(StageNewHandler::class);
$handler->run();
@@ -72,13 +73,15 @@ class FakeRoutine implements RoutineInterface
return;
case 'ahoy':
$this->repository->setStatus($this->importJob, 'running');
/** @var StageAhoyHandler $handler */
$handler = app(StageAhoyHandler::class);
$handler->run();
$this->repository->setStatus($this->importJob, 'need_job_config');
$this->repository->setStatus($this->importJob, 'ready_to_run');
$this->repository->setStage($this->importJob, 'final');
break;
case 'final':
$this->repository->setStatus($this->importJob, 'running');
/** @var StageFinalHandler $handler */
$handler = app(StageFinalHandler::class);
$handler->setImportJob($this->importJob);
@@ -96,7 +99,7 @@ class FakeRoutine implements RoutineInterface
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}

View File

@@ -48,10 +48,8 @@ class FileRoutine implements RoutineInterface
public function run(): void
{
Log::debug(sprintf('Now in run() for file routine with status: %s', $this->importJob->status));
if ($this->importJob->status !== 'running') {
throw new FireflyException('This file import job should not be started.'); // @codeCoverageIgnore
}
if ($this->importJob->stage === 'ready_to_run') {
if ($this->importJob->status === 'ready_to_run') {
$this->repository->setStatus($this->importJob, 'running');
// get processor, depending on file type
// is just CSV for now.
$processor = $this->getProcessor();

View File

@@ -163,6 +163,22 @@ class ImportArrayStorage
}
/**
* @param array $transaction
*
* @throws FireflyException
* @return string
*/
private function getHash(array $transaction): string
{
$json = json_encode($transaction);
if ($json === false) {
throw new FireflyException('Could not encode import array. Please see the logs.', $transaction); // @codeCoverageIgnore
}
return hash('sha256', $json, false);
}
/**
* Gets the users rules.
*
@@ -187,6 +203,7 @@ class ImportArrayStorage
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setUser($this->importJob->user);
$collector->setAllAssetAccounts()
->setTypes([TransactionType::TRANSFER])
->withOpposingAccount();
@@ -198,20 +215,13 @@ class ImportArrayStorage
/**
* Check if the hash exists for the array the user wants to import.
*
* @param array $transaction
* @param string $hash
*
* @return int|null
* @throws FireflyException
*/
private function hashExists(array $transaction): ?int
private function hashExists(string $hash): ?int
{
$json = json_encode($transaction);
if ($json === false) {
throw new FireflyException('Could not encode import array. Please see the logs.', $transaction); // @codeCoverageIgnore
}
$hash = hash('sha256', $json, false);
// find it!
$entry = $this->journalRepos->findByHash($hash);
if (null === $entry) {
return null;
@@ -328,12 +338,13 @@ class ImportArrayStorage
foreach ($array as $index => $transaction) {
Log::debug(sprintf('Now at item %d out of %d', $index + 1, $count));
$existingId = $this->hashExists($transaction);
$hash = $this->getHash($transaction);
$existingId = $this->hashExists($hash);
if (null !== $existingId) {
$this->logDuplicateObject($transaction, $existingId);
$this->repository->addErrorMessage(
$this->importJob, sprintf(
'Entry #%d ("%s") could not be imported. It already exists.',
'Row #%d ("%s") could not be imported. It already exists.',
$index, $transaction['description']
)
);
@@ -344,7 +355,7 @@ class ImportArrayStorage
$this->logDuplicateTransfer($transaction);
$this->repository->addErrorMessage(
$this->importJob, sprintf(
'Entry #%d ("%s") could not be imported. Such a transfer already exists.',
'Row #%d ("%s") could not be imported. Such a transfer already exists.',
$index,
$transaction['description']
)
@@ -352,7 +363,8 @@ class ImportArrayStorage
continue;
}
}
$toStore[] = $transaction;
$transaction['importHashV2'] = $hash;
$toStore[] = $transaction;
}
$count = \count($toStore);
if ($count === 0) {