Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.41% |
21 / 29 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RequestBodyDecoderContextBuilder | |
72.41% |
21 / 29 |
|
50.00% |
1 / 2 |
6.76 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
71.43% |
20 / 28 |
|
0.00% |
0 / 1 |
5.58 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ContextBuilders; |
| 3 | |
| 4 | use Apie\Common\RequestBodyDecoder; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Apie\Serializer\FieldFilters\FieldFilterInterface; |
| 9 | use Apie\Serializer\FieldFilters\FilterFromArray; |
| 10 | use Apie\Serializer\Interfaces\DecoderInterface; |
| 11 | use Apie\Serializer\Relations\EmbedRelationFromArray; |
| 12 | use Apie\Serializer\Relations\EmbedRelationInterface; |
| 13 | use Psr\Http\Message\ServerRequestInterface; |
| 14 | |
| 15 | class RequestBodyDecoderContextBuilder implements ContextBuilderInterface |
| 16 | { |
| 17 | public function __construct(private readonly RequestBodyDecoder $requestBodyDecoder) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | public function process(ApieContext $context): ApieContext |
| 22 | { |
| 23 | $request = $context->getContext(ServerRequestInterface::class, false); |
| 24 | if ($request instanceof ServerRequestInterface) { |
| 25 | $queryParams = $request->getQueryParams(); |
| 26 | if (isset($queryParams['fields'])) { |
| 27 | $context = $context->withContext( |
| 28 | FieldFilterInterface::class, |
| 29 | FilterFromArray::createFromMixed($queryParams['fields']) |
| 30 | ); |
| 31 | } |
| 32 | if (isset($queryParams['relations'])) { |
| 33 | $context = $context->withContext( |
| 34 | EmbedRelationInterface::class, |
| 35 | EmbedRelationFromArray::createFromMixed($queryParams['relations']) |
| 36 | ); |
| 37 | } |
| 38 | if (!$context->hasContext(ContextConstants::RAW_CONTENTS)) { |
| 39 | return $context |
| 40 | ->withContext( |
| 41 | DecoderInterface::class, |
| 42 | $this->requestBodyDecoder->getDecoder( |
| 43 | $context->getContext(ServerRequestInterface::class) |
| 44 | ) |
| 45 | ) |
| 46 | ->withContext( |
| 47 | ContextConstants::RAW_CONTENTS, |
| 48 | $this->requestBodyDecoder->decodeBody( |
| 49 | $context->getContext(ServerRequestInterface::class) |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $context; |
| 56 | } |
| 57 | } |