Passed
Push — master ( 705ab8...a10add )
by Rustam
13:49 queued 11:19
created

SchemaCommand::printValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 20
rs 10
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
    public function __construct(CycleDependencyProxy $promise)
24
    {
25
        $this->promise = $promise;
26
        parent::__construct();
27
    }
28
29
    public function configure(): void
30
    {
31
        $this->addArgument('role', InputArgument::OPTIONAL, 'Roles to display (separated by ",").');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        /** @var string|null $roleArgument */
37
        $roleArgument = $input->getArgument('role');
38
        $schema = $this->promise->getSchema();
39
        $roles = $roleArgument !== null ? explode(',', $roleArgument) : $schema->getRoles();
40
41
        $schemaArray = (new SchemaToArrayConverter())->convert($schema);
42
43
        $notFound = [];
44
        $found = [];
45
        foreach ($roles as $role) {
46
            if (!\array_key_exists($role, $schemaArray)) {
47
                $notFound[] = $role;
48
                continue;
49
            }
50
            $found[$role] = $schemaArray[$role];
51
        }
52
        $renderer = new OutputSchemaRenderer(OutputSchemaRenderer::FORMAT_CONSOLE_COLOR);
53
        $output->write($renderer->render($found));
54
55
        if ($notFound !== []) {
56
            $output->writeln(sprintf('<fg=red>Undefined roles: %s</>', implode(', ', $notFound)));
57
        }
58
59
        return ExitCode::OK;
60
    }
61
}
62