Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CmsErrorRenderer | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createCmsResponse | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\HtmlBuilders\ErrorHandler; |
| 4 | |
| 5 | use Apie\Common\Interfaces\DashboardContentFactoryInterface; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Context\ApieContext; |
| 8 | use Apie\Core\ContextConstants; |
| 9 | use Apie\HtmlBuilders\Factories\ComponentFactory; |
| 10 | use Apie\HtmlBuilders\Interfaces\ComponentRendererInterface; |
| 11 | use Symfony\Component\HttpFoundation\Request; |
| 12 | use Symfony\Component\HttpFoundation\Response; |
| 13 | use Throwable; |
| 14 | |
| 15 | class CmsErrorRenderer |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly ComponentFactory $componentFactory, |
| 19 | private readonly ComponentRendererInterface $componentRenderer, |
| 20 | private readonly DashboardContentFactoryInterface $dashboardContentFactory, |
| 21 | private readonly string $errorTemplate, |
| 22 | private readonly bool $debugMode |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @TODO: should be PSR request and response? |
| 28 | * |
| 29 | * @param Request $request |
| 30 | * @param Throwable $error |
| 31 | * @return Response |
| 32 | */ |
| 33 | public function createCmsResponse(Request $request, Throwable $error): Response |
| 34 | { |
| 35 | $contents = $this->dashboardContentFactory->create($this->errorTemplate, ['error' => $error, 'debugMode' => $this->debugMode]); |
| 36 | $boundedContextId = null; |
| 37 | if ($request->attributes->has(ContextConstants::BOUNDED_CONTEXT_ID)) { |
| 38 | $boundedContextId = new BoundedContextId($request->attributes->get(ContextConstants::BOUNDED_CONTEXT_ID)); |
| 39 | } |
| 40 | return new Response( |
| 41 | $this->componentRenderer->render( |
| 42 | $this->componentFactory->createWrapLayout( |
| 43 | 'Error', |
| 44 | $boundedContextId, |
| 45 | new ApieContext(), |
| 46 | $this->componentFactory->createRawContents($contents) |
| 47 | ), |
| 48 | new ApieContext(), |
| 49 | ), |
| 50 | (new WrappedError($error))->getStatusCode() |
| 51 | ); |
| 52 | } |
| 53 | } |