2017-08-10 22:21:00 -05:00
|
|
|
<?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/>.
|
2017-08-10 22:21:00 -05:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-12-16 01:03:35 -06:00
|
|
|
namespace FireflyIII\Import\Prerequisites;
|
2017-08-10 22:21:00 -05:00
|
|
|
|
|
|
|
use FireflyIII\User;
|
2017-08-18 14:09:22 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Log;
|
2017-08-10 22:21:00 -05:00
|
|
|
use Preferences;
|
|
|
|
|
|
|
|
/**
|
2017-08-18 16:02:29 -05:00
|
|
|
* This class contains all the routines necessary to connect to Bunq.
|
2017-08-10 22:21:00 -05:00
|
|
|
*/
|
|
|
|
class BunqPrerequisites implements PrerequisitesInterface
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
/** @var User */
|
2017-08-10 22:21:00 -05:00
|
|
|
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.
|
2017-08-10 22:21:00 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getView(): string
|
|
|
|
{
|
2018-03-10 00:16:38 -06:00
|
|
|
Log::debug('Now in BunqPrerequisites::getView()');
|
|
|
|
|
2017-08-10 22:21:00 -05:00
|
|
|
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];
|
2017-08-10 22:21:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-08-10 22:21:00 -05:00
|
|
|
*
|
|
|
|
* @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)));
|
2017-08-10 22:21:00 -05:00
|
|
|
|
2018-03-10 00:16:38 -06:00
|
|
|
return $result;
|
2017-08-10 22:21:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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));
|
2017-08-10 22:21:00 -05:00
|
|
|
$this->user = $user;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-08-18 14:09:22 -05:00
|
|
|
|
|
|
|
/**
|
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).
|
|
|
|
*
|
2017-08-18 14:09:22 -05:00
|
|
|
* @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-08-18 14:09:22 -05:00
|
|
|
}
|
2017-12-22 11:32:43 -06:00
|
|
|
}
|