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

typesConfigFromXml()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.0534
cc 4
eloc 13
nc 7
nop 2
crap 4
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\Resource\FileResource;
15
use Symfony\Component\Config\Util\XmlUtils;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18
use Symfony\Component\Finder\Finder;
19
use Symfony\Component\Finder\SplFileInfo;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Symfony\Component\Yaml\Exception\ParseException;
22
use Symfony\Component\Yaml\Parser as YamlParser;
23
24
class OverblogGraphQLTypesExtension extends Extension
25
{
26
    private $yamlParser;
27
28 15
    public function load(array $configs, ContainerBuilder $container)
29
    {
30 15
        $configuration = $this->getConfiguration($configs, $container);
31 15
        $config = $this->processConfiguration($configuration, $configs);
32
33 9
        $container->setParameter('overblog_graphql_types.config', $config);
34 9
    }
35
36 10
    public function containerPrependExtensionConfig(array $config, ContainerBuilder $container)
37
    {
38 10
        $typesMappings = array_merge(
39 10
            $this->typesConfigsMappingFromConfig($config, $container),
40 10
            $this->typesConfigsMappingFromBundles($container)
41 10
        );
42
43
        // treats mappings
44 10
        foreach ($typesMappings as $params) {
45 10
            $this->prependExtensionConfigFromFiles($params['type'], $params['files'], $container);
46 8
        }
47 8
    }
48
49
    /**
50
     * @param $type
51
     * @param SplFileInfo[]    $files
52
     * @param ContainerBuilder $container
53
     */
54 10
    private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder $container)
55
    {
56
        /** @var SplFileInfo $file */
57 10
        foreach ($files as $file) {
58 10
            $typeConfig = 'yml' === $type ? $this->typesConfigFromYml($file, $container) : $this->typesConfigFromXml($file, $container);
59 8
            $container->prependExtensionConfig($this->getAlias(), $typeConfig);
60 8
        }
61 8
    }
62
63 2
    private function typesConfigFromXml(SplFileInfo $file, ContainerBuilder $container)
64
    {
65 2
        $typesConfig = [];
66
67
        try {
68
            //@todo fix xml validateSchema
69 2
            $xml = XmlUtils::loadFile($file->getRealPath());
70 1
            foreach ($xml->documentElement->childNodes as $node) {
71 1
                if (!$node instanceof \DOMElement) {
72 1
                    continue;
73
                }
74 1
                $values = XmlUtils::convertDomElementToArray($node);
75 1
                $typesConfig = array_merge($typesConfig, $values);
76 1
            }
77 1
            $container->addResource(new FileResource($file->getRealPath()));
78 2
        } catch (\InvalidArgumentException $e) {
79 1
            throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
80
        }
81
82 1
        return $typesConfig;
83
    }
84
85 9
    private function typesConfigFromYml(SplFileInfo $file, ContainerBuilder $container)
86
    {
87 9
        if (null === $this->yamlParser) {
88 9
            $this->yamlParser = new YamlParser();
89 9
        }
90
91
        try {
92 9
            $typesConfig = $this->yamlParser->parse($file->getContents());
93 8
            $container->addResource(new FileResource($file->getRealPath()));
94 9
        } catch (ParseException $e) {
95 1
            throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
96
        }
97
98 8
        return $typesConfig;
99
    }
100
101 10
    private function typesConfigsMappingFromConfig(array $config, ContainerBuilder $container)
102
    {
103 10
        $typesMappings = [];
104
        // from config
105 10
        if (!empty($config['definitions']['mappings']['types'])) {
106 9
            $typesMappings = array_filter(array_map(
107 9
                function (array $typeMapping) use ($container) {
108
109 9
                    $params = $this->detectConfigFiles($container, $typeMapping['dir'],  $typeMapping['type']);
110
111 9
                    return $params;
112 9
                },
113 9
                $config['definitions']['mappings']['types']
114 9
            ));
115 9
        }
116
117 10
        return $typesMappings;
118
    }
119
120 10
    private function typesConfigsMappingFromBundles(ContainerBuilder $container)
121 1
    {
122 10
        $typesMappings = [];
123 10
        $bundles = $container->getParameter('kernel.bundles');
124
125
        // auto detect from bundle
126 10
        foreach ($bundles as $name => $class) {
127 8
            $bundle = new \ReflectionClass($class);
128 8
            $bundleDir = dirname($bundle->getFileName());
129
130 8
            $configPath = $bundleDir.'/'.$this->getMappingResourceConfigDirectory();
131 8
            $params = $this->detectConfigFiles($container, $configPath);
132
133 8
            if (null !== $params) {
134 8
                $typesMappings[] = $params;
135 8
            }
136 10
        }
137
138 10
        return $typesMappings;
139
    }
140
141 10
    private function detectConfigFiles(ContainerBuilder $container, $configPath, $type = null)
142
    {
143
        // add the closest existing directory as a resource
144 10
        $resource = $configPath;
145 10
        while (!is_dir($resource)) {
146 8
            $resource = dirname($resource);
147 8
        }
148 10
        $container->addResource(new FileResource($resource));
149
150 10
        $extension = $this->getMappingResourceExtension();
151 10
        $finder = new Finder();
152
153 10
        $types = null === $type ? ['yml', 'xml'] : [$type];
154
155 10
        foreach ($types as $type) {
156
            try {
157 10
                $finder->files()->in($configPath)->name('*.'.$extension.'.'.$type);
158 10
            } catch (\InvalidArgumentException $e) {
159 8
                continue;
160
            }
161 10
            if ($finder->count() > 0) {
162
                return [
163 10
                    'type' => $type,
164 10
                    'files' => $finder,
165 10
                ];
166
            }
167 8
        }
168
169 8
        return;
170
    }
171
172 8
    private function getMappingResourceConfigDirectory()
173
    {
174 8
        return 'Resources/config/graphql';
175
    }
176
177 10
    private function getMappingResourceExtension()
178
    {
179 10
        return 'types';
180
    }
181
182 8
    public function getAliasPrefix()
183
    {
184 8
        return 'overblog_graphql';
185
    }
186
187 8
    public function getAlias()
188
    {
189 8
        return $this->getAliasPrefix().'_types';
190
    }
191
192 15
    public function getConfiguration(array $config, ContainerBuilder $container)
193
    {
194 15
        return new TypesConfiguration();
195
    }
196
}
197