Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions src/Common/Error/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function header(string $name): string
*/
private function linkIsValid(string $link): bool
{
$link = $this->docDomain.$link;
$link = $this->docDomain . $link;

try {
return $this->client->request('HEAD', $link)->getStatusCode() < 400;
Expand All @@ -63,22 +63,56 @@ private function linkIsValid(string $link): bool
}
}

/**
* Reads the PHP memory limit from the ini file and then converts it into an integer, handling PHP's shorthand byte values.
*
* @see https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
*
* @return int
*/
private function memoryLimitToInt(): int
{
$megabyte = 1048576;
$limit = ini_get('memory_limit');

if (is_numeric($limit)) {
return intval($limit);
}

switch (strtolower(substr($limit, -1))) {
case 'g':
return intval(substr($limit, 0, -1)) * $megabyte * 1024;
break;

case 'm':
return intval(substr($limit, 0, -1)) * $megabyte;
break;

case 'k':
return intval(substr($limit, 0, -1)) * ($megabyte / 1024);
break;

default:
return 0;
}
}

/**
* @codeCoverageIgnore
*/
public function str(MessageInterface $message, int $verbosity = 0): string
{
if ($message instanceof RequestInterface) {
$msg = trim($message->getMethod().' '.$message->getRequestTarget());
$msg .= ' HTTP/'.$message->getProtocolVersion();
$msg = trim($message->getMethod() . ' ' . $message->getRequestTarget());
$msg .= ' HTTP/' . $message->getProtocolVersion();
if (!$message->hasHeader('host')) {
$msg .= "\r\nHost: ".$message->getUri()->getHost();
$msg .= "\r\nHost: " . $message->getUri()->getHost();
}
} else {
if ($message instanceof ResponseInterface) {
$msg = 'HTTP/'.$message->getProtocolVersion().' '
.$message->getStatusCode().' '
.$message->getReasonPhrase();
$msg = 'HTTP/' . $message->getProtocolVersion() . ' '
. $message->getStatusCode() . ' '
. $message->getReasonPhrase();
} else {
throw new \InvalidArgumentException('Unknown message type');
}
Expand All @@ -89,15 +123,15 @@ public function str(MessageInterface $message, int $verbosity = 0): string
}

foreach ($message->getHeaders() as $name => $values) {
$msg .= "\r\n{$name}: ".implode(', ', $values);
$msg .= "\r\n{$name}: " . implode(', ', $values);
}

if ($verbosity < 2) {
return $msg;
}

if (ini_get('memory_limit') < 0 || $message->getBody()->getSize() < ini_get('memory_limit')) {
$msg .= "\r\n\r\n".$message->getBody();
if ($this->memoryLimitToInt() < 0 || $message->getBody()->getSize() < $this->memoryLimitToInt()) {
$msg .= "\r\n\r\n" . $message->getBody();
}

return trim($msg);
Expand All @@ -120,16 +154,16 @@ public function httpError(RequestInterface $request, ResponseInterface $response
);

$message .= $this->header('Request');
$message .= $this->str($request, $verbosity).PHP_EOL.PHP_EOL;
$message .= $this->str($request, $verbosity) . PHP_EOL . PHP_EOL;

$message .= $this->header('Response');
$message .= $this->str($response, $verbosity).PHP_EOL.PHP_EOL;
$message .= $this->str($response, $verbosity) . PHP_EOL . PHP_EOL;

$message .= $this->header('Further information');
$message .= $this->getStatusCodeMessage($response->getStatusCode());

$message .= 'Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging '
.'HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues.';
. 'HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues.';

$e = new BadResponseError($message);
$e->setRequest($request);
Expand Down Expand Up @@ -170,7 +204,7 @@ public function userInputError(string $expectedType, $userValue, string $further
$message .= 'Please ensure that the value adheres to the expectation above. ';

if ($furtherLink && $this->linkIsValid($furtherLink)) {
$message .= sprintf('Visit %s for more information about input arguments. ', $this->docDomain.$furtherLink);
$message .= sprintf('Visit %s for more information about input arguments. ', $this->docDomain . $furtherLink);
}

$message .= 'If you run into trouble, please open a support issue on https://github.com/php-opencloud/openstack/issues.';
Expand Down