EmptyResponseOperationEnricher::enrichOperation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 3
rs 9.9666
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 Fig\Http\Message\StatusCodeInterface;
17
use ReflectionClass;
18
use ReflectionMethod;
19
use Sunrise\Http\Router\OpenApi\OpenApiConfiguration;
20
use Sunrise\Http\Router\OpenApi\OpenApiConfigurationAwareInterface;
21
use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface;
22
use Sunrise\Http\Router\OpenApi\Type;
23
use Sunrise\Http\Router\OpenApi\TypeFactory;
24
use Sunrise\Http\Router\RouteInterface;
25
26
/**
27
 * @since 3.0.0
28
 */
29
final class EmptyResponseOperationEnricher extends AbstractResponseOperationEnricher implements
30
    OpenApiOperationEnricherInterface,
31
    OpenApiConfigurationAwareInterface
32
{
33
    private readonly OpenApiConfiguration $openApiConfiguration;
34
35 5
    public function setOpenApiConfiguration(OpenApiConfiguration $openApiConfiguration): void
36
    {
37 5
        $this->openApiConfiguration = $openApiConfiguration;
0 ignored issues
show
Bug introduced by
The property openApiConfiguration is declared read-only in Sunrise\Http\Router\Open...sponseOperationEnricher.
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
        $responseBodyType = TypeFactory::fromPhpTypeReflection($requestHandler->getReturnType());
53 3
        if (!$responseBodyType->is(Type::PHP_TYPE_NAME_VOID)) {
54 3
            return;
55
        }
56
57 3
        $responseStatusCode = self::getResponseStatusCode($requestHandler) ?? StatusCodeInterface::STATUS_NO_CONTENT;
58
59 3
        $operation['responses'][$responseStatusCode] = [
60 3
            'description' => $this->openApiConfiguration->defaultResponseDescription,
61 3
        ];
62
63 3
        self::enrichResponseWithHeaders($requestHandler, $operation['responses'][$responseStatusCode]);
64
    }
65
66 3
    public function getWeight(): int
67
    {
68 3
        return 10;
69
    }
70
}
71