Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

TypeAutoLoader::autoloadTypes()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
ccs 23
cts 23
cp 1
rs 4.8196
cc 10
eloc 23
nc 10
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Type\Loader;
12
13
use GraphQL\Type\Definition\Type;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\Finder\Finder;
17
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
18
19
/**
20
 * Class TypeAutoLoader
21
 */
22
class TypeAutoLoader implements ContainerAwareInterface
23
{
24
    use ContainerAwareTrait;
25
26
    protected static $loaded = false;
27
28
    /**
29
     * Autoload all registered types
30
     */
31 21
    public function autoloadTypes()
32
    {
33 21
        if (self::$loaded) {
34 20
            return;
35
        }
36
37 1
        self::$loaded = true;
38 1
        $bundles = $this->container->get('kernel')->getBundles();
39
40
        //TODO: save in cache for production
41
42 1
        foreach ($bundles as $bundle) {
43 1
            $path = $bundle->getPath().'/Type';
44 1
            if (file_exists($path)) {
45 1
                $finder = new Finder();
46 1
                foreach ($finder->in($path)->name('/Type.php$/')->getIterator() as $file) {
47 1
                    $namespace = $bundle->getNamespace();
48 1
                    $className = preg_replace('/.php$/', null, $file->getFilename());
49 1
                    $name = preg_replace('/Type$/', null, $className);
50
51 1
                    if ($file->getRelativePath()) {
52 1
                        $subNamespace = str_replace('/', '\\', $file->getRelativePath());
53 1
                        $fullyClassName = $namespace.'\\Type\\'.$subNamespace.'\\'.$className;
54
                    } else {
55 1
                        $fullyClassName = $namespace.'\\Type\\'.$className;
56
                    }
57
58 1
                    if (class_exists($fullyClassName)) {
59 1
                        $ref = new \ReflectionClass($fullyClassName);
60 1
                        if ($ref->isSubclassOf(Type::class)
61 1
                            && $ref->isInstantiable()
62 1
                            && !$ref->getConstructor()->getNumberOfRequiredParameters()
63
                        ) {
64 1
                            TypeRegistry::addTypeMapping($name, $fullyClassName);
65
                        }
66
                    }
67
                }
68
            }
69
        }
70 1
    }
71
}
72