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 (#234)
by Jérémiah
07:13
created

GraphQLDumpSchemaCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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