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 (#293)
by Jérémiah
24:10
created

GraphQLDumpSchemaCommand::getRequestExecutor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 7
        $this->baseExportPath = $baseExportPath;
24
    }
25
26
    protected function configure()
27 7
    {
28 7
        $this
29 7
            ->setName('graphql:dump-schema')
30 7
            ->setAliases(['graph:dump-schema'])
31
            ->setDescription('Dumps GraphQL schema')
32 7
            ->addOption(
33
                'file',
34
                null,
35 7
                InputOption::VALUE_OPTIONAL,
36 7
                'Path to generate schema file.'
37 7
            )
38 7
            ->addOption(
39 7
                'schema',
40 7
                null,
41 7
                InputOption::VALUE_OPTIONAL,
42 7
                'The schema name to generate.'
43
            )
44 7
            ->addOption(
45 7
                'format',
46 7
                null,
47 7
                InputOption::VALUE_OPTIONAL,
48 7
                'The schema format to generate ("graphql" or "json").',
49
                'json'
50 7
            )
51 7
            ->addOption(
52 7
                'modern',
53 7
                null,
54 7
                InputOption::VALUE_NONE,
55 7
                'Enabled modern json format: { "data": { "__schema": {...} } }.'
56
            )
57 7
            ->addOption(
58 7
                'classic',
59 7
                null,
60 7
                InputOption::VALUE_NONE,
61 7
                'Enabled classic json format: { "__schema": {...} }.'
62
            )
63 7
        ;
64 7
    }
65 7
66 7
    protected function execute(InputInterface $input, OutputInterface $output)
67 7
    {
68
        $io = new SymfonyStyle($input, $output);
69
        $file = $this->createFile($input);
70 7
        $io->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file)));
71
    }
72 7
73
    private function createFile(InputInterface $input)
74 7
    {
75 7
        $format = strtolower($input->getOption('format'));
76 5
        $schemaName = $input->getOption('schema');
77 5
78
        $file = $input->getOption('file') ?: $this->baseExportPath.sprintf('/../var/schema%s.%s', $schemaName ? '.'.$schemaName : '', $format);
79 7
80
        switch ($format) {
81 7
            case 'json':
82 7
                $request = [
83
                    'query' => Introspection::getIntrospectionQuery(false),
84 7
                    'variables' => [],
85
                    'operationName' => null,
86 7
                ];
87 7
88
                $modern = $this->useModernJsonFormat($input);
89 5
90
                $result = $this->getRequestExecutor()
91
                    ->execute($schemaName, $request)
92
                    ->toArray();
93
94 5
                $content = json_encode($modern ? $result : $result['data'], \JSON_PRETTY_PRINT);
95
                break;
96 4
97 4
            case 'graphql':
98 4
                $content = SchemaPrinter::doPrint($this->getRequestExecutor()->getSchema($schemaName));
99
                break;
100 4
101 4
            default:
102
                throw new \InvalidArgumentException(sprintf('Unknown format %s.', json_encode($format)));
103 2
        }
104 1
105 1
        file_put_contents($file, $content);
106
107
        return $file;
108 1
    }
109
110
    private function useModernJsonFormat(InputInterface $input)
111 5
    {
112
        $modern = $input->getOption('modern');
113 5
        $classic = $input->getOption('classic');
114
        if ($modern && $classic) {
115
            throw new \InvalidArgumentException('"modern" and "classic" options should not be used together.');
116 5
        }
117
118 5
        return true === $modern;
119 5
    }
120
}
121