Use non-static class for ApiContext to improve testability.

This commit is contained in:
James Cole 2018-05-25 06:26:37 +02:00
parent 217ca98933
commit 70110208fc
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 84 additions and 19 deletions

View File

@ -22,11 +22,10 @@ declare(strict_types=1);
namespace FireflyIII\Import\Prerequisites;
use bunq\Context\ApiContext;
use bunq\Exception\BadRequestException;
use bunq\Exception\BunqException;
use bunq\Util\BunqEnumApiEnvironmentType;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Services\Bunq\ApiContext;
use FireflyIII\Services\IP\IPRetrievalInterface;
use FireflyIII\User;
use Illuminate\Support\MessageBag;
@ -41,6 +40,7 @@ class BunqPrerequisites implements PrerequisitesInterface
private $user;
/**
* @codeCoverageIgnore
* Returns view name that allows user to fill in prerequisites.
*
* @return string
@ -86,6 +86,7 @@ class BunqPrerequisites implements PrerequisitesInterface
}
/**
* @codeCoverageIgnore
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
*
* @param User $user
@ -117,34 +118,27 @@ class BunqPrerequisites implements PrerequisitesInterface
$permittedIps = [$externalIP];
try {
$apiContext = ApiContext::create(
$environment,
$apiKey,
$deviceDescription,
$permittedIps
);
} catch (BadRequestException|BunqException|Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$message = $e->getMessage();
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';
}
/** @var ApiContext $object */
$object = app(ApiContext::class);
$apiContext = $object->create($environment, $apiKey, $deviceDescription, $permittedIps);
} catch (FireflyException $e) {
$messages = new MessageBag();
$messages->add('bunq_error', $message);
$messages->add('bunq_error', $e->getMessage());
return $messages;
}
// store context in JSON:
try {
$json = $apiContext->toJson();
// @codeCoverageIgnoreStart
} catch (BunqException $e) {
$messages = new MessageBag();
$messages->add('bunq_error', $e->getMessage());
return $messages;
}
// @codeCoverageIgnoreEnd
// and store for user:
app('preferences')->setForUser($this->user, 'bunq_api_context', $json);
@ -152,6 +146,7 @@ class BunqPrerequisites implements PrerequisitesInterface
}
/**
* @codeCoverageIgnore
* @return BunqEnumApiEnvironmentType
*/
private function getBunqEnvironment(): BunqEnumApiEnvironmentType

View File

@ -46,7 +46,6 @@ class BunqRoutine implements RoutineInterface
*
* The final status of the routine must be "provider_finished".
*
* @return bool
* @throws FireflyException
*/
public function run(): void
@ -86,6 +85,7 @@ class BunqRoutine implements RoutineInterface
break;
}
}
throw new FireflyException(sprintf('bunq import routine cannot handle status "%s"', $this->importJob->status)); // @codeCoverageIgnore
}

View File

@ -0,0 +1,70 @@
<?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;
use bunq\Exception\BadRequestException;
use bunq\Exception\BunqException;
use bunq\Util\BunqEnumApiEnvironmentType;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use Log;
use stdClass;
/**
* Special class to hide away bunq's static initialisation methods.
*
* Class ApiContext
*/
class ApiContext
{
/**
* @param BunqEnumApiEnvironmentType $environmentType
* @param string $apiKey
* @param string $description
* @param array $permittedIps
* @param string|null $proxyUrl
*
* @throws FireflyException
* @return BunqApiContext
*/
public function create(BunqEnumApiEnvironmentType $environmentType, string $apiKey, string $description, array $permittedIps, string $proxyUrl = null
) {
$permittedIps = $permittedIps ?? [];
try {
$context = BunqApiContext::create($environmentType, $apiKey, $description, $permittedIps, $proxyUrl);
} catch (BunqException|BadRequestException|Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$message = $e->getMessage();
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;
}
}