Passed
Push — master ( 23ad8b...41e1cb )
by Bruno
05:36
created

ModelariumScaffoldCommand::writeFile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 3
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\GeneratedItem;
8
use Modelarium\Laravel\Processor as LaravelProcessor;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
11
class ModelariumScaffoldCommand 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...delariumScaffoldCommand: $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:scaffold
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
        {--lighthouse : use lighthouse directives}
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
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Creates scaffolding using Modelarium';
39
40
    /**
41
     * Create a new command instance.
42
     *
43
     * @return void
44
     */
45
    public function __construct()
46
    {
47
        parent::__construct();
48
    }
49
50
    /**
51
     * Execute the console command.
52
     *
53
     * @return mixed
54
     */
55
    public function handle()
56
    {
57
        $name = $this->argument('name');
58
59
        $processor = new LaravelProcessor();
60
61
        // parse args
62
        $processor->setRunModel($this->option('model') || $this->option('everything'));
63
        $processor->setRunMigration($this->option('migration') || $this->option('everything'));
64
        $processor->setRunFactory($this->option('factory') || $this->option('everything'));
65
        $processor->setRunSeed($this->option('seed') || $this->option('everything'));
66
        $processor->setRunPolicy($this->option('policy') || $this->option('everything'));
67
        $processor->setRunEvent($this->option('event') || $this->option('everything'));
68
69
        $files = [
70
            // todo __DIR__ . '/../../Graphql/definitions.graphql',
71
            __DIR__ . '/../../../Types/Graphql/scalars.graphql'
72
        ];
73
74
        if ($this->option('lighthouse')) {
75
            // TODO: see issue #2
76
            // generate lighthouse directives
77
            // $output = new BufferedOutput();
78
            // $this->call('lighthouse:ide-helper');
79
            // $output->fetch();
80
81
            $files[] = base_path('schema-directives.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

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

122
        return /** @scrutinizer ignore-call */ base_path('graphql/' . $name . '.graphql');
Loading history...
123
    }
124
}
125