First code for Spectre import.

This commit is contained in:
James Cole
2018-05-14 17:59:43 +02:00
parent 96411b17e9
commit 69019d5215
8 changed files with 206 additions and 179 deletions

View File

@@ -116,7 +116,7 @@ class CreateImport extends Command
// make prerequisites thing.
$class = (string)config(sprintf('import.prerequisites.%s', $provider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $provider)); // @codeCoverageIgnore
throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $provider)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $object */
$object = app($class);

View File

@@ -85,14 +85,13 @@ class IndexController extends Controller
Log::debug(sprintf('Created job #%d for provider %s', $importJob->id, $importProvider));
$hasPreReq = (bool)config(sprintf('import.has_prereq.%s', $importProvider));
$hasConfig = (bool)config(sprintf('import.has_config.%s', $importProvider));
$hasConfig = (bool)config(sprintf('import.has_job_config.%s', $importProvider));
// if job provider has no prerequisites:
if ($hasPreReq === false) {
Log::debug('Provider has no prerequisites. Continue.');
// @codeCoverageIgnoreStart
// if job provider also has no configuration:
if ($hasConfig === false) {
Log::debug('Provider has no configuration. Job is ready to start.');
Log::debug('Provider needs no configuration for job. Job is ready to start.');
$this->repository->updateStatus($importJob, 'ready_to_run');
Log::debug('Redirect to status-page.');
@@ -106,13 +105,12 @@ class IndexController extends Controller
Log::debug('Redirect to configuration.');
return redirect(route('import.job.configuration.index', [$importJob->key]));
// @codeCoverageIgnoreEnd
}
Log::debug('Job provider has prerequisites.');
// if need to set prerequisites, do that first.
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore
throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $importProvider)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $providerPre */
$providerPre = app($class);
@@ -189,7 +187,6 @@ class IndexController extends Controller
$providers[$providerName] = [
'has_prereq' => (bool)config('import.has_prereq.' . $providerName),
'has_config' => (bool)config('import.has_config.' . $providerName),
];
$class = (string)config(sprintf('import.prerequisites.%s', $providerName));
$result = false;

View File

@@ -83,7 +83,7 @@ class JobConfigurationController extends Controller
// if provider has no config, just push it through:
$importProvider = $importJob->provider;
if (!(bool)config(sprintf('import.has_config.%s', $importProvider))) {
if (!(bool)config(sprintf('import.has_job_config.%s', $importProvider))) {
// @codeCoverageIgnoreStart
Log::debug('Job needs no config, is ready to run!');
$this->repository->updateStatus($importJob, 'ready_to_run');

View File

@@ -83,7 +83,7 @@ class PrerequisitesController extends Controller
app('view')->share('subTitle', trans('import.prerequisites_breadcrumb_' . $importProvider));
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore
throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $importProvider)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $object */
$object = app($class);

View File

@@ -24,18 +24,15 @@ namespace FireflyIII\Import\Prerequisites;
use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Log;
use Preferences;
/**
* This class contains all the routines necessary to connect to Spectre.
*/
class SpectrePrerequisites implements PrerequisitesInterface
{
// /** @var User */
// private $user;
/** @var User */
private $user;
//
// /**
// * Returns view name that allows user to fill in prerequisites. Currently asks for the API key.
@@ -204,7 +201,20 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/
public function getViewParameters(): array
{
return [];
/** @var Preference $appIdPreference */
$appIdPreference = app('preferences')->getForUser($this->user, 'spectre_app_id', null);
$appId = null === $appIdPreference ? '' : $appIdPreference->data;
/** @var Preference $secretPreference */
$secretPreference = app('preferences')->getForUser($this->user, 'spectre_secret', null);
$secret = null === $secretPreference ? '' : $secretPreference->data;
return [
'app_id' => $appId,
'secret' => $secret,
];
}
/**
@@ -224,6 +234,7 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
@@ -239,4 +250,20 @@ class SpectrePrerequisites implements PrerequisitesInterface
{
return new MessageBag;
}
/**
* @return bool
*/
private function hasAppId(): bool
{
$appId = app('preferences')->getForUser($this->user, 'spectre_app_id', null);
if (null === $appId) {
return false;
}
if ('' === (string)$appId->data) {
return false;
}
return true;
}
}

View File

@@ -36,7 +36,7 @@ return [
'fake' => true,
'file' => true,
'bunq' => false,
'spectre' => false,
'spectre' => true,
'plaid' => false,
'quovo' => false,
'yodlee' => false,
@@ -75,21 +75,21 @@ return [
'prerequisites' => [
'fake' => FakePrerequisites::class,
'file' => false,
'bunq' => SpectrePrerequisites::class,
'spectre' => false,
'bunq' => false,
'spectre' => SpectrePrerequisites::class,
'plaid' => false,
'quovo' => false,
'yodlee' => false,
],
// some providers may have extra configuration per job.
'has_config' => [
// some providers may need extra configuration per job
'has_job_config' => [
'fake' => true,
'file' => true,
'bunq' => true,
'spectre' => true,
'plaid' => true,
'quovo' => true,
'yodlee' => true,
'bunq' => false,
'spectre' => false,
'plaid' => false,
'quovo' => false,
'yodlee' => false,
],
// if so, this is the class that handles it.
'configuration' => [

View File

@@ -68,6 +68,9 @@ return [
// prerequisites:
'prereq_fake_title' => 'Prerequisites for an import from the fake import provider',
'prereq_fake_text' => 'This fake provider requires a fake API key. It must be 32 characters long. You can use this one: 123456789012345678901234567890AA',
'prereq_spectre_title' => 'Prerequisites for an import using the Spectre API',
'prereq_spectre_text' => 'In order to import data using the Spectre API (v4), you must provide Firefly III with two secret values. They can be found on the <a href="https://www.saltedge.com/clients/profile/secrets">secrets page</a>.',
'prereq_spectre_pub' => 'Likewise, the Spectre API needs to know the public key you see below. Without it, it will not recognize you. Please enter this public key on your <a href="https://www.saltedge.com/clients/profile/secrets">secrets page</a>.',
// prerequisites success messages:
'prerequisites_saved_for_fake' => 'Fake API key stored successfully!',

View File

@@ -10,13 +10,13 @@
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">{{ trans('import.spectre_prerequisites_title') }}</h3>
<h3 class="box-title">{{ trans('import.prereq_spectre_title') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
{{ trans('import.spectre_prerequisites_text')|raw }}
{{ trans('import.prereq_spectre_text')|raw }}
</p>
</div>
</div>
@@ -29,7 +29,7 @@
</div>
<div class="row">
<div class="col-lg-8">
<p>{{ trans('import.spectre_enter_pub_key')|raw }}</p>
<p>{{ trans('import.prereq_spectre_pub')|raw }}</p>
<div class="form-group" id="pub_key_holder">
<label for="ffInput_pub_key_holder" class="col-sm-4 control-label">{{ trans('form.public_key') }}</label>