Passed
Pull Request — master (#24)
by Alexander
01:32
created

SchemaPhpCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 20
ccs 0
cts 18
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Common;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Yiisoft\Aliases\Aliases;
12
use Yiisoft\Yii\Console\ExitCode;
13
use Yiisoft\Yii\Cycle\Command\CycleDependencyPromise;
14
use Yiisoft\Yii\Cycle\Helper\SchemaToPHP;
15
16
final class SchemaPhpCommand extends Command
17
{
18
    protected static $defaultName = 'cycle/schema/php';
19
20
    private CycleDependencyPromise $promise;
21
    private Aliases $aliases;
22
23
    public function __construct(Aliases $aliases, CycleDependencyPromise $promise)
24
    {
25
        $this->aliases = $aliases;
26
        $this->promise = $promise;
27
        parent::__construct();
28
    }
29
30
    public function configure(): void
31
    {
32
        $this->setDescription('Save current schema in a PHP file')
33
             ->addArgument('file', InputArgument::OPTIONAL, 'file')
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39
        $file = $input->getArgument('file');
40
41
        $content = (new SchemaToPHP($this->promise->getSchema()))->render();
42
        if ($file !== null) {
43
            $file = $this->aliases->get($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type string[]; however, parameter $alias of Yiisoft\Aliases\Aliases::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            $file = $this->aliases->get(/** @scrutinizer ignore-type */ $file);
Loading history...
44
            $output->writeln("Destination: <fg=cyan>{$file}</>");
45
            // Dir exists
46
            $dir = dirname($file);
47
            if (!is_dir($dir)) {
48
                throw new \RuntimeException("Directory {$dir} not found");
49
            }
50
            if (file_put_contents($file, $content) === false) {
51
                return ExitCode::UNSPECIFIED_ERROR;
52
            }
53
        } else {
54
            echo $content;
55
        }
56
        return ExitCode::OK;
57
    }
58
}
59