Passed
Push — master ( 41e1cb...c64b66 )
by Bruno
09:40
created

ModelariumFrontendCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Formularium\FrameworkComposer;
6
use Formularium\Model;
7
use Illuminate\Console\Command;
8
use Modelarium\Frontend\FrontendGenerator;
9
10
class ModelariumFrontendCommand extends Command
11
{
12
    use WriterTrait;
0 ignored issues
show
introduced by
The trait Modelarium\Laravel\Console\Commands\WriterTrait requires some properties which are not provided by Modelarium\Laravel\Conso...delariumFrontendCommand: $filename, $onlyIfNewFile, $contents
Loading history...
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'modelarium:frontend
20
        {name : The model name. Use "*" or "all" for all models}
21
        {--framework=* : The frameworks to use}
22
        {--overwrite : overwrite files if they exist}
23
        {--prettier : run prettier on files}
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Creates frontend using Modelarium';
32
33
    /**
34
     * Create a new command instance.
35
     *
36
     * @return void
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return mixed
47
     */
48
    public function handle()
49
    {
50
        $name = $this->argument('name');
51
52
        // setup stuff
53
        $frameworks = $this->option('framework');
54
        if (empty($frameworks)) {
55
            $this->error('If you are generating frontend you need to specify frameworks. Example: `--framework=HTML --framework=Bootstrap --framework=Vue`');
56
            return;
57
        }
58
        if (!is_array($frameworks)) {
0 ignored issues
show
introduced by
The condition is_array($frameworks) is always true.
Loading history...
59
            $frameworks = [$frameworks];
60
        }
61
      
62
        $composer = FrameworkComposer::create($frameworks);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Formularium\FrameworkComposer. ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        $composer = FrameworkComposer::create($frameworks);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        $collection = null;
64
65
        if ($name === '*' || $name === 'all') {
66
            // TODO: all classes
67
        } elseif (is_array($name)) {
68
            // TODO
69
        } else {
70
            $model = $name::getFormularium();
71
            $generator = new FrontendGenerator($composer, $model);
72
            $collection = $generator->generate();
73
        }
74
75
        if (!$collection) {
76
            $this->info('Nothing generated.');
77
            return;
78
        }
79
80
        $basepath = 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

80
        $basepath = /** @scrutinizer ignore-call */ base_path();
Loading history...
81
        $writtenFiles = $this->writeFiles(
82
            $collection,
83
            $basepath,
84
            (bool)$this->option('overwrite')
85
        );
86
87
        if ($this->option('prettier')) {
88
            $this->info('Running prettier.');
89
            foreach ($writtenFiles as $f) {
90
                shell_exec("cd $basepath && npx prettier --write $f");
91
            }
92
        }
93
        $this->info('Finished frontend.');
94
    }
95
}
96