Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.47% |
26 / 34 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RequestBodyDecoderContextBuilder | |
76.47% |
26 / 34 |
|
50.00% |
1 / 2 |
6.47 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
75.76% |
25 / 33 |
|
0.00% |
0 / 1 |
5.36 | |||
| 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 | $rawContents = $this->requestBodyDecoder->decodeBody( |
| 40 | $context->getContext(ServerRequestInterface::class) |
| 41 | ); |
| 42 | return $context |
| 43 | ->withContext( |
| 44 | DecoderInterface::class, |
| 45 | $this->requestBodyDecoder->getDecoder( |
| 46 | $context->getContext(ServerRequestInterface::class) |
| 47 | ) |
| 48 | ) |
| 49 | ->withContext( |
| 50 | ServerRequestInterface::class, |
| 51 | $request->withParsedBody($rawContents) |
| 52 | ) |
| 53 | ->withContext( |
| 54 | ContextConstants::RAW_CONTENTS, |
| 55 | $rawContents |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return $context; |
| 61 | } |
| 62 | } |