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

214 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
* BunqPrerequisites.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Prerequisites;
2018-05-23 12:36:12 +02:00
use bunq\Exception\BunqException;
use bunq\Util\BunqEnumApiEnvironmentType;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Services\Bunq\ApiContext;
use FireflyIII\Services\IP\IPRetrievalInterface;
use FireflyIII\User;
use Illuminate\Support\MessageBag;
use Log;
/**
2017-08-18 23:02:29 +02:00
* This class contains all the routines necessary to connect to Bunq.
*/
class BunqPrerequisites implements PrerequisitesInterface
{
2018-05-05 16:51:32 +02:00
/** @var User */
private $user;
2018-05-23 12:36:12 +02:00
/**
* @codeCoverageIgnore
* Returns view name that allows user to fill in prerequisites.
*
* @return string
*/
public function getView(): string
{
2018-05-23 12:36:12 +02:00
return 'import.bunq.prerequisites';
}
/**
* Returns any values required for the prerequisites-view.
*
* @return array
*/
public function getViewParameters(): array
{
2018-05-23 12:36:12 +02:00
Log::debug('Now in BunqPrerequisites::getViewParameters()');
$key = '';
$externalIP = '';
if ($this->hasApiKey()) {
$key = app('preferences')->getForUser($this->user, 'bunq_api_key', null)->data;
}
if ($this->hasExternalIP()) {
$externalIP = app('preferences')->getForUser($this->user, 'bunq_external_ip', null)->data;
}
if (!$this->hasExternalIP()) {
/** @var IPRetrievalInterface $service */
$service = app(IPRetrievalInterface::class);
$externalIP = (string)$service->getIP();
}
return ['api_key' => $key, 'external_ip' => $externalIP];
}
2018-04-29 18:06:47 +02:00
/**
* Indicate if all prerequisites have been met.
*
* @return bool
*/
public function isComplete(): bool
{
2018-05-23 12:36:12 +02:00
return $this->hasApiKey() && $this->hasExternalIP() && $this->hasApiContext();
2018-04-29 18:06:47 +02:00
}
/**
* @codeCoverageIgnore
* 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-05-05 16:51:32 +02:00
$this->user = $user;
}
/**
* This method responds to the user's submission of an API key. Should do nothing but store the value.
*
* Errors must be returned in the message bag under the field name they are requested by.
2017-08-18 23:02:29 +02:00
*
* @param array $data
*
* @return MessageBag
*/
public function storePrerequisites(array $data): MessageBag
{
2018-05-23 12:36:12 +02:00
$apiKey = $data['api_key'] ?? '';
$externalIP = $data['external_ip'] ?? '';
Log::debug('Storing bunq API key');
app('preferences')->setForUser($this->user, 'bunq_api_key', $apiKey);
app('preferences')->setForUser($this->user, 'bunq_external_ip', $externalIP);
$environment = $this->getBunqEnvironment();
$deviceDescription = 'Firefly III v' . config('firefly.version');
$permittedIps = [$externalIP];
2018-07-03 19:34:13 +02:00
Log::debug(sprintf('Environment for bunq is %s', $environment->getChoiceString()));
2018-05-23 12:36:12 +02:00
try {
/** @var ApiContext $object */
2018-06-06 21:23:00 +02:00
$object = app(ApiContext::class);
$apiContext = $object->create($environment, $apiKey, $deviceDescription, $permittedIps);
} catch (FireflyException $e) {
2018-05-23 12:36:12 +02:00
$messages = new MessageBag();
$messages->add('bunq_error', $e->getMessage());
2018-05-23 12:36:12 +02:00
return $messages;
}
// store context in JSON:
try {
$json = $apiContext->toJson();
// @codeCoverageIgnoreStart
2018-05-23 12:36:12 +02:00
} catch (BunqException $e) {
$messages = new MessageBag();
$messages->add('bunq_error', $e->getMessage());
return $messages;
}
// @codeCoverageIgnoreEnd
2018-05-23 12:36:12 +02:00
// and store for user:
app('preferences')->setForUser($this->user, 'bunq_api_context', $json);
2018-05-05 16:51:32 +02:00
return new MessageBag;
}
2018-05-23 12:36:12 +02:00
/**
* @codeCoverageIgnore
2018-05-23 12:36:12 +02:00
* @return BunqEnumApiEnvironmentType
*/
private function getBunqEnvironment(): BunqEnumApiEnvironmentType
{
$env = env('BUNQ_USE_SANDBOX');
if (null === $env) {
return BunqEnumApiEnvironmentType::PRODUCTION();
}
if (false === $env) {
return BunqEnumApiEnvironmentType::PRODUCTION();
}
return BunqEnumApiEnvironmentType::SANDBOX();
}
/**
* @return bool
*/
private function hasApiContext(): bool
{
$apiContext = app('preferences')->getForUser($this->user, 'bunq_api_context', null);
if (null === $apiContext) {
return false;
}
if ('' === (string)$apiContext->data) {
return false;
}
return true;
}
/**
* @return bool
*/
private function hasApiKey(): bool
{
$apiKey = app('preferences')->getForUser($this->user, 'bunq_api_key', null);
if (null === $apiKey) {
return false;
}
if ('' === (string)$apiKey->data) {
return false;
}
return true;
}
/**
* @return bool
*/
private function hasExternalIP(): bool
{
$externalIP = app('preferences')->getForUser($this->user, 'bunq_external_ip', null);
if (null === $externalIP) {
return false;
}
if ('' === (string)$externalIP->data) {
return false;
}
return true;
}
2017-12-22 18:32:43 +01:00
}