Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       13 / 13  | 
               | 
       100.00%  | 
       3 / 3  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| DropdownOptionController |         | 
       100.00%  | 
       13 / 13  | 
               | 
       100.00%  | 
       3 / 3  | 
       4 |         | 
       100.00%  | 
       1 / 1  | 
      
| __construct |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| __invoke |         | 
       100.00%  | 
       4 / 4  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| createResponse |         | 
       100.00%  | 
       8 / 8  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| 1 | <?php | 
| 2 | namespace Apie\CmsApiDropdownOption\Controllers; | 
| 3 | |
| 4 | use Apie\Common\ApieFacade; | 
| 5 | use Apie\Core\Actions\ActionResponse; | 
| 6 | use Apie\Core\ContextBuilders\ContextBuilderFactory; | 
| 7 | use Apie\Core\ContextConstants; | 
| 8 | use Apie\Serializer\EncoderHashmap; | 
| 9 | use Nyholm\Psr7\Factory\Psr17Factory; | 
| 10 | use Psr\Http\Message\ResponseInterface; | 
| 11 | use Psr\Http\Message\ServerRequestInterface; | 
| 12 | |
| 13 | class DropdownOptionController | 
| 14 | { | 
| 15 | public function __construct( | 
| 16 | private readonly ContextBuilderFactory $contextBuilderFactory, | 
| 17 | private readonly ApieFacade $apieFacade, | 
| 18 | private readonly EncoderHashmap $encoderHashmap | 
| 19 | ) { | 
| 20 | } | 
| 21 | |
| 22 | public function __invoke(ServerRequestInterface $request): ResponseInterface | 
| 23 | { | 
| 24 | $context = $this->contextBuilderFactory->createFromRequest($request, [ContextConstants::REST_API => true]); | 
| 25 | |
| 26 | $action = $this->apieFacade->createAction($context); | 
| 27 | $data = ($action)($context, $context->getContext(ContextConstants::RAW_CONTENTS)); | 
| 28 | return $this->createResponse($request, $data); | 
| 29 | } | 
| 30 | |
| 31 | private function createResponse(ServerRequestInterface $request, ActionResponse $output): ResponseInterface | 
| 32 | { | 
| 33 | $contentType = $this->encoderHashmap->getAcceptedContentTypeForRequest($request); | 
| 34 | $encoder = $this->encoderHashmap[$contentType]; | 
| 35 | |
| 36 | $psr17Factory = new Psr17Factory(); | 
| 37 | $statusCode = $output->getStatusCode(); | 
| 38 | |
| 39 | $responseBody = $psr17Factory->createStream($statusCode === 204 ? '' : $encoder->encode($output->getResultAsNativeData())); | 
| 40 | |
| 41 | return $psr17Factory->createResponse($statusCode) | 
| 42 | ->withBody($responseBody) | 
| 43 | ->withHeader('Content-Type', $contentType); | 
| 44 | } | 
| 45 | } |