GenerateSchemaCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils\Console;
4
5
use DeInternetJongens\LighthouseUtils\Generators\SchemaGenerator;
6
use Illuminate\Console\Command;
7
8
/**
9
 * @codeCoverageIgnore
10
 */
11
class GenerateSchemaCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'lighthouse-utils:generate-schema';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Generates a new schema from seperate files and validate the generated schema.';
26
27
    /** @var SchemaGenerator */
28
    private $schemaGenerator;
29
30
    /**
31
     * @param SchemaGenerator $schemaGenerator
32
     */
33
    public function __construct(SchemaGenerator $schemaGenerator)
34
    {
35
        $this->schemaGenerator = $schemaGenerator;
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @throws \Exception
43
     */
44
    public function handle()
45
    {
46
        // Clear the Lighthouse cached schema
47
        $this->call('lighthouse:clear-cache');
48
49
        $schemaFilePath = config('lighthouse.schema.register');
50
        $this->askWithCompletion(
51
            sprintf(
52
                'Generating schema in location: "%s", do you want to continue?',
53
                $schemaFilePath
54
            ),
55
            ['yes', 'no'],
56
            'yes'
57
        );
58
59
        $schemaFilesPaths = config('lighthouse-utils.schema_paths');
60
        $generatedSchema = $this->schemaGenerator->generate($schemaFilesPaths);
61
62
        $schemaFile = fopen($schemaFilePath, 'wb');
63
        fwrite($schemaFile, $generatedSchema);
0 ignored issues
show
Bug introduced by
It seems like $schemaFile can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

63
        fwrite(/** @scrutinizer ignore-type */ $schemaFile, $generatedSchema);
Loading history...
64
65
        $this->info('Generation complete.');
66
    }
67
}
68