OpenApiController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
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\Controller;
15
16
use Psr\Http\Message\ResponseFactoryInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Message\StreamFactoryInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use Sunrise\Http\Router\Annotation\GetRoute;
22
use Sunrise\Http\Router\Annotation\Priority;
23
use Sunrise\Http\Router\Dictionary\HeaderName;
24
use Sunrise\Http\Router\OpenApi\OpenApiConfiguration;
25
use Sunrise\Http\Router\OpenApi\OpenApiDocumentManagerInterface;
26
27
/**
28
 * @since 3.0.0
29
 */
30
#[GetRoute(self::class, self::ROUTE_PATH)]
31
#[Priority(-1)]
32
final class OpenApiController implements RequestHandlerInterface
33
{
34
    public const ROUTE_PATH = '/openapi';
35
36 2
    public function __construct(
37
        private readonly OpenApiConfiguration $openApiConfiguration,
38
        private readonly OpenApiDocumentManagerInterface $openApiDocumentManager,
39
        private readonly ResponseFactoryInterface $responseFactory,
40
        private readonly StreamFactoryInterface $streamFactory,
41
    ) {
42 2
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 2
    public function handle(ServerRequestInterface $request): ResponseInterface
48
    {
49 2
        $responseBody = $this->streamFactory->createStreamFromResource(
50 2
            $this->openApiDocumentManager->openDocument(),
51 2
        );
52
53 1
        $responseContentType = $this->openApiConfiguration->documentMediaType->getIdentifier();
54 1
        $responseContentType .= '; charset=UTF-8'; // It should be a text data type...
55
56 1
        return $this->responseFactory->createResponse()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->responseFa...withBody($responseBody) returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
57 1
            ->withHeader(HeaderName::CONTENT_TYPE, $responseContentType)
58 1
            ->withBody($responseBody);
59
    }
60
}
61