Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| RunGlobalMethodFormController | |
100.00% |
24 / 24 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
23 / 23 |
|
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\HtmlBuilders\Factories\ComponentFactory; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | use ReflectionClass; |
| 14 | use ReflectionMethod; |
| 15 | |
| 16 | class RunGlobalMethodFormController |
| 17 | { |
| 18 | public function __construct( |
| 19 | private readonly ApieFacade $apieFacade, |
| 20 | private readonly ComponentFactory $componentFactory, |
| 21 | private readonly ContextBuilderFactory $contextBuilderFactory, |
| 22 | private readonly ResponseFactory $responseFactory, |
| 23 | private readonly LayoutPicker $layoutPicker, |
| 24 | ) { |
| 25 | } |
| 26 | |
| 27 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
| 28 | { |
| 29 | $context = $this->contextBuilderFactory->createFromRequest($request, [ContextConstants::CMS => true]); |
| 30 | $context->checkAuthorization(); |
| 31 | $action = $this->apieFacade->createAction($context); |
| 32 | $method = new ReflectionMethod( |
| 33 | $context->getContext(ContextConstants::SERVICE_CLASS), |
| 34 | $context->getContext(ContextConstants::METHOD_NAME) |
| 35 | ); |
| 36 | $method = $action::getInputType( |
| 37 | new ReflectionClass($request->getAttribute(ContextConstants::SERVICE_CLASS)), |
| 38 | $method |
| 39 | ); |
| 40 | assert($method instanceof ReflectionMethod); |
| 41 | $layout = $this->layoutPicker->pickLayout($request); |
| 42 | $component = $this->componentFactory->createFormForMethod( |
| 43 | $request->getAttribute(ContextConstants::METHOD_NAME) ? : 'Form', |
| 44 | $method, |
| 45 | new BoundedContextId($request->getAttribute(ContextConstants::BOUNDED_CONTEXT_ID)), |
| 46 | $context, |
| 47 | $layout |
| 48 | ); |
| 49 | return $this->responseFactory->createComponentPageRender($component, $context) |
| 50 | ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') |
| 51 | ->withHeader('Pragma', 'no-cache'); |
| 52 | ; |
| 53 | } |
| 54 | } |