diff --git a/app/Services/Bunq/ApiContext.php b/app/Services/Bunq/ApiContext.php index 758af9fcd6..b657fcf18f 100644 --- a/app/Services/Bunq/ApiContext.php +++ b/app/Services/Bunq/ApiContext.php @@ -24,13 +24,13 @@ declare(strict_types=1); namespace FireflyIII\Services\Bunq; use bunq\Context\ApiContext as BunqApiContext; +use bunq\Context\BunqContext; 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. @@ -55,9 +55,10 @@ class ApiContext 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(); + 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'; } @@ -65,6 +66,27 @@ class ApiContext } return $context; + } + /** + * @throws FireflyException + * + * @param string $jsonString + */ + public function fromJson(string $jsonString): void + { + try { + $apiContext = BunqApiContext::fromJson($jsonString); + BunqContext::loadApiContext($apiContext); + } catch (BadRequestException|BunqException|Exception $e) { + $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); + } } } \ No newline at end of file diff --git a/app/Services/Bunq/MonetaryAccount.php b/app/Services/Bunq/MonetaryAccount.php new file mode 100644 index 0000000000..bb77c54a80 --- /dev/null +++ b/app/Services/Bunq/MonetaryAccount.php @@ -0,0 +1,57 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Services\Bunq; + +use bunq\Exception\BunqException; +use bunq\Model\Generated\Endpoint\BunqResponseMonetaryAccountList; +use bunq\Model\Generated\Endpoint\MonetaryAccount as BunqMonetaryAccount; +use Exception; +use FireflyIII\Exceptions\FireflyException; + +/** + * Class MonetaryAccount + */ +class MonetaryAccount +{ + /** + * @param array $params + * @param array $customHeaders + * + * @return BunqResponseMonetaryAccountList + * @throws FireflyException + */ + public function listing(array $params = null, array $customHeaders = null): BunqResponseMonetaryAccountList + { + $params = $params ?? []; + $customHeaders = $customHeaders ?? []; + try { + $result = BunqMonetaryAccount::listing($params, $customHeaders); + } catch (BunqException|Exception $e) { + throw new FireflyException($e->getMessage()); + } + + return $result; + } + +} \ No newline at end of file diff --git a/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php b/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php index 955800fa1c..247a7a2916 100644 --- a/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php +++ b/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php @@ -81,7 +81,7 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface $mapping = $data['account_mapping'] ?? []; $final = []; if (\count($accounts) === 0) { - throw new FireflyException('No bunq accounts found. Import cannot continue.'); + throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore } if (\count($mapping) === 0) { $messages = new MessageBag; @@ -115,7 +115,7 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface $config = $this->repository->getConfiguration($this->importJob); $accounts = $config['accounts'] ?? []; if (\count($accounts) === 0) { - throw new FireflyException('No bunq accounts found. Import cannot continue.'); + throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore } // list the users accounts: $collection = $this->accountRepository->getAccountsByType([AccountType::ASSET]); @@ -140,6 +140,8 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface } /** + * @codeCoverageIgnore + * * Get the view for this stage. * * @return string diff --git a/app/Support/Import/Routine/Bunq/StageNewHandler.php b/app/Support/Import/Routine/Bunq/StageNewHandler.php index e83a8db7e7..7ec9e2248c 100644 --- a/app/Support/Import/Routine/Bunq/StageNewHandler.php +++ b/app/Support/Import/Routine/Bunq/StageNewHandler.php @@ -23,19 +23,15 @@ declare(strict_types=1); namespace FireflyIII\Support\Import\Routine\Bunq; -use bunq\Context\ApiContext; -use bunq\Context\BunqContext; -use bunq\Exception\BadRequestException; -use bunq\Exception\BunqException; -use bunq\Model\Generated\Endpoint\MonetaryAccount; +use bunq\Model\Generated\Endpoint\MonetaryAccount as BunqMonetaryAccount; use bunq\Model\Generated\Endpoint\MonetaryAccountBank; use bunq\Model\Generated\Object\Pointer; -use Exception; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\ImportJob; use FireflyIII\Models\Preference; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; -use Log; +use FireflyIII\Services\Bunq\ApiContext; +use FireflyIII\Services\Bunq\MonetaryAccount; /** * Class StageNewHandler @@ -56,19 +52,9 @@ class StageNewHandler $preference = app('preferences')->getForUser($this->importJob->user, 'bunq_api_context', null); if (null !== $preference && '' !== (string)$preference->data) { // restore API context - try { - $apiContext = ApiContext::fromJson($preference->data); - BunqContext::loadApiContext($apiContext); - } 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'; - - } - throw new FireflyException($message); - } + /** @var ApiContext $apiContext */ + $apiContext = app(ApiContext::class); + $apiContext->fromJson($preference->data); // list bunq accounts: $accounts = $this->listAccounts(); @@ -97,13 +83,17 @@ class StageNewHandler /** * @return array + * @throws FireflyException */ private function listAccounts(): array { - $accounts = []; - $monetaryAccountList = MonetaryAccount::listing(); - /** @var MonetaryAccount $monetaryAccount */ - foreach ($monetaryAccountList->getValue() as $monetaryAccount) { + $accounts = []; + /** @var MonetaryAccount $lister */ + $lister = app(MonetaryAccount::class); + $result = $lister->listing(); + + /** @var BunqMonetaryAccount $monetaryAccount */ + foreach ($result->getValue() as $monetaryAccount) { $mab = $monetaryAccount->getMonetaryAccountBank(); $array = $this->processMab($mab); $accounts[] = $array; diff --git a/resources/views/import/spectre/prerequisites.twig b/resources/views/import/spectre/prerequisites.twig index 73b0e89824..1a1909bc0a 100644 --- a/resources/views/import/spectre/prerequisites.twig +++ b/resources/views/import/spectre/prerequisites.twig @@ -5,7 +5,7 @@ {% endblock %} {% block content %}