. */ declare(strict_types=1); namespace FireflyIII\Import\Prerequisites; use FireflyIII\User; use Illuminate\Http\Request; use Illuminate\Support\MessageBag; /** * This class contains all the routines necessary for the fake import provider. * * Class FakePrerequisites */ class FakePrerequisites implements PrerequisitesInterface { /** @var User */ private $user; /** * Returns view name that allows user to fill in prerequisites. Currently asks for the API key. * * @codeCoverageIgnore * @return string */ public function getView(): string { return 'import.fake.prerequisites'; } /** * Returns any values required for the prerequisites-view. * * @return array */ public function getViewParameters(): array { $apiKey = ''; if ($this->hasApiKey()) { $apiKey = app('preferences')->getForUser($this->user, 'fake_api_key', null)->data; } $oldKey = (string)\request()->old('api_key'); if ($oldKey !== '') { $apiKey = \request()->old('api_key'); // @codeCoverageIgnore } return ['api_key' => $apiKey]; } /** * Indicate if all prerequisites have been met. * * @return bool */ public function isComplete(): bool { return $this->hasApiKey(); } /** * Set the user for this Prerequisites-routine. Class is expected to implement and save this. * * @param User $user */ public function setUser(User $user): void { $this->user = $user; } /** * @param array $data * * @return MessageBag */ public function storePrerequisites(array $data): MessageBag { $apiKey = $data['api_key'] ?? ''; $messageBag = new MessageBag(); if (32 !== \strlen($apiKey)) { $messageBag->add('api_key', 'API key must be 32 chars.'); return $messageBag; } app('preferences')->setForUser($this->user, 'fake_api_key', $apiKey); return $messageBag; } /** * @return bool */ private function hasApiKey(): bool { $apiKey = app('preferences')->getForUser($this->user, 'fake_api_key', false); if (null === $apiKey) { return false; } if (null === $apiKey->data) { return false; } if (\strlen((string)$apiKey->data) === 32) { return true; } return false; } }