Skip to content

Commit

Permalink
Displaying the error message belongs to the bunq-install script. (bun…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorijn authored and OGKevin committed Apr 7, 2018
1 parent f13be0f commit 7972a37
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 100 deletions.
16 changes: 11 additions & 5 deletions bin/bunq-install
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
179 changes: 84 additions & 95 deletions src/Util/InstallationUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,124 +58,113 @@ 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);
}
}

/**
* @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);
}
}

Expand Down

0 comments on commit 7972a37

Please sign in to comment.