Code cleanup.

This commit is contained in:
James Cole
2018-07-20 14:34:56 +02:00
parent cfc2181a48
commit 44fb307da4
73 changed files with 1914 additions and 1423 deletions

View File

@@ -23,8 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Support\Binder;
use Carbon\Carbon;
use FireflyIII\Import\Prerequisites\PrerequisitesInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Routing\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Log;
/**
* Class ImportProvider.
@@ -40,10 +44,63 @@ class ImportProvider implements BinderInterface
*/
public static function routeBinder(string $value, Route $route): string
{
$providers = array_keys((array)config('import.enabled'));
$providers = array_keys(self::getProviders());
if (\in_array($value, $providers, true)) {
return $value;
}
throw new NotFoundHttpException;
}
/**
* @return array
*/
public static function getProviders(): array
{
$repository = app(UserRepositoryInterface::class);
// get and filter all import routines:
/** @var User $user */
$user = auth()->user();
/** @var array $config */
$providerNames = array_keys(config('import.enabled'));
$providers = [];
$isDemoUser = $repository->hasRole($user, 'demo');
$isDebug = (bool)config('app.debug');
foreach ($providerNames as $providerName) {
//Log::debug(sprintf('Now with provider %s', $providerName));
// only consider enabled providers
$enabled = (bool)config(sprintf('import.enabled.%s', $providerName));
$allowedForDemo = (bool)config(sprintf('import.allowed_for_demo.%s', $providerName));
$allowedForUser = (bool)config(sprintf('import.allowed_for_user.%s', $providerName));
if (false === $enabled) {
//Log::debug('Provider is not enabled. NEXT!');
continue;
}
if (true === $isDemoUser && false === $allowedForDemo) {
//Log::debug('User is demo and this provider is not allowed for demo user. NEXT!');
continue;
}
if (false === $isDemoUser && false === $allowedForUser && false === $isDebug) {
//Log::debug('User is not demo and this provider is not allowed for such users. NEXT!');
continue; // @codeCoverageIgnore
}
$providers[$providerName] = [
'has_prereq' => (bool)config('import.has_prereq.' . $providerName),
];
$class = (string)config(sprintf('import.prerequisites.%s', $providerName));
$result = false;
if ('' !== $class && class_exists($class)) {
//Log::debug('Will not check prerequisites.');
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser($user);
$result = $object->isComplete();
}
$providers[$providerName]['prereq_complete'] = $result;
}
Log::debug(sprintf('Enabled providers: %s', json_encode(array_keys($providers))));
return $providers;
}
}