SchemaCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 12
nop 2
dl 0
loc 26
ccs 17
cts 17
cp 1
crap 5
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Schema;
6
7
use Cycle\Schema\Renderer\OutputSchemaRenderer;
8
use Cycle\Schema\Renderer\SchemaToArrayConverter;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Console\ExitCode;
14
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
15
16
final class SchemaCommand extends Command
17
{
18
    protected static $defaultName = 'cycle/schema';
19
    protected static $defaultDescription = 'Shown current schema';
20
21
    private CycleDependencyProxy $promise;
22
23 2
    public function __construct(CycleDependencyProxy $promise)
24
    {
25 2
        $this->promise = $promise;
26 2
        parent::__construct();
27
    }
28
29 2
    public function configure(): void
30
    {
31 2
        $this->addArgument('role', InputArgument::OPTIONAL, 'Roles to display (separated by ",").');
32
    }
33
34 2
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        /** @var string|null $roleArgument */
37 2
        $roleArgument = $input->getArgument('role');
38 2
        $schema = $this->promise->getSchema();
39 2
        $roles = $roleArgument !== null ? explode(',', $roleArgument) : $schema->getRoles();
40
41 2
        $schemaArray = (new SchemaToArrayConverter())->convert($schema);
42
43 2
        $notFound = [];
44 2
        $found = [];
45 2
        foreach ($roles as $role) {
46 2
            if (!\array_key_exists($role, $schemaArray)) {
47 1
                $notFound[] = $role;
48 1
                continue;
49
            }
50 1
            $found[$role] = $schemaArray[$role];
51
        }
52 2
        $renderer = new OutputSchemaRenderer(OutputSchemaRenderer::FORMAT_CONSOLE_COLOR);
53 2
        $output->write($renderer->render($found));
54
55 2
        if ($notFound !== []) {
56 1
            $output->writeln(sprintf('<fg=red>Undefined roles: %s</>', implode(', ', $notFound)));
57
        }
58
59 2
        return ExitCode::OK;
60
    }
61
}
62