Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RunGlobalMethodFormController
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\Cms\Controllers;
3
4use Apie\Cms\LayoutPicker;
5use Apie\Cms\Services\ResponseFactory;
6use Apie\Common\ApieFacade;
7use Apie\Core\BoundedContext\BoundedContextId;
8use Apie\Core\ContextBuilders\ContextBuilderFactory;
9use Apie\Core\ContextConstants;
10use Apie\HtmlBuilders\Factories\ComponentFactory;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13use ReflectionClass;
14use ReflectionMethod;
15
16class 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    }
51}