SchemaCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 44
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 26 5
A __construct() 0 4 1
A configure() 0 3 1
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