Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ForbiddenResponse | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Miniframe\Response; |
| 4 | |
| 5 | use Miniframe\Core\Request; |
| 6 | use Miniframe\Core\Response; |
| 7 | |
| 8 | class ForbiddenResponse extends Response |
| 9 | { |
| 10 | /** |
| 11 | * Initializes a basic 403 Forbidden response |
| 12 | */ |
| 13 | public function __construct() |
| 14 | { |
| 15 | parent::__construct('', 1); |
| 16 | $this->setResponseCode(403); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Returns a basic HTML page explaining that something went wrong. |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public function render(): string |
| 25 | { |
| 26 | $request = Request::getActual(); |
| 27 | $signature = $request->getServer('SERVER_SIGNATURE'); |
| 28 | |
| 29 | // When JSON is requested, return JSON error |
| 30 | if ($request->getServer('HTTP_ACCEPT') == 'application/json') { |
| 31 | $data = [ |
| 32 | 'code' => 403, |
| 33 | 'error' => 'Forbidden', |
| 34 | 'message' => 'You don\'t have permission to access this resource.', |
| 35 | ]; |
| 36 | if ($signature) { |
| 37 | $data['signature'] = $signature; |
| 38 | } |
| 39 | return json_encode($data, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); |
| 40 | } |
| 41 | |
| 42 | return '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . PHP_EOL |
| 43 | . '<html><head>' . PHP_EOL |
| 44 | . '<title>403 Forbidden</title>' . PHP_EOL |
| 45 | . '</head><body>' . PHP_EOL |
| 46 | . '<h1>Forbidden</h1>' . PHP_EOL |
| 47 | . '<p>You don\'t have permission to access this resource.</p>' . PHP_EOL |
| 48 | . '<hr>' . PHP_EOL |
| 49 | . (is_string($signature) ? $signature : '') . PHP_EOL |
| 50 | . '</body></html>'; |
| 51 | } |
| 52 | } |