From 7972a3746455ea2c8598beec87954bb71a8d2cbb Mon Sep 17 00:00:00 2001 From: Jorijn Schrijvershof Date: Sat, 7 Apr 2018 15:29:52 +0200 Subject: [PATCH] Displaying the error message belongs to the bunq-install script. (bunq/sdk_php#133) --- bin/bunq-install | 16 ++- src/Util/InstallationUtil.php | 179 ++++++++++++++++------------------ 2 files changed, 95 insertions(+), 100 deletions(-) diff --git a/bin/bunq-install b/bin/bunq-install index 69a1597d..effe0ebf 100755 --- a/bin/bunq-install +++ b/bin/bunq-install @@ -15,12 +15,18 @@ if (file_exists($filenameAutoloadLocal)) { $allOption = getopt('', ['environment:', 'config-file:', 'api-key:']); -if (empty($allOption)) { - \bunq\Util\InstallationUtil::interactiveInstall(); -} else { - \bunq\Util\InstallationUtil::automaticInstall( +try { + if (empty($allOption)) { + \bunq\Util\InstallationUtil::interactiveInstall(); + } else { + \bunq\Util\InstallationUtil::automaticInstall( new \bunq\Util\BunqEnumApiEnvironmentType($allOption['environment']), $allOption['config-file'], $allOption['api-key'] ?? null - ); + ); + } +} catch (bunq\Exception\BunqException $exception) { + echo sprintf(\bunq\Util\InstallationUtil::ERROR_BUNQ_EXCEPTION, $exception->getMessage()); +} catch (Exception $exception) { + echo sprintf(\bunq\Util\InstallationUtil::ERROR_EXCEPTION, $exception->getMessage()); } diff --git a/src/Util/InstallationUtil.php b/src/Util/InstallationUtil.php index a03b22fa..404c79f8 100644 --- a/src/Util/InstallationUtil.php +++ b/src/Util/InstallationUtil.php @@ -58,59 +58,54 @@ final class InstallationUtil const PREG_MATCH_SUCCESS = 1; /** + * @throws BunqException */ public static function interactiveInstall() { - try { - $context = static::createApiContextWithoutConstructor(); - - $environmentType = new BunqEnumApiEnvironmentType(static::readLine( - self::PROMPT_ENVIRONMENT, - self::ERROR_EMPTY_ENVIRONMENT - )); - static::setPrivateProperty($context, self::PROPERTY_ENVIRONMENT_TYPE, $environmentType); - - $apiKey = static::readLine( - self::PROMPT_API_KEY, - self::ERROR_EMPTY_API_KEY - ); - static::setPrivateProperty($context, self::PROPERTY_API_KEY, $apiKey); - - $proxyUrl = static::readLineOrNull(self::PROMPT_PROXY_URL); - static::setPrivateProperty($context, self::PROPERTY_PROXY_URL, $proxyUrl); - - $methodInitializeInstallationContext = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_INITIALIZE_INSTALLATION_CONTEXT - ); - $methodInitializeInstallationContext->invoke($context); - - $description = static::readLine(self::PROMPT_DESCRIPTION, self::ERROR_EMPTY_DESCRIPTION); - $permittedIpsInput = static::readLineOrNull(self::PROMPT_PERMITTED_IPS); - $permittedIps = static::formatIps($permittedIpsInput); - $methodRegisterDevice = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_REGISTER_DEVICE - ); - $methodRegisterDevice->invoke($context, $description, $permittedIps); - - $methodInitializeSessionContext = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_INITIALIZE_SESSION_CONTEXT - ); - $methodInitializeSessionContext->invoke($context); - - $contextFileName = static::readLineOrNull(self::PROMPT_CONTEXT_FILE); - - if ($contextFileName === null) { - $context->save(); - } else { - $context->save($contextFileName); - } - } catch (BunqException $exception) { - echo sprintf(self::ERROR_BUNQ_EXCEPTION, $exception->getMessage()); - } catch (Exception $exception) { - echo sprintf(self::ERROR_EXCEPTION, $exception->getMessage()); + $context = static::createApiContextWithoutConstructor(); + + $environmentType = new BunqEnumApiEnvironmentType(static::readLine( + self::PROMPT_ENVIRONMENT, + self::ERROR_EMPTY_ENVIRONMENT + )); + static::setPrivateProperty($context, self::PROPERTY_ENVIRONMENT_TYPE, $environmentType); + + $apiKey = static::readLine( + self::PROMPT_API_KEY, + self::ERROR_EMPTY_API_KEY + ); + static::setPrivateProperty($context, self::PROPERTY_API_KEY, $apiKey); + + $proxyUrl = static::readLineOrNull(self::PROMPT_PROXY_URL); + static::setPrivateProperty($context, self::PROPERTY_PROXY_URL, $proxyUrl); + + $methodInitializeInstallationContext = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_INITIALIZE_INSTALLATION_CONTEXT + ); + $methodInitializeInstallationContext->invoke($context); + + $description = static::readLine(self::PROMPT_DESCRIPTION, self::ERROR_EMPTY_DESCRIPTION); + $permittedIpsInput = static::readLineOrNull(self::PROMPT_PERMITTED_IPS); + $permittedIps = static::formatIps($permittedIpsInput); + $methodRegisterDevice = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_REGISTER_DEVICE + ); + $methodRegisterDevice->invoke($context, $description, $permittedIps); + + $methodInitializeSessionContext = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_INITIALIZE_SESSION_CONTEXT + ); + $methodInitializeSessionContext->invoke($context); + + $contextFileName = static::readLineOrNull(self::PROMPT_CONTEXT_FILE); + + if ($contextFileName === null) { + $context->save(); + } else { + $context->save($contextFileName); } } @@ -118,64 +113,58 @@ public static function interactiveInstall() * @param BunqEnumApiEnvironmentType $environmentType * @param string $contextFileName * @param string|null $apiKey + * @throws BunqException */ public static function automaticInstall( BunqEnumApiEnvironmentType $environmentType, string $contextFileName, string $apiKey = null ) { - try { - $context = static::createApiContextWithoutConstructor(); + $context = static::createApiContextWithoutConstructor(); - if (is_null($environmentType)) { - $environmentType = BunqEnumApiEnvironmentType::SANDBOX(); - } else { - // Environment already passed - } - - static::setPrivateProperty($context, self::PROPERTY_ENVIRONMENT_TYPE, $environmentType); - - if ($environmentType->equals(BunqEnumApiEnvironmentType::SANDBOX()) && is_null($apiKey)) { - $methodCreateSandboxUser = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_CREATE_SANDBOX_USER - ); - - $methodCreateSandboxUser->invoke($context); - } elseif (!is_null($apiKey)) { - static::setPrivateProperty($context, self::PROPERTY_API_KEY, $apiKey); - } else { - throw new BunqException(self::ERROR_CANNOT_CREATE_API_KEY_PRODUCTION); - } + if (is_null($environmentType)) { + $environmentType = BunqEnumApiEnvironmentType::SANDBOX(); + } else { + // Environment already passed + } - $methodInitializeInstallationContext = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_INITIALIZE_INSTALLATION_CONTEXT - ); - $methodInitializeInstallationContext->invoke($context); + static::setPrivateProperty($context, self::PROPERTY_ENVIRONMENT_TYPE, $environmentType); - $methodRegisterDevice = static::createAccessibleReflectionMethod( + if ($environmentType->equals(BunqEnumApiEnvironmentType::SANDBOX()) && is_null($apiKey)) { + $methodCreateSandboxUser = static::createAccessibleReflectionMethod( ApiContext::class, - self::METHOD_REGISTER_DEVICE + self::METHOD_CREATE_SANDBOX_USER ); - $methodRegisterDevice->invoke($context, gethostname(), []); - $methodInitializeSessionContext = static::createAccessibleReflectionMethod( - ApiContext::class, - self::METHOD_INITIALIZE_SESSION_CONTEXT - ); - $methodInitializeSessionContext->invoke($context); + $methodCreateSandboxUser->invoke($context); + } elseif (!is_null($apiKey)) { + static::setPrivateProperty($context, self::PROPERTY_API_KEY, $apiKey); + } else { + throw new BunqException(self::ERROR_CANNOT_CREATE_API_KEY_PRODUCTION); + } - if ($contextFileName === null) { - $context->save(); - } else { - $context->save($contextFileName); - } - } catch (BunqException $exception) { - echo sprintf(self::ERROR_BUNQ_EXCEPTION, $exception->getMessage()); - var_dump($exception); - } catch (Exception $exception) { - echo sprintf(self::ERROR_EXCEPTION, $exception->getMessage()); + $methodInitializeInstallationContext = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_INITIALIZE_INSTALLATION_CONTEXT + ); + $methodInitializeInstallationContext->invoke($context); + + $methodRegisterDevice = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_REGISTER_DEVICE + ); + $methodRegisterDevice->invoke($context, gethostname(), []); + + $methodInitializeSessionContext = static::createAccessibleReflectionMethod( + ApiContext::class, + self::METHOD_INITIALIZE_SESSION_CONTEXT + ); + $methodInitializeSessionContext->invoke($context); + + if ($contextFileName === null) { + $context->save(); + } else { + $context->save($contextFileName); } }