Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.50% |
37 / 40 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ModifyResourceFormController | |
92.50% |
37 / 40 |
|
50.00% |
1 / 2 |
6.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
92.31% |
36 / 39 |
|
0.00% |
0 / 1 |
5.01 | |||
| 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\Common\IntegrationTestLogger; |
| 8 | use Apie\Core\BoundedContext\BoundedContextId; |
| 9 | use Apie\Core\ContextBuilders\ContextBuilderFactory; |
| 10 | use Apie\Core\ContextConstants; |
| 11 | use Apie\Core\Entities\EntityInterface; |
| 12 | use Apie\Core\Exceptions\EntityNotFoundException; |
| 13 | use Apie\Core\Exceptions\InvalidTypeException; |
| 14 | use Apie\Core\IdentifierUtils; |
| 15 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 16 | use Apie\HtmlBuilders\Factories\ComponentFactory; |
| 17 | use Apie\Serializer\Serializer; |
| 18 | use Psr\Http\Message\ResponseInterface; |
| 19 | use Psr\Http\Message\ServerRequestInterface; |
| 20 | use ReflectionClass; |
| 21 | |
| 22 | class ModifyResourceFormController |
| 23 | { |
| 24 | public function __construct( |
| 25 | private readonly ApieFacade $apieFacade, |
| 26 | private readonly ComponentFactory $componentFactory, |
| 27 | private readonly ContextBuilderFactory $contextBuilderFactory, |
| 28 | private readonly ResponseFactory $responseFactory, |
| 29 | private readonly LayoutPicker $layoutPicker |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
| 34 | { |
| 35 | $context = $this->contextBuilderFactory->createFromRequest($request, [ContextConstants::CMS => true]); |
| 36 | $context->checkAuthorization(); |
| 37 | $action = $this->apieFacade->createAction($context); |
| 38 | $class = $action::getInputType( |
| 39 | new ReflectionClass($request->getAttribute(ContextConstants::RESOURCE_NAME)) |
| 40 | ); |
| 41 | |
| 42 | // TODO: copied from ModifyObjectAction, make it shared |
| 43 | $resourceClass = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME)); |
| 44 | $id = $context->getContext(ContextConstants::RESOURCE_ID); |
| 45 | if (!$resourceClass->implementsInterface(EntityInterface::class)) { |
| 46 | throw new InvalidTypeException($resourceClass->name, 'EntityInterface'); |
| 47 | } |
| 48 | try { |
| 49 | $resource = $this->apieFacade->find( |
| 50 | IdentifierUtils::idStringToIdentifier($id, $context), |
| 51 | new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 52 | ); |
| 53 | } catch (InvalidStringForValueObjectException|EntityNotFoundException $error) { |
| 54 | IntegrationTestLogger::logException($error); |
| 55 | } |
| 56 | if (isset($resource)) { |
| 57 | $context = $context->withContext(ContextConstants::RESOURCE, $resource); |
| 58 | $class = new ReflectionClass($resource); |
| 59 | if (empty($context->getContext(ContextConstants::RAW_CONTENTS, false))) { |
| 60 | /** @var Serializer $serializer */ |
| 61 | $serializer = $context->getContext(Serializer::class); |
| 62 | $context = $context->withContext( |
| 63 | ContextConstants::RAW_CONTENTS, |
| 64 | json_decode(json_encode($serializer->normalize( |
| 65 | $resource, |
| 66 | $context |
| 67 | )), true) |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | $layout = $this->layoutPicker->pickLayout($request); |
| 72 | $component = $this->componentFactory->createFormForResourceModification( |
| 73 | 'Modify ' . $class->getShortName(), |
| 74 | $class, |
| 75 | new BoundedContextId($request->getAttribute(ContextConstants::BOUNDED_CONTEXT_ID)), |
| 76 | $context, |
| 77 | $layout |
| 78 | ); |
| 79 | return $this->responseFactory->createComponentPageRender($component, $context) |
| 80 | ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') |
| 81 | ->withHeader('Pragma', 'no-cache'); |
| 82 | ; |
| 83 | } |
| 84 | } |