Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RunMethodCallOnSingleResourceFormController
100.00% covered (success)
100.00%
28 / 28
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%
27 / 27
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\Core\IdentifierUtils;
11use Apie\HtmlBuilders\Factories\ComponentFactory;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14use ReflectionClass;
15use ReflectionMethod;
16
17class 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    }
58}