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

Passed
Pull Request — master (#210)
by Renato
08:31
created

GraphQLDumpSchemaCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 1

Importance

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