Skip to content

Commit

Permalink
Merge branch 'develop' into move-to-new-sandbox-bunq/sdk_php#149
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin authored May 29, 2018
2 parents e3f0769 + 9d9ea46 commit e9f50fd
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ class ApiClient
/**
* Public key locations.
*/
const FILE_PUBLIC_KEY_ENVIRONMENT_SANDBOX = '/Certificate/sandbox.public.api.bunq.com.pubkey.pem';
const FILE_PUBLIC_KEY_ENVIRONMENT_PRODUCTION = '/Certificate/api.bunq.com.pubkey.pem';

/**
* String format constants.
*/
const FORMAT_CURL_INSTALLATION_INSTRUCTIONS =
'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s';
const FORMAT_ERROR_MESSAGE_MAC_CURL = '%s%s%s';

/**
* Body constants.
*/
Expand Down Expand Up @@ -133,6 +139,12 @@ class ApiClient
*/
const COMMAND_DETERMINE_BREW_PHP_VERSION = 'brew list | egrep -e "^php[0-9]{2}$"';

/**
* Curl error regex constants.
*/
const REGEX_CURL_ERROR_CODE = '/(cURL error )(?P<errorCode>\d+)/';
const REGEX_NAMED_GOUP_ERROR_CODE = 'errorCode';

/**
* @var Client
*/
Expand Down Expand Up @@ -195,6 +207,7 @@ public function get(string $uri, array $params, array $customHeaders): BunqRespo
* @param string[] $customHeaders
*
* @return BunqResponseRaw
* @throws BunqException
*/
private function request(
string $method,
Expand All @@ -212,8 +225,8 @@ private function request(
$this->determineRequestOptions($body, $customHeaders)
);
} catch (RequestException $exception) {
if ($exception->getCode() === self::ERROR_CODE_MAC_OS_CURL_BUG && $this->isMacOs()) {
die(vsprintf(self::ERROR_MAC_OS_CURL_VERSION, [$this->determineVersionPhpMacOs(), PHP_EOL]));
if ($this->isCurlErrorCodeZero($exception) && $this->isMacOs()) {
throw new BunqException($this->determineErrorMessageCurlZero());
} else {
throw $exception;
}
Expand Down Expand Up @@ -394,6 +407,21 @@ protected function determineBodyString($body): string
return $bodyString;
}

/**
* @param RequestException $exception
*
* @return bool
*/
private function isCurlErrorCodeZero(RequestException $exception): bool
{
$allMatch = [];

preg_match(self::REGEX_CURL_ERROR_CODE, $exception->getMessage(), $allMatch);

return isset($allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE])
&& $allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE] === self::ERROR_CODE_MAC_OS_CURL_BUG;
}

/**
* @return bool
*/
Expand All @@ -402,6 +430,20 @@ private function isMacOs(): bool
return posix_uname()[self::INDEX_UNAME_SYSNAME] === self::SYSNAME_MAC_OS;
}

/**
* @return string
*/
private function determineErrorMessageCurlZero(): string
{
return vsprintf(
vsprintf(
self::FORMAT_ERROR_MESSAGE_MAC_CURL,
[self::ERROR_MAC_OS_CURL_VERSION, PHP_EOL, self::FORMAT_CURL_INSTALLATION_INSTRUCTIONS]
),
[$this->determineVersionPhpMacOs(), PHP_EOL]
);
}

/**
* @return string
*/
Expand Down

0 comments on commit e9f50fd

Please sign in to comment.