diff --git a/app/Models/ImportJob.php b/app/Models/ImportJob.php index a984176a9b..e3744e4368 100644 --- a/app/Models/ImportJob.php +++ b/app/Models/ImportJob.php @@ -43,11 +43,6 @@ class ImportJob extends Model public $validStatus = [ 'new', - 'configuring', - 'configured', - 'running', - 'error', - 'finished', ]; /** * The attributes that should be casted to native types. @@ -56,9 +51,14 @@ class ImportJob extends Model */ protected $casts = [ - 'created_at' => 'datetime', - 'updated_at' => 'datetime', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + 'configuration' => 'array', + 'extended_status' => 'array', + 'transactions' => 'array', ]; + /** @var array */ + protected $fillable = ['key', 'user_id', 'file_type', 'source', 'status', 'stage', 'configuration', 'extended_status', 'transactions']; /** * @param $value @@ -86,9 +86,11 @@ class ImportJob extends Model } /** + * @deprecated + * * @param int $count */ - public function addTotalSteps(int $count) + public function addTotalSteps(int $count): void { $status = $this->extended_status; $status['steps'] += $count; @@ -97,88 +99,9 @@ class ImportJob extends Model Log::debug(sprintf('Add %d to total steps for job "%s" making total steps %d', $count, $this->key, $status['steps'])); } - /** - * @param string $status - * - * @throws FireflyException - */ - public function change(string $status): void - { - if (\in_array($status, $this->validStatus)) { - Log::debug(sprintf('Job status set (in model) to "%s"', $status)); - $this->status = $status; - $this->save(); - - return; - } - throw new FireflyException(sprintf('Status "%s" is invalid for job "%s".', $status, $this->key)); - - } - - /** - * @param $value - * - * @return mixed - */ - public function getConfigurationAttribute($value) - { - if (null === $value) { - return []; - } - if (0 === \strlen($value)) { - return []; - } - - return json_decode($value, true); - } - - /** - * @param $value - * - * @return mixed - */ - public function getExtendedStatusAttribute($value) - { - if (0 === \strlen((string)$value)) { - return []; - } - - return json_decode($value, true); - } - - /** - * @codeCoverageIgnore - * - * @param $value - */ - public function setConfigurationAttribute($value) - { - $this->attributes['configuration'] = json_encode($value); - } - - /** - * @codeCoverageIgnore - * - * @param $value - */ - public function setExtendedStatusAttribute($value) - { - $this->attributes['extended_status'] = json_encode($value); - } - - /** - * @param $value - */ - public function setStatusAttribute(string $value) - { - if (\in_array($value, $this->validStatus)) { - $this->attributes['status'] = $value; - } - } - /** * @return string - * + * @deprecated * @throws \Illuminate\Contracts\Encryption\DecryptException * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ diff --git a/app/Repositories/ImportJob/ImportJobRepository.php b/app/Repositories/ImportJob/ImportJobRepository.php index 353c33e9bb..f62f16e21d 100644 --- a/app/Repositories/ImportJob/ImportJobRepository.php +++ b/app/Repositories/ImportJob/ImportJobRepository.php @@ -108,34 +108,34 @@ class ImportJobRepository implements ImportJobRepositoryInterface } /** - * @param string $type + * @param string $importProvider * * @return ImportJob * * @throws FireflyException */ - public function create(string $type): ImportJob + public function create(string $importProvider): ImportJob { - $count = 0; - $type = strtolower($type); + $count = 0; + $importProvider = strtolower($importProvider); while ($count < 30) { $key = Str::random(12); $existing = $this->findByKey($key); if (null === $existing->id) { - $importJob = new ImportJob; - $importJob->user()->associate($this->user); - $importJob->file_type = $type; - $importJob->key = Str::random(12); - $importJob->status = 'new'; - $importJob->configuration = []; - $importJob->extended_status = [ - 'steps' => 0, - 'done' => 0, - 'tag' => 0, - 'errors' => [], - ]; - $importJob->save(); + $importJob = ImportJob::create( + [ + 'user_id' => $this->user->id, + 'source' => $importProvider, + 'file_type' => '', + 'key' => Str::random(12), + 'status' => 'new', + 'stage' => 'new', + 'configuration' => [], + 'extended_status' => [], + 'transactions' => [], + ] + ); // breaks the loop: return $importJob; diff --git a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php index 5bb3a905cb..bb56921445 100644 --- a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php +++ b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php @@ -67,11 +67,11 @@ interface ImportJobRepositoryInterface public function countByHash(string $hash): int; /** - * @param string $type + * @param string $importProvider * * @return ImportJob */ - public function create(string $type): ImportJob; + public function create(string $importProvider): ImportJob; /** * @param string $key diff --git a/app/Support/Binder/ImportProvider.php b/app/Support/Binder/ImportProvider.php new file mode 100644 index 0000000000..3e82d332d5 --- /dev/null +++ b/app/Support/Binder/ImportProvider.php @@ -0,0 +1,55 @@ +. + */ +declare(strict_types=1); + +namespace FireflyIII\Support\Binder; + +use Carbon\Carbon; +use Illuminate\Routing\Route; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + +/** + * Class ImportProvider. + */ +class ImportProvider implements BinderInterface +{ + /** + * @param string $value + * @param Route $route + * + * @return Carbon + * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException + */ + public static function routeBinder(string $value, Route $route): string + { + $providers = (array)config('import.enabled'); + $allowed = []; + foreach ($providers as $name => $enabled) { + if ($enabled || (bool)config('app.debug') === true) { + $allowed[] = $name; + } + } + if (\in_array($value, $allowed, true)) { + return $value; + } + throw new NotFoundHttpException; + } +}