Skip to content

Commit

Permalink
Throw the exception instead of catching it during auto install. (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Apr 6, 2018
1 parent 55d197f commit 776a39d
Showing 1 changed file with 60 additions and 69 deletions.
129 changes: 60 additions & 69 deletions src/Util/InstallationUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,75 +114,6 @@ public static function interactiveInstall()
}
}

/**
* @param BunqEnumApiEnvironmentType $environmentType
* @param string|null $contextFileName
* @param string|null $apiKey
* @param bool $saveToConfFile
*
* @return ApiContext
*/
public static function automaticInstall(
BunqEnumApiEnvironmentType $environmentType,
string $contextFileName = null,
string $apiKey = null,
bool $saveToConfFile = false
): ApiContext {
try {
$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);
}

$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 && $saveToConfFile) {
$context->save($contextFileName);
}

return $context;
} catch (BunqException $exception) {
echo sprintf(self::ERROR_BUNQ_EXCEPTION, $exception->getMessage());
var_dump($exception);
} catch (Exception $exception) {
echo sprintf(self::ERROR_EXCEPTION, $exception->getMessage());
}
}

/**
* @return ApiContext
*/
Expand Down Expand Up @@ -291,4 +222,64 @@ private static function assertIpIsValid(string $ip)
throw new BunqException(self::ERROR_INVALID_IP_ADDRESS, [$ip]);
}
}

/**
* @param BunqEnumApiEnvironmentType $environmentType
* @param string|null $contextFileName
* @param string|null $apiKey
* @param bool $saveToConfFile
*
* @return ApiContext
*/
public static function automaticInstall(
BunqEnumApiEnvironmentType $environmentType,
string $contextFileName = null,
string $apiKey = null,
bool $saveToConfFile = false
): ApiContext {
$context = static::createApiContextWithoutConstructor();

if (!is_null($environmentType)) {
$environmentType = BunqEnumApiEnvironmentType::SANDBOX();
}

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);
}

$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 && $saveToConfFile) {
$context->save($contextFileName);
}

return $context;
}
}

0 comments on commit 776a39d

Please sign in to comment.