Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.47% covered (warning)
76.47%
26 / 34
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequestBodyDecoderContextBuilder
76.47% covered (warning)
76.47%
26 / 34
50.00% covered (danger)
50.00%
1 / 2
6.47
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
75.76% covered (warning)
75.76%
25 / 33
0.00% covered (danger)
0.00%
0 / 1
5.36
1<?php
2namespace Apie\Common\ContextBuilders;
3
4use Apie\Common\RequestBodyDecoder;
5use Apie\Core\Context\ApieContext;
6use Apie\Core\ContextBuilders\ContextBuilderInterface;
7use Apie\Core\ContextConstants;
8use Apie\Serializer\FieldFilters\FieldFilterInterface;
9use Apie\Serializer\FieldFilters\FilterFromArray;
10use Apie\Serializer\Interfaces\DecoderInterface;
11use Apie\Serializer\Relations\EmbedRelationFromArray;
12use Apie\Serializer\Relations\EmbedRelationInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15class 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}