Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| NotFoundResponse | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Miniframe\Response; |
| 4 | |
| 5 | use Miniframe\Core\Request; |
| 6 | use Miniframe\Core\Response; |
| 7 | |
| 8 | class NotFoundResponse extends Response |
| 9 | { |
| 10 | /** |
| 11 | * Initializes a basic 404 Not Found response |
| 12 | */ |
| 13 | public function __construct() |
| 14 | { |
| 15 | $request = Request::getActual(); |
| 16 | $requestUri = $request->getServer('REQUEST_URI'); |
| 17 | $signature = $request->getServer('SERVER_SIGNATURE'); |
| 18 | |
| 19 | $code = 404; |
| 20 | $error = 'Not Found'; |
| 21 | $message = 'The requested URL ' |
| 22 | . (is_string($requestUri) ? $requestUri : '') |
| 23 | . ' was not found on this server.'; |
| 24 | |
| 25 | if ($request->getServer('HTTP_ACCEPT') == 'application/json') { |
| 26 | $data = [ |
| 27 | 'code' => $code, |
| 28 | 'error' => $error, |
| 29 | 'message' => $message, |
| 30 | ]; |
| 31 | if (is_string($signature)) { |
| 32 | $data['signature'] = $signature; |
| 33 | } |
| 34 | $text = json_encode($data, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); |
| 35 | } else { |
| 36 | $text = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . PHP_EOL |
| 37 | . '<html><head>' . PHP_EOL |
| 38 | . '<title>' . htmlspecialchars($code . ' ' . $error) . '</title>' . PHP_EOL |
| 39 | . '</head><body>' . PHP_EOL |
| 40 | . '<h1>' . htmlspecialchars($error) . '</h1>' . PHP_EOL |
| 41 | . '<p>' . htmlspecialchars($message) . '</p>' . PHP_EOL |
| 42 | . '<hr>' . PHP_EOL |
| 43 | . (is_string($signature) ? $signature : '') . PHP_EOL |
| 44 | . '</body></html>'; |
| 45 | } |
| 46 | |
| 47 | parent::__construct($text, 1); |
| 48 | $this->setResponseCode($code); |
| 49 | } |
| 50 | } |