Massive rewrite for import routine, part 1.

This commit is contained in:
James Cole
2017-12-16 08:03:35 +01:00
parent 985cc100e2
commit 84b6708260
34 changed files with 671 additions and 244 deletions

View File

@@ -50,80 +50,16 @@ class BankController extends Controller
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
$importJob = $repository->create($bank);
$importJob = $repository->create($bank);
$config = $importJob->configuration;
$config['has-config-file'] = false;
$config['auto-start'] = true;
$importJob->configuration = $config;
$importJob->save();
return redirect(route('import.file.configure', [$importJob->key]));
}
/**
* This method processes the prerequisites the user has entered in the previous step.
*
* Whatever storePrerequisites does, it should make sure that the system is ready to continue immediately. So
* no extra calls or stuff, except maybe to open a session
*
* @see PrerequisitesInterface::storePrerequisites
*
* @param Request $request
* @param string $bank
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws FireflyException
*/
public function postPrerequisites(Request $request, string $bank)
{
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
$class = config(sprintf('firefly.import_pre.%s', $bank));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if (!$object->hasPrerequisites()) {
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
return redirect(route('import.bank.create-job', [$bank]));
}
Log::debug('Going to store entered preprerequisites.');
// store post data
$result = $object->storePrerequisites($request);
if ($result->count() > 0) {
Session::flash('error', $result->first());
return redirect(route('import.bank.prerequisites', [$bank]));
}
return redirect(route('import.bank.create-job', [$bank]));
}
/**
* This method shows you, if necessary, a form that allows you to enter any required values, such as API keys,
* login passwords or other values.
*
* @param string $bank
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* @throws FireflyException
*/
public function prerequisites(string $bank)
{
$class = config(sprintf('firefly.import_pre.%s', $bank));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
$view = $object->getView();
$parameters = ['title' => strval(trans('firefly.import_index_title')), 'mainTitleIcon' => 'fa-archive'];
$parameters = $object->getViewParameters() + $parameters;
return view($view, $parameters);
}
return redirect(route('import.bank.create-job', [$bank]));
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Import\Configuration\ConfiguratorInterface;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class ConfigurationController
*/
class ConfigurationController extends Controller
{
/** @var ImportJobRepositoryInterface */
public $repository;
/**
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-archive');
app('view')->share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Configure the job. This method is returned to until job is deemed "configured".
*
* @param ImportJob $job
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* @throws FireflyException
*/
public function index(ImportJob $job)
{
// create configuration class:
$configurator = $this->makeConfigurator($job);
// is the job already configured?
if ($configurator->isJobConfigured()) {
$this->repository->updateStatus($job, 'configured');
return redirect(route('import.file.status', [$job->key]));
}
$view = $configurator->getNextView();
$data = $configurator->getNextData();
$subTitle = trans('firefly.import_config_bread_crumb');
$subTitleIcon = 'fa-wrench';
return view($view, compact('data', 'job', 'subTitle', 'subTitleIcon'));
}
/**
* @param ImportJob $job
*
* @return ConfiguratorInterface
*
* @throws FireflyException
*/
private function makeConfigurator(ImportJob $job): ConfiguratorInterface
{
$type = $job->file_type;
$key = sprintf('import.configuration.%s', $type);
$className = config($key);
if (null === $className || !class_exists($className)) {
throw new FireflyException(sprintf('Cannot find configurator class for job of type "%s".',$type)); // @codeCoverageIgnore
}
/** @var ConfiguratorInterface $configurator */
$configurator = app($className);
$configurator->setJob($job);
return $configurator;
}
}

View File

@@ -54,8 +54,8 @@ class FileController extends Controller
$this->middleware(
function ($request, $next) {
View::share('mainTitleIcon', 'fa-archive');
View::share('title', trans('firefly.import_index_title'));
app('view')->share('mainTitleIcon', 'fa-archive');
app('view')->share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request);
@@ -107,6 +107,7 @@ class FileController extends Controller
$config['column-roles-complete'] = false;
$config['column-mapping-complete'] = false;
$config['initial-config-complete'] = false;
$config['has-file-upload'] = false;
$config['delimiter'] = "\t" === $config['delimiter'] ? 'tab' : $config['delimiter'];
$result = json_encode($config, JSON_PRETTY_PRINT);

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use View;
/**
* Class FileController.
*/
class IndexController extends Controller
{
/** @var ImportJobRepositoryInterface */
public $repository;
/**
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-archive');
app('view')->share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Creates a new import job for $bank with the default (global) job configuration.
*
* @param string $bank
*
* @throws FireflyException
*/
public function create(string $bank)
{
if (!(config(sprintf('import.enabled.%s', $bank))) === true) {
throw new FireflyException(sprintf('Cannot import from "%s" at this time.', $bank));
}
$importJob = $this->repository->create($bank);
// from here, always go to configure step.
return redirect(route('import.configure', [$importJob->key]));
}
/**
* General import index.
*
* @return View
*/
public function index()
{
$subTitle = trans('firefly.import_index_sub_title');
$subTitleIcon = 'fa-home';
$routines = config('import.enabled');
return view('import.index', compact('subTitle', 'subTitleIcon', 'routines'));
}
}

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Import\Prerequisites\PrerequisitesInterface;
use Illuminate\Http\Request;
use Log;
/**
* Class PrerequisitesController
*/
class PrerequisitesController extends Controller
{
/**
* Once there are no prerequisites, this method will create an importjob object and
* redirect the user to a view where this object can be used by a bank specific
* class to process.
*
* @param string $bank
*
* @return \Illuminate\Http\RedirectResponse|null
* @throws FireflyException
*/
public function index(string $bank)
{
if (!(config(sprintf('import.enabled.%s', $bank))) === true) {
throw new FireflyException(sprintf('Cannot import from "%s" at this time.', $bank));
}
$class = strval(config(sprintf('import.prerequisites.%s', $bank)));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle "%s".', $bank));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
$view = $object->getView();
$parameters = ['title' => strval(trans('firefly.import_index_title')), 'mainTitleIcon' => 'fa-archive'];
$parameters = $object->getViewParameters() + $parameters;
return view($view, $parameters);
}
// if no (more) prerequisites, return to create a job:
return redirect(route('import.create-job', [$bank]));
}
/**
* This method processes the prerequisites the user has entered in the previous step.
*
* Whatever storePrerequisites does, it should make sure that the system is ready to continue immediately. So
* no extra calls or stuff, except maybe to open a session
*
* @see PrerequisitesInterface::storePrerequisites
*
* @param Request $request
* @param string $bank
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws FireflyException
*/
public function postPrerequisites(Request $request, string $bank)
{
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
$class = config(sprintf('firefly.import_pre.%s', $bank));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if (!$object->hasPrerequisites()) {
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
return redirect(route('import.create-job', [$bank]));
}
Log::debug('Going to store entered prerequisites.');
// store post data
$result = $object->storePrerequisites($request);
if ($result->count() > 0) {
$request->session()->flash('error', $result->first());
return redirect(route('import.prerequisites', [$bank]));
}
return redirect(route('import.create-job', [$bank]));
}
}

View File

@@ -1,72 +0,0 @@
<?php
/**
* ImportController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use View;
/**
* Class ImportController.
*/
class ImportController extends Controller
{
/** @var ImportJobRepositoryInterface */
public $repository;
/**
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
View::share('mainTitleIcon', 'fa-archive');
View::share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request);
}
);
}
/**
* General import index.
*
* @return View
*/
public function index()
{
$subTitle = trans('firefly.import_index_sub_title');
$subTitleIcon = 'fa-home';
$importFileTypes = [];
$defaultImportType = config('firefly.default_import_format');
foreach (array_keys(config('firefly.import_formats')) as $type) {
$importFileTypes[$type] = trans('firefly.import_file_type_' . $type);
}
return view('import.index', compact('subTitle', 'subTitleIcon', 'importFileTypes', 'defaultImportType'));
}
}