firefly-iii/app/Import/Prerequisites/BunqPrerequisites.php

118 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* BunqPrerequisites.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Prerequisites;
use FireflyIII\User;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Log;
use Preferences;
/**
2017-08-18 16:02:29 -05:00
* This class contains all the routines necessary to connect to Bunq.
*/
class BunqPrerequisites implements PrerequisitesInterface
{
2017-11-15 05:25:49 -06:00
/** @var User */
private $user;
/**
2017-08-18 16:02:29 -05:00
* Returns view name that allows user to fill in prerequisites. Currently asks for the API key.
*
* @return string
*/
public function getView(): string
{
2018-03-10 00:16:38 -06:00
Log::debug('Now in BunqPrerequisites::getView()');
return 'import.bunq.prerequisites';
}
/**
* Returns any values required for the prerequisites-view.
*
* @return array
*/
public function getViewParameters(): array
{
2018-03-10 00:16:38 -06:00
Log::debug('Now in BunqPrerequisites::getViewParameters()');
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', null);
$string = '';
if (!is_null($apiKey)) {
$string = $apiKey->data;
}
return ['key' => $string];
}
/**
* Returns if this import method has any special prerequisites such as config
2017-08-18 16:02:29 -05:00
* variables or other things. The only thing we verify is the presence of the API key. Everything else
* tumbles into place: no installation token? Will be requested. No device server? Will be created. Etc.
*
* @return bool
*/
public function hasPrerequisites(): bool
{
2018-03-10 00:16:38 -06:00
Log::debug('Now in BunqPrerequisites::hasPrerequisites()');
2018-03-10 13:25:42 -06:00
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', false);
$result = (false === $apiKey->data || null === $apiKey->data);
2018-03-10 00:16:38 -06:00
Log::debug(sprintf('Is apiKey->data false? %s', var_export(false === $apiKey->data, true)));
Log::debug(sprintf('Is apiKey->data NULL? %s', var_export(null === $apiKey->data, true)));
Log::debug(sprintf('Result is: %s', var_export($result, true)));
2018-03-10 00:16:38 -06:00
return $result;
}
/**
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
*
* @param User $user
*/
public function setUser(User $user): void
{
2018-03-10 00:16:38 -06:00
Log::debug(sprintf('Now in setUser(#%d)', $user->id));
$this->user = $user;
return;
}
/**
2017-08-18 16:02:29 -05:00
* This method responds to the user's submission of an API key. It tries to register this instance as a new Firefly III device.
* If this fails, the error is returned in a message bag and the user is notified (this is fairly friendly).
*
* @param Request $request
*
* @return MessageBag
*/
public function storePrerequisites(Request $request): MessageBag
{
$apiKey = $request->get('api_key');
Log::debug('Storing bunq API key');
Preferences::setForUser($this->user, 'bunq_api_key', $apiKey);
2018-03-10 13:25:42 -06:00
return new MessageBag;
}
2017-12-22 11:32:43 -06:00
}