RequestCookiesOperationEnricher::enrichOperation()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 4
rs 9.7998
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 ReflectionAttribute;
17
use ReflectionClass;
18
use ReflectionMethod;
19
use Sunrise\Http\Router\Annotation\RequestCookie;
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 RequestCookiesOperationEnricher 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...ookiesOperationEnricher.
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
        foreach ($requestHandler->getParameters() as $requestHandlerParameter) {
54
            /** @var list<ReflectionAttribute<RequestCookie>> $annotations */
55 3
            $annotations = $requestHandlerParameter->getAttributes(RequestCookie::class);
56 3
            if (isset($annotations[0])) {
57 3
                $requestCookie = $annotations[0]->newInstance();
58 3
                $requestCookieType = TypeFactory::fromPhpTypeReflection($requestHandlerParameter->getType());
59 3
                $requestCookieSchema = $this->openApiPhpTypeSchemaResolverManager
60 3
                    ->resolvePhpTypeSchema($requestCookieType, $requestHandlerParameter);
61
62 3
                $operation['parameters'][] = [
63 3
                    'in' => 'cookie',
64 3
                    'name' => $requestCookie->name,
65 3
                    'schema' => $requestCookieSchema,
66 3
                    'required' => !$requestHandlerParameter->isDefaultValueAvailable(),
67 3
                ];
68
            }
69
        }
70
    }
71
72 3
    public function getWeight(): int
73
    {
74 3
        return 10;
75
    }
76
}
77