Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#333)
by Jérémiah
08:28 queued 05:48
created

GraphQLDumpSchemaCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Overblog\GraphQLBundle\Command;
4
5
use GraphQL\Type\Introspection;
6
use GraphQL\Utils\SchemaPrinter;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
final class GraphQLDumpSchemaCommand extends Command
14
{
15
    use RequestExecutorLazyLoaderTrait;
16
17
    /** @var string */
18
    private $baseExportPath;
19
20
    public function __construct($baseExportPath)
21
    {
22
        parent::__construct();
23
        $this->baseExportPath = $baseExportPath;
24
    }
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('graphql:dump-schema')
30
            ->setAliases(['graph:dump-schema'])
31
            ->setDescription('Dumps GraphQL schema')
32
            ->addOption(
33
                'file',
34
                null,
35
                InputOption::VALUE_OPTIONAL,
36
                'Path to generate schema file.'
37
            )
38
            ->addOption(
39
                'schema',
40
                null,
41
                InputOption::VALUE_OPTIONAL,
42
                'The schema name to generate.'
43
            )
44
            ->addOption(
45
                'format',
46
                null,
47
                InputOption::VALUE_OPTIONAL,
48
                'The schema format to generate ("graphql" or "json").',
49
                'json'
50
            )
51
            ->addOption(
52
                'modern',
53
                null,
54
                InputOption::VALUE_NONE,
55
                'Enabled modern json format: { "data": { "__schema": {...} } }.'
56
            )
57
            ->addOption(
58
                'classic',
59
                null,
60
                InputOption::VALUE_NONE,
61
                'Enabled classic json format: { "__schema": {...} }.'
62
            )
63
            ->addOption(
64
                'with-descriptions',
65
                null,
66
                InputOption::VALUE_NONE,
67
                'Dump schema including descriptions.'
68
            )
69
        ;
70
    }
71
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $io = new SymfonyStyle($input, $output);
75
        $file = $this->createFile($input);
76
        $io->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file)));
77
    }
78
79
    private function createFile(InputInterface $input)
80
    {
81
        $format = strtolower($input->getOption('format'));
82
        $schemaName = $input->getOption('schema');
83
        $includeDescription = $input->getOption('with-descriptions');
84
85
        $file = $input->getOption('file') ?: $this->baseExportPath.sprintf('/../var/schema%s.%s', $schemaName ? '.'.$schemaName : '', $format);
86
87
        switch ($format) {
88
            case 'json':
89
                $request = [
90
                    // TODO(mcg-web): remove silence deprecation notices after removing webonyx/graphql-php <= 0.11
91
                    'query' => @Introspection::getIntrospectionQuery($includeDescription),
92
                    'variables' => [],
93
                    'operationName' => null,
94
                ];
95
96
                $modern = $this->useModernJsonFormat($input);
97
98
                $result = $this->getRequestExecutor()
99
                    ->execute($schemaName, $request)
100
                    ->toArray();
101
102
                $content = json_encode($modern ? $result : $result['data'], \JSON_PRETTY_PRINT);
103
                break;
104
105
            case 'graphql':
106
                $content = SchemaPrinter::doPrint($this->getRequestExecutor()->getSchema($schemaName));
107
                break;
108
109
            default:
110
                throw new \InvalidArgumentException(sprintf('Unknown format %s.', json_encode($format)));
111
        }
112
113
        file_put_contents($file, $content);
114
115
        return $file;
116
    }
117
118
    private function useModernJsonFormat(InputInterface $input)
119
    {
120
        $modern = $input->getOption('modern');
121
        $classic = $input->getOption('classic');
122
        if ($modern && $classic) {
123
            throw new \InvalidArgumentException('"modern" and "classic" options should not be used together.');
124
        }
125
126
        return true === $modern;
127
    }
128
}
129