Passed
Push — master ( 6ac985...96b623 )
by Bruno
09:22 queued 14s
created

ModelariumCommand   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 191
rs 10
c 0
b 0
f 0
wmc 25

6 Methods

Rating   Name   Duplication   Size   Complexity  
A writeFiles() 0 14 3
A getPathGraphql() 0 4 1
A _handle() 0 2 1
C handle() 0 60 14
A writeFile() 0 18 5
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Modelarium\GeneratedCollection;
7
use Modelarium\Laravel\Processor as LaravelProcessor;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
10
// use Formularium\FrameworkComposer;
11
// use Formularium\Frontend\Blade\Framework as FrameworkBlade;
12
// use Formularium\Frontend\Vue\Framework as FrameworkVue;
13
14
class ModelariumCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'modelarium:scaffold
22
        {name : The model name}
23
        {--framework=* : The frameworks to use}
24
        {--overwrite : overwrite files if they exist}
25
        {--everything : make everything}
26
        {--model : make model}
27
        {--event : make event}
28
        {--migration : make migration}
29
        {--factory : make factory}
30
        {--seed : make seed}
31
        {--policy : make policy}
32
        {--frontend : make frontend files}';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Creates scaffolding using Modelarium';
40
41
    /**
42
     * Create a new command instance.
43
     *
44
     * @return void
45
     */
46
    public function __construct()
47
    {
48
        parent::__construct();
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return mixed
55
     */
56
    public function handle()
57
    {
58
        $name = $this->argument('name');
59
60
        $processor = new LaravelProcessor();
61
62
        // parse args
63
        $processor->setRunModel($this->option('model') || $this->option('everything'));
64
        $processor->setRunMigration($this->option('migration') || $this->option('everything'));
65
        $processor->setRunFactory($this->option('factory') || $this->option('everything'));
66
        $processor->setRunSeed($this->option('seed') || $this->option('everything'));
67
        $processor->setRunPolicy($this->option('policy') || $this->option('everything'));
68
        $processor->setRunEvent($this->option('event') || $this->option('everything'));
69
70
        // TODO: see issue #2
71
        // generate lighthouse directives
72
        // $output = new BufferedOutput();
73
        // $this->call('lighthouse:ide-helper');
74
        // $output->fetch();
75
76
        if ($name === '*' || $name === 'all') {
77
            // @phpstan-ignore-next-line
78
            $path = base_path('graphql');
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

78
            $path = /** @scrutinizer ignore-call */ base_path('graphql');
Loading history...
79
            $dir = \Safe\scandir($path);
80
81
            // parse directives from lighthouse
82
            $modelNames = array_diff($dir, array('.', '..'));
83
            $files = [
84
                base_path('schema-directives.graphql'),
85
                __DIR__ . '/../../Graphql/definitions.graphql'
86
            ];
87
            
88
            foreach ($modelNames as $n) {
89
                if (mb_strpos($n, '.graphql') === false) {
90
                    continue;
91
                }
92
                // @phpstan-ignore-next-line
93
                $files[] = base_path('graphql/' . $n);
94
            }
95
            $processor->processFiles($files);
96
        } elseif (!$name || is_array($name)) {
97
            $this->error('Invalid name parameter');
98
            return;
99
        } else {
100
            try {
101
                $data = \Safe\file_get_contents($this->getPathGraphql($name));
102
                $processor->processString($data);
103
            } catch (\Safe\Exceptions\FilesystemException $e) {
104
                $this->error("Cannot open model $name");
105
            }
106
        }
107
108
        $this->writeFiles(
109
            $processor->getCollection(),
110
            // @phpstan-ignore-next-line
111
            base_path(),
112
            (bool)$this->option('overwrite')
113
        );
114
115
        $this->info('Finished. You might want to run `composer dump-autoload`');
116
    }
117
118
    protected function getPathGraphql(string $name): string
119
    {
120
        // @phpstan-ignore-next-line
121
        return base_path('graphql/' . $name . '.graphql');
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

121
        return /** @scrutinizer ignore-call */ base_path('graphql/' . $name . '.graphql');
Loading history...
122
    }
123
124
    protected function _handle(string $data): void
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

124
    protected function _handle(/** @scrutinizer ignore-unused */ string $data): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        // TODO
127
        // // setup stuff
128
        // $frameworks = $this->option('framework');
129
        // FrameworkComposer::set($frameworks);
130
131
        // /**
132
        //  * @var FrameworkVue $vue
133
        //  */
134
        // $vue = FrameworkComposer::getByName('Vue');
135
        // $blade = FrameworkComposer::getByName('Blade');
136
137
        // if ($this->hasOption('stubdir')) {
138
        //     $this->stubDir = $this->option('stubdir');
139
        // }
140
141
142
143
        // if ($this->option('frontend') || $this->option('everything')) {
144
        //     if ($vue) {
145
        //         $this->makeVueScaffold();
146
        //         $this->makeVue($vue, 'Base', 'viewable');
147
        //         $this->makeVue($vue, 'Item', 'viewable');
148
        //         $this->makeVue($vue, 'ListPage', 'viewable');
149
        //         $this->makeVue($vue, 'ShowPage', 'viewable');
150
        //         $this->makeVue($vue, 'EditPage', 'editable');
151
        //         $this->line('Generated Vue');
152
        //     } elseif ($blade) {
153
        //         $this->makeBlade($blade, 'show', 'viewable');
154
        //         $this->makeBlade($blade, 'index', 'viewable');
155
        //         $this->makeBlade($blade, 'form', 'editable');
156
        //         $this->line('Generated Blade');
157
        //     } else {
158
        //         // TODO: react?
159
        //     }
160
        // }
161
    }
162
163
    public function writeFiles(GeneratedCollection $collection, string $basepath, bool $overwrite = true): self
164
    {
165
        foreach ($collection as $element) {
166
            /**
167
             * @var GeneratedItem $element
168
             */
169
            $path = $basepath . '/' . $element->filename;
170
            $this->writeFile(
171
                $path,
172
                ($element->onlyIfNewFile ? false : $overwrite),
173
                $element->contents
174
            );
175
        }
176
        return $this;
177
    }
178
179
    /**
180
     * Takes a stub file and generates the target file with replacements.
181
     *
182
     * @param string $targetPath The path for the stub file.
183
     * @param boolean $overwrite
184
     * @param string $data The data to write
185
     * @return void
186
     */
187
    protected function writeFile(string $targetPath, bool $overwrite, string $data)
188
    {
189
        if (file_exists($targetPath) && !$overwrite) {
190
            $this->comment("File $targetPath already exists, not overwriting.");
191
            return;
192
        }
193
194
        $dir = dirname($targetPath);
195
        if (!is_dir($dir)) {
196
            \Safe\mkdir($dir, 0777, true);
197
        }
198
199
        $ret = \Safe\file_put_contents($targetPath, $data);
200
        if (!$ret) {
201
            $this->error("Cannot write to $targetPath");
202
            return;
203
        }
204
        $this->line("Wrote $targetPath");
205
    }
206
}
207