Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
30 / 30 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| RunMethodCallOnSingleResourceFormController | |
100.00% |
30 / 30 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Cms\Controllers; |
| 3 | |
| 4 | use Apie\Cms\LayoutPicker; |
| 5 | use Apie\Cms\Services\ResponseFactory; |
| 6 | use Apie\Common\ApieFacade; |
| 7 | use Apie\Core\BoundedContext\BoundedContextId; |
| 8 | use Apie\Core\ContextBuilders\ContextBuilderFactory; |
| 9 | use Apie\Core\ContextConstants; |
| 10 | use Apie\Core\IdentifierUtils; |
| 11 | use Apie\HtmlBuilders\Factories\ComponentFactory; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | use Psr\Http\Message\ServerRequestInterface; |
| 14 | use ReflectionClass; |
| 15 | use ReflectionMethod; |
| 16 | |
| 17 | class RunMethodCallOnSingleResourceFormController |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly ApieFacade $apieFacade, |
| 21 | private readonly ComponentFactory $componentFactory, |
| 22 | private readonly ContextBuilderFactory $contextBuilderFactory, |
| 23 | private readonly ResponseFactory $responseFactory, |
| 24 | private readonly LayoutPicker $layoutPicker, |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
| 29 | { |
| 30 | $context = $this->contextBuilderFactory->createFromRequest($request, [ContextConstants::CMS => true]); |
| 31 | $context->checkAuthorization(); |
| 32 | $action = $this->apieFacade->createAction($context); |
| 33 | $method = new ReflectionMethod( |
| 34 | $context->getContext(ContextConstants::METHOD_CLASS), |
| 35 | $context->getContext(ContextConstants::METHOD_NAME) |
| 36 | ); |
| 37 | $method = $action::getInputType( |
| 38 | new ReflectionClass($request->getAttribute(ContextConstants::METHOD_CLASS)), |
| 39 | $method |
| 40 | ); |
| 41 | assert($method instanceof ReflectionMethod); |
| 42 | $id = $context->getContext(ContextConstants::RESOURCE_ID); |
| 43 | $resource = $this->apieFacade->find( |
| 44 | IdentifierUtils::idStringToIdentifier($id, $context), |
| 45 | new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 46 | ); |
| 47 | $context = $context->withContext(ContextConstants::RESOURCE, $resource); |
| 48 | $layout = $this->layoutPicker->pickLayout($request); |
| 49 | $component = $this->componentFactory->createFormForMethod( |
| 50 | $request->getAttribute(ContextConstants::METHOD_NAME) ? : 'Form', |
| 51 | $method, |
| 52 | new BoundedContextId($request->getAttribute(ContextConstants::BOUNDED_CONTEXT_ID)), |
| 53 | $context, |
| 54 | $layout |
| 55 | ); |
| 56 | return $this->responseFactory->createComponentPageRender($component, $context) |
| 57 | ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') |
| 58 | ->withHeader('Pragma', 'no-cache'); |
| 59 | ; |
| 60 | } |
| 61 | } |