firefly-iii/app/Services/Bunq/ApiContext.php

95 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
* ApiContext.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Bunq;
use bunq\Context\ApiContext as BunqApiContext;
2018-05-25 01:38:15 -05:00
use bunq\Context\BunqContext;
use bunq\Util\BunqEnumApiEnvironmentType;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use Log;
2018-07-26 21:46:21 -05:00
use Tests\Object\FakeApiContext;
/**
* Special class to hide away bunq's static initialisation methods.
*
* Class ApiContext
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*/
class ApiContext
{
/**
* @param BunqEnumApiEnvironmentType $environmentType
* @param string $apiKey
* @param string $description
* @param array $permittedIps
* @param string|null $proxyUrl
*
2018-07-26 21:46:21 -05:00
* @return BunqApiContext|FakeApiContext
2019-08-17 03:47:29 -05:00
*
*@throws FireflyException
*/
public function create(BunqEnumApiEnvironmentType $environmentType, string $apiKey, string $description, array $permittedIps, string $proxyUrl = null
2018-07-26 21:46:21 -05:00
) {
$permittedIps = $permittedIps ?? [];
try {
$context = BunqApiContext::create($environmentType, $apiKey, $description, $permittedIps, $proxyUrl);
2018-07-25 23:27:52 -05:00
} catch (Exception $e) {
$message = $e->getMessage();
2018-05-25 01:38:15 -05:00
Log::error($message);
Log::error($e->getTraceAsString());
if (stripos($message, 'Generating a new private key failed')) {
$message = 'Could not generate key-material. Please make sure OpenSSL is installed and configured: http://bit.ly/FF3-openSSL';
}
throw new FireflyException($message);
}
return $context;
2018-05-25 01:38:15 -05:00
}
2018-05-25 01:38:15 -05:00
/**
* @throws FireflyException
*
* @param string $jsonString
*/
public function fromJson(string $jsonString): void
{
try {
$apiContext = BunqApiContext::fromJson($jsonString);
BunqContext::loadApiContext($apiContext);
2018-07-25 23:27:52 -05:00
} catch (Exception $e) {
2018-05-25 01:38:15 -05:00
$message = $e->getMessage();
Log::error($message);
Log::error($e->getTraceAsString());
if (stripos($message, 'Generating a new private key failed')) {
$message = 'Could not generate key-material. Please make sure OpenSSL is installed and configured: http://bit.ly/FF3-openSSL';
}
throw new FireflyException($message);
}
}
}