Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ResponseFactory | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRedirect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createComponentPageRender | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Cms\Services; |
| 3 | |
| 4 | use Apie\Common\Events\ResponseDispatcher; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 7 | use Apie\HtmlBuilders\Interfaces\ComponentRendererInterface; |
| 8 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | |
| 11 | final class ResponseFactory |
| 12 | { |
| 13 | private readonly Psr17Factory $psr17Factory; |
| 14 | |
| 15 | public function __construct( |
| 16 | private readonly ComponentRendererInterface $renderer, |
| 17 | private readonly ResponseDispatcher $responseDispatcher |
| 18 | ) { |
| 19 | $this->psr17Factory = new Psr17Factory(); |
| 20 | } |
| 21 | |
| 22 | public function createRedirect(string $redirectUrl, ApieContext $context): ResponseInterface |
| 23 | { |
| 24 | $response = $this->psr17Factory->createResponse(301)->withHeader('Location', $redirectUrl); |
| 25 | return $this->responseDispatcher->triggerResponseCreated( |
| 26 | $response, |
| 27 | $context |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | public function createComponentPageRender(ComponentInterface $component, ApieContext $context): ResponseInterface |
| 32 | { |
| 33 | $html = $this->renderer->render($component, $context); |
| 34 | $psr17Factory = new Psr17Factory(); |
| 35 | $response = $psr17Factory->createResponse(200) |
| 36 | ->withBody($psr17Factory->createStream($html)) |
| 37 | ->withHeader('Content-Type', 'text/html'); |
| 38 | return $this->responseDispatcher->triggerResponseCreated( |
| 39 | $response, |
| 40 | $context |
| 41 | ); |
| 42 | } |
| 43 | } |