1 | <?php |
||
20 | class Builder |
||
21 | { |
||
22 | /** |
||
23 | * The default domain to use for further link documentation. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $docDomain = 'http://docs.php-opencloud.com/en/latest/'; |
||
28 | |||
29 | /** |
||
30 | * The HTTP client required to validate the further links. |
||
31 | * |
||
32 | * @var ClientInterface |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * @param ClientInterface $client |
||
38 | */ |
||
39 | 2 | public function __construct(ClientInterface $client = null) |
|
40 | { |
||
41 | 2 | $this->client = $client ?: new Client(); |
|
42 | 2 | } |
|
43 | |||
44 | /** |
||
45 | * Internal method used when outputting headers in the error description. |
||
46 | * |
||
47 | * @param $name |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 4 | private function header($name) |
|
55 | |||
56 | /** |
||
57 | * Before outputting custom links, it is validated to ensure that the user is not |
||
58 | * directed off to a broken link. If a 404 is detected, it is hidden. |
||
59 | * |
||
60 | * @param $link The proposed link |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | 2 | private function linkIsValid($link) |
|
75 | |||
76 | 2 | public function str(MessageInterface $message) |
|
77 | { |
||
78 | 2 | if ($message instanceof RequestInterface) { |
|
79 | 2 | $msg = trim($message->getMethod() . ' ' |
|
80 | 2 | . $message->getRequestTarget()) |
|
81 | 2 | . ' HTTP/' . $message->getProtocolVersion(); |
|
82 | 2 | if (!$message->hasHeader('host')) { |
|
83 | 1 | $msg .= "\r\nHost: " . $message->getUri()->getHost(); |
|
84 | 1 | } |
|
85 | 2 | } elseif ($message instanceof ResponseInterface) { |
|
86 | 2 | $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' |
|
87 | 2 | . $message->getStatusCode() . ' ' |
|
88 | 2 | . $message->getReasonPhrase(); |
|
89 | 2 | } |
|
90 | |||
91 | 2 | foreach ($message->getHeaders() as $name => $values) { |
|
92 | 1 | $msg .= "\r\n{$name}: " . implode(', ', $values); |
|
93 | 2 | } |
|
94 | |||
95 | 2 | if ($message->getBody()->getSize() < ini_get('memory_limit')) { |
|
96 | 2 | $msg .= "\r\n\r\n" . $message->getBody(); |
|
97 | 2 | } |
|
98 | |||
99 | 2 | return $msg; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Helper method responsible for constructing and returning {@see BadResponseError} exceptions. |
||
104 | * |
||
105 | * @param RequestInterface $request The faulty request |
||
106 | * @param ResponseInterface $response The error-filled response |
||
107 | * |
||
108 | * @return BadResponseError |
||
109 | */ |
||
110 | 2 | public function httpError(RequestInterface $request, ResponseInterface $response) |
|
111 | { |
||
112 | 2 | $message = $this->header('HTTP Error'); |
|
113 | |||
114 | 2 | $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", |
|
115 | 2 | $response->getStatusCode(), $response->getReasonPhrase()); |
|
116 | |||
117 | 2 | $message .= $this->header('Request'); |
|
118 | 2 | $message .= trim($this->str($request)) . PHP_EOL . PHP_EOL; |
|
119 | |||
120 | 2 | $message .= $this->header('Response'); |
|
121 | 2 | $message .= trim($this->str($response)) . PHP_EOL . PHP_EOL; |
|
122 | |||
123 | 2 | $message .= $this->header('Further information'); |
|
124 | 2 | $message .= $this->getStatusCodeMessage($response->getStatusCode()); |
|
125 | |||
126 | $message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " |
||
127 | 2 | . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; |
|
128 | |||
129 | 2 | $e = new BadResponseError($message); |
|
130 | 2 | $e->setRequest($request); |
|
131 | 2 | $e->setResponse($response); |
|
132 | |||
133 | 2 | return $e; |
|
134 | } |
||
135 | |||
136 | 2 | private function getStatusCodeMessage($statusCode) |
|
147 | |||
148 | /** |
||
149 | * Helper method responsible for constructing and returning {@see UserInputError} exceptions. |
||
150 | * |
||
151 | * @param string $expectedType The type that was expected from the user |
||
152 | * @param mixed $userValue The incorrect value the user actually provided |
||
153 | * @param string|null $furtherLink A link to further information if necessary (optional). |
||
154 | * |
||
155 | * @return UserInputError |
||
156 | */ |
||
157 | 2 | public function userInputError($expectedType, $userValue, $furtherLink = null) |
|
174 | } |
||
175 |