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

OverblogGraphQLTypesExtension   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 0
loc 169
ccs 73
cts 73
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 7 1
A containerPrependExtensionConfig() 0 9 2
A prependExtensionConfigFromFiles() 0 10 2
B mappingConfig() 0 28 4
A detectFilesFromTypesMappings() 0 16 2
A mappingFromBundles() 0 15 2
B detectFilesByType() 0 29 6
A bundleDir() 0 7 1
A getAliasPrefix() 0 4 1
A getAlias() 0 4 1
A getConfiguration() 0 4 1
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 Overblog\GraphQLBundle\OverblogGraphQLBundle;
15
use Symfony\Component\Config\Resource\FileResource;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\Finder\SplFileInfo;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
21
class OverblogGraphQLTypesExtension extends Extension
22
{
23
    private static $configTypes = ['yaml', 'xml'];
24
25
    private static $typeExtensions = ['yaml' => '{yaml,yml}', 'xml' => 'xml'];
26
27
    private static $defaultDefaultConfig = [
28
        'definitions' => [
29
            'mappings' => [
30
                'auto_discover' => [
31
                    'root_dir' => true,
32
                    'bundles' => true,
33
                ],
34
                'types' => [],
35
            ],
36
        ],
37
    ];
38
39
    const DEFAULT_TYPES_SUFFIX = '.types';
40
41 25
    public function load(array $configs, ContainerBuilder $container)
42
    {
43 25
        $configuration = $this->getConfiguration($configs, $container);
44 25
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 43 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
45
46 19
        $container->setParameter($this->getAlias().'.config', $config);
47 19
    }
48
49 20
    public function containerPrependExtensionConfig(array $config, ContainerBuilder $container)
50
    {
51 20
        $typesMappings = $this->mappingConfig($config, $container);
52
53
        // treats mappings
54 20
        foreach ($typesMappings as $params) {
55 20
            $this->prependExtensionConfigFromFiles($params['type'], $params['files'], $container);
56
        }
57 18
    }
58
59
    /**
60
     * @param $type
61
     * @param SplFileInfo[]    $files
62
     * @param ContainerBuilder $container
63
     */
64 20
    private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder $container)
65
    {
66
        /** @var SplFileInfo $file */
67 20
        foreach ($files as $file) {
68 20
            $parserClass = sprintf('Overblog\\GraphQLBundle\\Config\\Parser\\%sParser', ucfirst($type));
69
70 20
            $typeConfig = call_user_func($parserClass.'::parse', $file, $container);
71 18
            $container->prependExtensionConfig($this->getAlias(), $typeConfig);
72
        }
73 18
    }
74
75 20
    private function mappingConfig(array $config, ContainerBuilder $container)
76
    {
77
        // use default value if needed
78 20
        $config = array_replace_recursive(self::$defaultDefaultConfig, $config);
79
80 20
        $mappingConfig = $config['definitions']['mappings'];
81 20
        $typesMappings = $mappingConfig['types'];
82
83
        // app only config files (yml or xml)
84 20
        if ($mappingConfig['auto_discover']['root_dir'] && $container->hasParameter('kernel.root_dir')) {
85 1
            $typesMappings[] = ['dir' => $container->getParameter('kernel.root_dir').'/config/graphql', 'type' => null];
86
        }
87 20
        if ($mappingConfig['auto_discover']['bundles']) {
88 3
            $mappingFromBundles = $this->mappingFromBundles($container);
89 3
            $typesMappings = array_merge($typesMappings, $mappingFromBundles);
90
        } else {
91
            // enabled only for this bundle
92 17
            $typesMappings[] = [
93 17
                'dir' => $this->bundleDir(OverblogGraphQLBundle::class).'/Resources/config/graphql',
94 17
                'type' => 'yaml',
95
            ];
96
        }
97
98
        // from config
99 20
        $typesMappings = $this->detectFilesFromTypesMappings($typesMappings, $container);
100
101 20
        return $typesMappings;
102
    }
103
104 20
    private function detectFilesFromTypesMappings(array $typesMappings, ContainerBuilder $container)
105
    {
106 20
        return array_filter(array_map(
107 20
            function (array $typeMapping) use ($container) {
108 20
                $params = $this->detectFilesByType(
109 20
                    $container,
110 20
                    $typeMapping['dir'],
111 20
                    $typeMapping['type'],
112 20
                    isset($typeMapping['suffix']) ? $typeMapping['suffix'] : ''
113
                );
114
115 20
                return $params;
116 20
            },
117 20
            $typesMappings
118
        ));
119
    }
120
121 3
    private function mappingFromBundles(ContainerBuilder $container)
122
    {
123 3
        $typesMappings = [];
124 3
        $bundles = $container->getParameter('kernel.bundles');
125
126
        // auto detect from bundle
127 3
        foreach ($bundles as $name => $class) {
128 1
            $bundleDir = $this->bundleDir($class);
129
130
            // only config files (yml or xml)
131 1
            $typesMappings[] = ['dir' => $bundleDir.'/Resources/config/graphql', 'type' => null];
132
        }
133
134 3
        return $typesMappings;
135
    }
136
137 20
    private function detectFilesByType(ContainerBuilder $container, $path, $type, $suffix)
138
    {
139
        // add the closest existing directory as a resource
140 20
        $resource = $path;
141 20
        while (!is_dir($resource)) {
142 1
            $resource = dirname($resource);
143
        }
144 20
        $container->addResource(new FileResource($resource));
145
146 20
        $finder = new Finder();
147
148 20
        $types = null === $type ? self::$configTypes : [$type];
149
150 20
        foreach ($types as $type) {
151
            try {
152 20
                $finder->files()->in($path)->name('*'.$suffix.'.'.self::$typeExtensions[$type]);
153 1
            } catch (\InvalidArgumentException $e) {
154 1
                continue;
155
            }
156 20
            if ($finder->count() > 0) {
157
                return [
158 20
                    'type' => $type,
159 20
                    'files' => $finder,
160
                ];
161
            }
162
        }
163
164 1
        return;
165
    }
166
167 18
    private function bundleDir($bundleClass)
168
    {
169 18
        $bundle = new \ReflectionClass($bundleClass);
170 18
        $bundleDir = dirname($bundle->getFileName());
171
172 18
        return $bundleDir;
173
    }
174
175 19
    public function getAliasPrefix()
176
    {
177 19
        return 'overblog_graphql';
178
    }
179
180 19
    public function getAlias()
181
    {
182 19
        return $this->getAliasPrefix().'_types';
183
    }
184
185 25
    public function getConfiguration(array $config, ContainerBuilder $container)
186
    {
187 25
        return new TypesConfiguration();
188
    }
189
}
190