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 (#9)
by Jérémiah
08:12
created

OverblogGraphQLExtension::setGraphiQLTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\Config\Resource\FileResource;
16
use Symfony\Component\Config\Util\XmlUtils;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\Finder\Finder;
23
use Symfony\Component\Finder\SplFileInfo;
24
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
25
use Symfony\Component\Yaml\Exception\ParseException;
26
use Symfony\Component\Yaml\Parser as YamlParser;
27
28
class OverblogGraphQLExtension extends Extension
29
{
30
    private $yamlParser;
31
32 31
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 31
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
35 31
        $loader->load('services.yml');
36 31
        $loader->load('graphql_types.yml');
37 31
        $loader->load('graphql_fields.yml');
38 31
        $loader->load('graphql_args.yml');
39
40 31
        $configuration = $this->getConfiguration($configs, $container);
41 31
        $config = $this->processConfiguration($configuration, $configs);
42
43 31
        $this->setServicesAliases($config, $container);
44 31
        $this->setSchemaBuilderArguments($config, $container);
45 31
        $this->setSchemaArguments($config, $container);
46 31
        $this->setErrorHandlerArguments($config, $container);
47 31
        $this->setGraphiQLTemplate($config, $container);
48 31
        $this->loadTypes($config, $container);
49 31
    }
50
51 31
    private function loadTypes(array $config, ContainerBuilder $container)
52
    {
53 31
        $typesConfigs = $this->getTypesConfigs($config, $container);
54 31
        $typesConfig = $this->processConfiguration(new TypesConfiguration(), $typesConfigs);
55
56 31
        if (!empty($typesConfig)) {
57 27
            $builderId = $this->getAlias().'.type_builder';
58
59 27
            foreach ($typesConfig as $name => $options) {
60 27
                $customTypeId = sprintf('%s.definition.custom_%s_type', $this->getAlias(), $container->underscore($name));
61
62 27
                $options['config']['name'] = $name;
63
64
                $container
65 27
                    ->setDefinition($customTypeId, new Definition('GraphQL\\Type\\Definition\\Type'))
66 27
                    ->setFactory([new Reference($builderId), 'create'])
67 27
                    ->setArguments([$options['type'], $options['config']])
68 27
                    ->addTag($this->getAlias().'.type', ['alias' => $name])
69
                ;
70 27
            }
71 27
        }
72 31
    }
73
74 31
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
75
    {
76 31
        if (isset($config['templates']['graphiql'])) {
77 31
            $container->setParameter('overblog_graphql.graphiql_template', $config['templates']['graphiql']);
78 31
        }
79 31
    }
80
81 31
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
82
    {
83 31
        if (isset($config['definitions']['internal_error_message'])) {
84
            $container
85
                ->getDefinition($this->getAlias().'.error_handler')
86
                ->replaceArgument(0, $config['definitions']['internal_error_message'])
87
                ->setPublic(true)
88
            ;
89
        }
90 31
    }
91
92 31
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
93
    {
94 31
        $container->getDefinition($this->getAlias().'.schema_builder')
95 31
            ->replaceArgument(1, $config['definitions']['config_validation']);
96 31
    }
97
98 31
    private function setSchemaArguments(array $config, ContainerBuilder $container)
99
    {
100 31
        if (isset($config['definitions']['schema'])) {
101 1
            $container
102 31
                ->getDefinition($this->getAlias().'.schema')
103 31
                ->replaceArgument(0, $config['definitions']['schema']['query'])
104 31
                ->replaceArgument(1, $config['definitions']['schema']['mutation'])
105 31
                ->replaceArgument(2, $config['definitions']['schema']['subscription'])
106 31
                ->setPublic(true)
107
            ;
108 31
        }
109 31
    }
110
111 31
    private function setServicesAliases(array $config, ContainerBuilder $container)
112
    {
113 31
        if (isset($config['services'])) {
114 31
            foreach ($config['services'] as $name => $id) {
115 31
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
116 31
                $container->setAlias($alias, $id);
117 31
            }
118 31
        }
119 31
    }
120
121 31
    private function getTypesConfigs(array $config, ContainerBuilder $container)
122
    {
123 31
        $typesMappings = array_merge(
124 31
            $this->typesConfigsMappingFromConfig($config, $container),
125 31
            $this->typesConfigsMappingFromBundles($container)
126 31
        );
127
128
        // treats mappings
129 31
        $typesConfigs =  array_map(
130
            function ($params) use ($container) {
131 27
                $typesConfig = [];
132
                /** @var SplFileInfo $file */
133 27
                foreach ($params['files'] as $file) {
134 27
                    $method = 'typesConfigFrom'.$params['type'];
135 27
                    $typesConfig = array_merge($typesConfig, $this->$method($file, $container));
136 27
                }
137
138 27
                return $typesConfig;
139 31
            },
140
            $typesMappings
141 31
        );
142
143
        // TODO remove when types mapping 100% functional
144 31
        if (isset($config['definitions']['types'])) {
145
            $typesConfigs[] = $config['definitions']['types'];
146
        }
147
148 31
        return $typesConfigs;
149
    }
150
151
    private function typesConfigFromXml(SplFileInfo $file, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
152
    {
153
        $typesConfig = [];
154
155
        try {
156
            //@todo fix xml validateSchema
157
            $xml = XmlUtils::loadFile($file->getRealPath());
158
            foreach ($xml->documentElement->childNodes as $node) {
159
                if (!$node instanceof \DOMElement) {
160
                    continue;
161
                }
162
                $values = XmlUtils::convertDomElementToArray($node);
163
                if (!is_array($values)) {
164
                    continue;
165
                }
166
                $typesConfig = array_merge($typesConfig, $values);
167
            }
168
            $container->addResource(new FileResource($file->getRealPath()));
169
        } catch (\InvalidArgumentException $e) {
170
            throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
171
        }
172
173
        return $typesConfig;
174
    }
175
176 27
    private function typesConfigFromYml(SplFileInfo $file, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
177
    {
178 27
        if (null === $this->yamlParser) {
179 27
            $this->yamlParser = new YamlParser();
180 27
        }
181
182
        try {
183 27
            $typesConfig = $this->yamlParser->parse($file->getContents());
184 27
            $container->addResource(new FileResource($file->getRealPath()));
185 27
        } catch (ParseException $e) {
186
            throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
187
        }
188
189 27
        return $typesConfig;
190
    }
191
192 31
    private function typesConfigsMappingFromConfig(array $config, ContainerBuilder $container)
193
    {
194 31
        $typesMappings = [];
195
        // from config
196 31
        if (!empty($config['definitions']['mappings']['types'])) {
197 27
            $typesMappings = array_filter(array_map(
198 27
                function (array $typeMapping) use ($container) {
199
200 27
                    $params = $this->detectConfigFiles($container, $typeMapping['dir'],  $typeMapping['type']);
201
202 27
                    return $params;
203 27
                },
204 27
                $config['definitions']['mappings']['types']
205 27
            ));
206 27
        }
207
208 31
        return $typesMappings;
209
    }
210
211 31
    private function typesConfigsMappingFromBundles(ContainerBuilder $container)
212
    {
213 31
        $typesMappings = [];
214 31
        $bundles = $container->getParameter('kernel.bundles');
215
216
        // auto detect from bundle
217 31
        foreach ($bundles as $name => $class) {
218 31
            $bundle = new \ReflectionClass($class);
219 31
            $bundleDir = dirname($bundle->getFileName());
220
221 31
            $configPath = $bundleDir.'/'.$this->getMappingResourceConfigDirectory();
222 31
            $params = $this->detectConfigFiles($container, $configPath);
223
224 31
            if (null === $params) {
225 31
                continue;
226
            }
227
228
            $typesMappings[] = $params;
229 31
        }
230
231 31
        return $typesMappings;
232
    }
233
234 31
    private function detectConfigFiles(ContainerBuilder $container, $configPath, $type = null)
235
    {
236
        // add the closest existing directory as a resource
237 31
        $resource = $configPath;
238 31
        while (!is_dir($resource)) {
239 31
            $resource = dirname($resource);
240 31
        }
241 31
        $container->addResource(new FileResource($resource));
242
243 31
        $extension = $this->getMappingResourceExtension();
244 31
        $finder = new Finder();
245
246 31
        $types = null === $type ? ['yml', 'xml'] : [$type];
247
248 31
        foreach ($types as $type) {
249
            try {
250 31
                $finder->files()->in($configPath)->name('*.'.$extension.'.'.$type);
251 31
            } catch (\InvalidArgumentException $e) {
252 31
                continue;
253
            }
254 27
            if (0 === $finder->count()) {
255
                continue;
256
            }
257
258
            return [
259 27
                'type' => $type,
260 27
                'files' => $finder,
261 27
            ];
262 31
        }
263
264 31
        return;
265
    }
266
267 31
    private function getMappingResourceConfigDirectory()
268
    {
269 31
        return 'Resources/config/graphql';
270
    }
271
272 31
    private function getMappingResourceExtension()
273
    {
274 31
        return 'types';
275
    }
276
277 31
    public function getAlias()
278
    {
279 31
        return 'overblog_graphql';
280
    }
281
282 31
    public function getConfiguration(array $config, ContainerBuilder $container)
283
    {
284 31
        return new Configuration($container->getParameter('kernel.debug'));
285
    }
286
}
287