RequestBodyOperationEnricher   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 55
ccs 23
cts 23
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setOpenApiPhpTypeSchemaResolverManager() 0 4 1
B enrichOperation() 0 35 7
A getWeight() 0 3 1
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\OpenApi\OperationEnricher;
15
16
use Psr\Http\Message\StreamInterface;
17
use ReflectionClass;
18
use ReflectionMethod;
19
use Sunrise\Http\Router\Annotation\RequestBody;
20
use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface;
21
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface;
22
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface;
23
use Sunrise\Http\Router\OpenApi\TypeFactory;
24
use Sunrise\Http\Router\RouteInterface;
25
26
/**
27
 * @since 3.0.0
28
 */
29
final class RequestBodyOperationEnricher implements
30
    OpenApiOperationEnricherInterface,
31
    OpenApiPhpTypeSchemaResolverManagerAwareInterface
32
{
33
    private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager;
34
35 5
    public function setOpenApiPhpTypeSchemaResolverManager(
36
        OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager,
37
    ): void {
38 5
        $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager;
0 ignored issues
show
Bug introduced by
The property openApiPhpTypeSchemaResolverManager is declared read-only in Sunrise\Http\Router\Open...stBodyOperationEnricher.
Loading history...
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 3
    public function enrichOperation(
45
        RouteInterface $route,
46
        ReflectionClass|ReflectionMethod $requestHandler,
47
        array &$operation,
48
    ): void {
49 3
        if (! $requestHandler instanceof ReflectionMethod) {
50 3
            return;
51
        }
52
53 3
        $requestBodySchema = null;
54 3
        foreach ($requestHandler->getParameters() as $requestHandlerParameter) {
55 3
            $requestBodyType = TypeFactory::fromPhpTypeReflection($requestHandlerParameter->getType());
56
            if (
57 3
                $requestHandlerParameter->getAttributes(RequestBody::class) !== [] ||
58 3
                $requestBodyType->is(StreamInterface::class)
59
            ) {
60 3
                $requestBodySchema = $this->openApiPhpTypeSchemaResolverManager
61 3
                    ->resolvePhpTypeSchema($requestBodyType, $requestHandlerParameter);
62 3
                break;
63
            }
64
        }
65
66 3
        if ($requestBodySchema === null) {
67 3
            return;
68
        }
69
70 3
        $operation['requestBody'] = [];
71
72 3
        foreach ($route->getConsumedMediaTypes() as $consumedMediaType) {
73 3
            $operation['requestBody']['content'][$consumedMediaType->getIdentifier()] = [
74 3
                'schema' => $requestBodySchema,
75 3
            ];
76
        }
77
78 3
        $operation['requestBody']['required'] = true;
79
    }
80
81 3
    public function getWeight(): int
82
    {
83 3
        return 0;
84
    }
85
}
86