summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Error/Builder.php
diff options
context:
space:
mode:
authorEole <EoleDev@outlook.fr>2016-04-27 16:42:28 +0200
committerEole <EoleDev@outlook.fr>2016-04-27 16:42:28 +0200
commit49f416dc5061032e0514ea0cfeceaca37d13e432 (patch)
tree1202ac2a6fa860b8929afdc886c94fc50bd0a1de /server/vendor/php-opencloud/common/src/Common/Error/Builder.php
parentc7edd70b5e5b0f5159c78ce3d924d4e7f60db816 (diff)
parentc9202d9113210981ae47df40511645da2ee140df (diff)
Merge branch 'develop' into Eole_Graph
Conflicts: client/index.html client/js/controllers/home/home.js client/partials/home/home.html
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Error/Builder.php')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Error/Builder.php179
1 files changed, 0 insertions, 179 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Error/Builder.php b/server/vendor/php-opencloud/common/src/Common/Error/Builder.php
deleted file mode 100644
index e3ccdfe..0000000
--- a/server/vendor/php-opencloud/common/src/Common/Error/Builder.php
+++ /dev/null
@@ -1,179 +0,0 @@
-<?php
-
-namespace OpenCloud\Common\Error;
-
-use GuzzleHttp\Client;
-use GuzzleHttp\ClientInterface;
-use GuzzleHttp\Exception\ClientException;
-use Psr\Http\Message\MessageInterface;
-use Psr\Http\Message\RequestInterface;
-use Psr\Http\Message\ResponseInterface;
-
-/**
- * Class responsible for building meaningful exceptions. For HTTP problems, it produces a {@see HttpError}
- * exception, and supplies a error message with reasonable defaults. For user input problems, it produces a
- * {@see UserInputError} exception. For both, the problem is described, a potential solution is offered and
- * a link to further information is included.
- *
- * @package OpenCloud\Common\Error
- */
-class Builder
-{
- /**
- * The default domain to use for further link documentation.
- *
- * @var string
- */
- private $docDomain = 'http://docs.php-opencloud.com/en/latest/';
-
- /**
- * The HTTP client required to validate the further links.
- *
- * @var ClientInterface
- */
- private $client;
-
- /**
- * @param ClientInterface $client
- */
- public function __construct(ClientInterface $client = null)
- {
- $this->client = $client ?: new Client();
- }
-
- /**
- * Internal method used when outputting headers in the error description.
- *
- * @param $name
- *
- * @return string
- */
- private function header($name)
- {
- return sprintf("%s\n%s\n", $name, str_repeat('~', strlen($name)));
- }
-
- /**
- * Before outputting custom links, it is validated to ensure that the user is not
- * directed off to a broken link. If a 404 is detected, it is hidden.
- *
- * @param $link The proposed link
- *
- * @return bool
- */
- private function linkIsValid($link)
- {
- $link = $this->docDomain . $link;
-
- try {
- return $this->client->request('HEAD', $link)->getStatusCode() < 400;
- } catch (ClientException $e) {
- return false;
- }
- }
-
- /**
- * @param MessageInterface $message
- *
- * @codeCoverageIgnore
- * @return string
- */
- public function str(MessageInterface $message)
- {
- if ($message instanceof RequestInterface) {
- $msg = trim($message->getMethod() . ' '
- . $message->getRequestTarget())
- . ' HTTP/' . $message->getProtocolVersion();
- if (!$message->hasHeader('host')) {
- $msg .= "\r\nHost: " . $message->getUri()->getHost();
- }
- } elseif ($message instanceof ResponseInterface) {
- $msg = 'HTTP/' . $message->getProtocolVersion() . ' '
- . $message->getStatusCode() . ' '
- . $message->getReasonPhrase();
- }
-
- foreach ($message->getHeaders() as $name => $values) {
- $msg .= "\r\n{$name}: " . implode(', ', $values);
- }
-
- if ($message->getBody()->getSize() < ini_get('memory_limit')) {
- $msg .= "\r\n\r\n" . $message->getBody();
- }
-
- return $msg;
- }
-
- /**
- * Helper method responsible for constructing and returning {@see BadResponseError} exceptions.
- *
- * @param RequestInterface $request The faulty request
- * @param ResponseInterface $response The error-filled response
- *
- * @return BadResponseError
- */
- public function httpError(RequestInterface $request, ResponseInterface $response)
- {
- $message = $this->header('HTTP Error');
-
- $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n",
- $response->getStatusCode(), $response->getReasonPhrase());
-
- $message .= $this->header('Request');
- $message .= trim($this->str($request)) . PHP_EOL . PHP_EOL;
-
- $message .= $this->header('Response');
- $message .= trim($this->str($response)) . 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.";
-
- $e = new BadResponseError($message);
- $e->setRequest($request);
- $e->setResponse($response);
-
- return $e;
- }
-
- private function getStatusCodeMessage($statusCode)
- {
- $errors = [
- 400 => 'Please ensure that your input values are valid and well-formed. ',
- 401 => 'Please ensure that your authentication credentials are valid. ',
- 404 => "Please ensure that the resource you're trying to access actually exists. ",
- 500 => 'Please try this operation again once you know the remote server is operational. ',
- ];
-
- return isset($errors[$statusCode]) ? $errors[$statusCode] : '';
- }
-
- /**
- * Helper method responsible for constructing and returning {@see UserInputError} exceptions.
- *
- * @param string $expectedType The type that was expected from the user
- * @param mixed $userValue The incorrect value the user actually provided
- * @param string|null $furtherLink A link to further information if necessary (optional).
- *
- * @return UserInputError
- */
- public function userInputError($expectedType, $userValue, $furtherLink = null)
- {
- $message = $this->header('User Input Error');
-
- $message .= sprintf("%s was expected, but the following value was passed in:\n\n%s\n",
- $expectedType, print_r($userValue, true));
-
- $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 .= 'If you run into trouble, please open a support issue on https://github.com/php-opencloud/openstack/issues.';
-
- return new UserInputError($message);
- }
-}