ModelariumDirectiveCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 80
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handle() 0 44 8
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Formularium\Factory\DatatypeFactory;
6
use Formularium\Exception\Exception;
7
use HaydenPierce\ClassFinder\ClassFinder;
8
use Illuminate\Console\Command;
9
use Nette\PhpGenerator\PhpNamespace;
10
11
use function Safe\substr;
12
13
class ModelariumDirectiveCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'modelarium:directive
21
        {name : The directive name}
22
        {--generator= : the generator it inherits from. May have more than one.}
23
        {--namespace=Modelarium\Laravel\Directives : the class namespace. Defaults to "Modelarium\Laravel\Directives"}
24
        {--path= : path to save the file. Defaults to base_path("app/Modelarium/Datatype") }
25
    ';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Creates directives using Modelarium';
33
34
    /**
35
     * Create a new command instance.
36
     *
37
     * @return void
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return mixed
48
     */
49
    public function handle()
50
    {
51
        $name = $this->argument('name');
52
        if (!is_string($name)) {
53
            $this->error('Name must be a string');
54
            return;
55
        }
56
        $generators = $this->option('generator');
57
        if (is_string($generators)) {
0 ignored issues
show
introduced by
The condition is_string($generators) is always true.
Loading history...
58
            $generators = [$generators];
59
        } elseif (is_array($generators)) {
60
            // ok
61
        } else {
62
            $this->error('Invalid generators');
63
            return;
64
        }
65
66
        $ns = $this->option('namespace');
67
        if (!is_string($ns)) {
0 ignored issues
show
introduced by
The condition is_string($ns) is always true.
Loading history...
68
            $this->error('Namespace must be a string');
69
            return;
70
        }
71
        $path = $this->option('path') ?: base_path("");
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

71
        $path = $this->option('path') ?: /** @scrutinizer ignore-call */ base_path("");
Loading history...
72
73
        if (!is_dir($path)) {
74
            \Safe\mkdir($path, 0777, true);
75
        }
76
77
        $printer = new \Nette\PhpGenerator\PsrPrinter;
78
79
        $namespace = new PhpNamespace($ns);
80
        $class = $namespace->addClass(ucfirst($name) . 'Directive');
81
82
        foreach ($generators as $g) {
83
            // TODO: validate $g
84
            $namespace->addUse("Modelarium\Laravel\Targets\$gGenerator");
85
            $class->addImplement("{$g}DirectiveInterface");
86
        }
87
88
        $code = "<?php declare(strict_types=1);\n" . $printer->printNamespace($namespace);
89
        $filename = $path . "/" . ucfirst($name) . 'Directive.php';
90
        \Safe\file_put_contents($filename, $code);
91
92
        $this->info("Directive $name created.");
93
    }
94
}
95