|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Plaisio\ExceptionHandler; |
| 5 | + |
| 6 | +use Plaisio\Kernel\Nub; |
| 7 | +use Plaisio\Obfuscator\Exception\DecodeException; |
| 8 | +use Plaisio\Response\BadRequestResponse; |
| 9 | + |
| 10 | +/** |
| 11 | + * An agent that handles DecodeException exceptions. |
| 12 | + */ |
| 13 | +class DecodeExceptionAgent |
| 14 | +{ |
| 15 | + //-------------------------------------------------------------------------------------------------------------------- |
| 16 | + /** |
| 17 | + * Handles a DecodeException thrown in the constructor of a page object. |
| 18 | + * |
| 19 | + * @param DecodeException $exception The exception. |
| 20 | + * |
| 21 | + * @since 1.2.0 |
| 22 | + * @api |
| 23 | + */ |
| 24 | + public function handleConstructException(DecodeException $exception): void |
| 25 | + { |
| 26 | + $this->handleException($exception); |
| 27 | + } |
| 28 | + |
| 29 | + //-------------------------------------------------------------------------------------------------------------------- |
| 30 | + /** |
| 31 | + * Handles a DecodeException thrown during generating the response by a page object. |
| 32 | + * |
| 33 | + * @param DecodeException $exception The exception. |
| 34 | + * |
| 35 | + * @since 1.2.0 |
| 36 | + * @api |
| 37 | + */ |
| 38 | + public function handleResponseException(DecodeException $exception): void |
| 39 | + { |
| 40 | + $this->handleException($exception); |
| 41 | + } |
| 42 | + |
| 43 | + //-------------------------------------------------------------------------------------------------------------------- |
| 44 | + /** |
| 45 | + * Handles a DecodeException. |
| 46 | + * |
| 47 | + * @param DecodeException $exception The exception. |
| 48 | + */ |
| 49 | + private function handleException(DecodeException $exception): void |
| 50 | + { |
| 51 | + Nub::$DL->rollback(); |
| 52 | + |
| 53 | + // Set the HTTP status to 400 (Bad Request). |
| 54 | + $response = new BadRequestResponse(); |
| 55 | + $response->send(); |
| 56 | + |
| 57 | + // Log the bad request. |
| 58 | + Nub::$requestLogger->logRequest($response->getStatus()); |
| 59 | + Nub::$DL->commit(); |
| 60 | + |
| 61 | + // Only on development environment log the error. |
| 62 | + if (Nub::$request->isEnvDev()) |
| 63 | + { |
| 64 | + $logger = Nub::$nub->getErrorLogger(); |
| 65 | + $logger->logError($exception); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + //-------------------------------------------------------------------------------------------------------------------- |
| 70 | +} |
| 71 | + |
| 72 | +//---------------------------------------------------------------------------------------------------------------------- |
0 commit comments