Passed
Push — master ( 69cfd7...96431f )
by Bruno
10:28 queued 13s
created

ModelariumFrontendCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 50
dl 0
loc 115
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 34 8
A __construct() 0 3 1
A generateFromModel() 0 36 4
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Formularium\FrameworkComposer;
6
use Formularium\Model;
7
use HaydenPierce\ClassFinder\ClassFinder;
8
use Illuminate\Console\Command;
9
use Modelarium\Frontend\FrontendGenerator;
10
11
class ModelariumFrontendCommand extends Command
12
{
13
    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...
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'modelarium:frontend
21
        {name : The model name. Use "*" or "all" for all models}
22
        {--framework=* : The frameworks to use}
23
        {--overwrite : overwrite files if they exist}
24
        {--prettier : run prettier on files}
25
    ';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Creates frontend using Modelarium';
33
34
    /**
35
     * @var FrameworkComposer
36
     */
37
    protected $composer;
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     * @return void
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct();
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        $name = $this->argument('name');
57
58
        // setup stuff
59
        $frameworks = $this->option('framework');
60
        if (empty($frameworks)) {
61
            $this->error('If you are generating frontend you need to specify frameworks. Example: `--framework=HTML --framework=Bootstrap --framework=Vue`');
62
            return;
63
        }
64
        if (!is_array($frameworks)) {
0 ignored issues
show
introduced by
The condition is_array($frameworks) is always true.
Loading history...
65
            $frameworks = [$frameworks];
66
        }
67
      
68
        $this->composer = FrameworkComposer::create($frameworks);
69
70
        if ($name === '*' || $name === 'all') {
71
            // TODO: all classes
72
            $classesInNamespace = ClassFinder::getClassesInNamespace('App\\Models');
73
74
            foreach ($classesInNamespace as $class) {
75
                $reflection = new \ReflectionClass($class);
76
                if (!$reflection->isInstantiable()) {
77
                    continue;
78
                }
79
                $this->generateFromModel($class);
80
            }
81
            return;
82
        } elseif (is_array($name)) {
83
            // TODO
84
        } else {
85
            $this->generateFromModel($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Modelarium\Laravel\Conso...nd::generateFromModel() 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

85
            $this->generateFromModel(/** @scrutinizer ignore-type */ $name);
Loading history...
86
        }
87
        $this->info('Finished frontend.');
88
    }
89
90
    protected function generateFromModel(string $name)
91
    {
92
        $model = $name::getFormularium();
93
        $generator = new FrontendGenerator($this->composer, $model);
94
        $collection = $generator->generate();
95
    
96
        if (!$collection) {
0 ignored issues
show
introduced by
$collection is of type Modelarium\GeneratedCollection, thus it always evaluated to true.
Loading history...
97
            $this->info('Nothing generated.');
98
            return;
99
        }
100
101
        $basepath = base_path('resources/js/components/');
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

101
        $basepath = /** @scrutinizer ignore-call */ base_path('resources/js/components/');
Loading history...
102
        $writtenFiles = $this->writeFiles(
103
            $collection,
104
            $basepath,
105
            (bool)$this->option('overwrite')
106
        );
107
        $this->info('Files generated.');
108
109
        if ($this->option('prettier')) {
110
            $this->info('Running prettier on generated files.');
111
            $useYarn = file_exists(base_path('yarn.lock'));
112
            if ($useYarn) {
113
                $command = "cd $basepath && npx prettier --write ";
114
            } else {
115
                $command = "cd $basepath && yarn prettier --write ";
116
            }
117
118
            // this runs all prettier commands in parallel.
119
            $run = array_reduce(
120
                $writtenFiles,
121
                function ($carry, $f) use ($command) {
122
                    return $carry . '(' . $command . $f . ') & ';
123
                }
124
            );
125
            shell_exec($run . ' wait');
126
        }
127
    }
128
}
129