RouterOpenApiBuildDocumentCommand::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 16
ccs 13
cts 13
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\Command;
15
16
use Sunrise\Http\Router\OpenApi\OpenApiDocumentManagerInterface;
17
use Sunrise\Http\Router\RouteInterface;
18
use Sunrise\Http\Router\RouterInterface;
19
use Symfony\Component\Console\Attribute\AsCommand;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
use function array_filter;
25
26
/**
27
 * @since 3.0.0
28
 */
29
#[AsCommand('router:openapi:build-document', 'Builds the OpenAPI document.')]
30
final class RouterOpenApiBuildDocumentCommand extends Command
31
{
32 3
    public function __construct(
33
        private readonly RouterInterface $router,
34
        private readonly OpenApiDocumentManagerInterface $openApiDocumentManager,
35
    ) {
36 3
        parent::__construct();
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 3
    protected function execute(InputInterface $input, OutputInterface $output): int
43
    {
44 3
        $this->openApiDocumentManager->saveDocument(
45 3
            $this->openApiDocumentManager->buildDocument(
46 3
                array_filter(
47 3
                    $this->router->getRoutes(),
48 3
                    static function (RouteInterface $route): bool {
49 3
                        return $route->isApiRoute();
50 3
                    },
51 3
                )
52 3
            )
53 3
        );
54
55 1
        $output->writeln('Done.');
56
57 1
        return self::SUCCESS;
58
    }
59
}
60