RequestQueryOperationEnricher::getWeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 ReflectionClass;
17
use ReflectionMethod;
18
use Sunrise\Http\Router\Annotation\RequestQuery;
19
use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface;
20
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface;
21
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface;
22
use Sunrise\Http\Router\OpenApi\TypeFactory;
23
use Sunrise\Http\Router\RouteInterface;
24
25
/**
26
 * @since 3.0.0
27
 */
28
final class RequestQueryOperationEnricher implements
29
    OpenApiOperationEnricherInterface,
30
    OpenApiPhpTypeSchemaResolverManagerAwareInterface
31
{
32
    private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager;
33
34 5
    public function setOpenApiPhpTypeSchemaResolverManager(
35
        OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager,
36
    ): void {
37 5
        $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager;
0 ignored issues
show
Bug introduced by
The property openApiPhpTypeSchemaResolverManager is declared read-only in Sunrise\Http\Router\Open...tQueryOperationEnricher.
Loading history...
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 3
    public function enrichOperation(
44
        RouteInterface $route,
45
        ReflectionClass|ReflectionMethod $requestHandler,
46
        array &$operation,
47
    ): void {
48 3
        if (! $requestHandler instanceof ReflectionMethod) {
49 3
            return;
50
        }
51
52 3
        $requestQuerySchema = null;
53 3
        foreach ($requestHandler->getParameters() as $requestHandlerParameter) {
54 3
            if ($requestHandlerParameter->getAttributes(RequestQuery::class) !== []) {
55 3
                $requestQueryType = TypeFactory::fromPhpTypeReflection($requestHandlerParameter->getType());
56 3
                $requestQuerySchema = $this->openApiPhpTypeSchemaResolverManager
57 3
                    ->resolvePhpTypeSchema($requestQueryType, $requestHandlerParameter);
58 3
                break;
59
            }
60
        }
61
62 3
        if ($requestQuerySchema === null) {
63 3
            return;
64
        }
65
66 3
        $operation['parameters'][] = [
67 3
            'in' => 'query',
68 3
            'name' => 'Query',
69 3
            'schema' => $requestQuerySchema,
70 3
            'required' => true,
71 3
        ];
72
    }
73
74 3
    public function getWeight(): int
75
    {
76 3
        return 30;
77
    }
78
}
79