Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Use non-static class for ApiContext to improve testability.
James Cole70110208fc5e
diff --git a/app/Import/Prerequisites/BunqPrerequisites.php b/app/Import/Prerequisites/BunqPrerequisites.phpindex 82c3918eeb..ea39453f3a 100644--- a/app/Import/Prerequisites/BunqPrerequisites.php+++ b/app/Import/Prerequisites/BunqPrerequisites.php@@ -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 PrerequisitesInterfaceprivate $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(): BunqEnumApiEnvironmentTypediff --git a/app/Import/Routine/BunqRoutine.php b/app/Import/Routine/BunqRoutine.phpindex 3f3994afc0..fe58acd99b 100644--- a/app/Import/Routine/BunqRoutine.php+++ b/app/Import/Routine/BunqRoutine.php@@ -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 RoutineInterfacebreak;}}+ throw new FireflyException(sprintf('bunq import routine cannot handle status "%s"', $this->importJob->status)); // @codeCoverageIgnore}diff --git a/app/Services/Bunq/ApiContext.php b/app/Services/Bunq/ApiContext.phpnew file mode 100644index 0000000000..758af9fcd6--- /dev/null+++ b/app/Services/Bunq/ApiContext.php@@ -0,0 +1,70 @@+<?php+/**+ * ApiContext.php+ * Copyright (c) 2018 [email protected]+ *+ * 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;++ }+}\ No newline at end of file