InjectEntityDefinitionsPass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 64
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B computeConfig() 0 29 5
A process() 0 18 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\DependencyInjection\Compiler;
5
6
use Innmind\Neo4jBundle\Exception\NoEntityDefinitionFoundException;
7
use Innmind\Filesystem\{
8
    Adapter\FilesystemAdapter,
9
    Exception\FileNotFound,
10
    Directory
11
};
12
use Symfony\Component\DependencyInjection\{
13
    Compiler\CompilerPassInterface,
14
    ContainerBuilder
15
};
16
use Symfony\Component\Yaml\Yaml;
17
18
final class InjectEntityDefinitionsPass implements CompilerPassInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    public function process(ContainerBuilder $container)
24
    {
25 4
        $bundles = $container->getParameter('kernel.bundles');
26 4
        $configs = [];
27
28 4
        foreach ($bundles as $bundle => $class) {
29
            try {
30 4
                $configs[] = $this->computeConfig($class);
31 4
            } catch (NoEntityDefinitionFoundException $e) {
32
                //pass
33
            }
34
        }
35
36
        $container
37 4
            ->getDefinition('innmind_neo4j.metadata_builder')
38 4
            ->addMethodCall(
39 4
                'inject',
40 4
                [$configs]
41
            );
42 4
    }
43
44
    /**
45
     * Load the entity definitions for the given bundle
46
     *
47
     * @param string $class Bundle class FQCN
48
     *
49
     * @throws NoEntityDefinitionFoundException
50
     *
51
     * @return array
52
     */
53 4
    private function computeConfig(string $class): array
54
    {
55
        try {
56 4
            $refl = new \ReflectionClass($class);
57 4
            $dir = (new FilesystemAdapter(dirname($refl->getFileName())))
58 4
                ->get('Resources')
59 4
                ->get('config');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Innmind\Filesystem\File\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                ->/** @scrutinizer ignore-call */ get('config');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61 4
            if ($dir->has('neo4j.yml')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on Innmind\Filesystem\File. It seems like you code against a sub-type of Innmind\Filesystem\File such as Innmind\Filesystem\Directory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            if ($dir->/** @scrutinizer ignore-call */ has('neo4j.yml')) {
Loading history...
62 4
                return Yaml::parse((string) $dir->get('neo4j.yml')->content());
0 ignored issues
show
Bug introduced by
The method get() does not exist on Innmind\Filesystem\File. It seems like you code against a sub-type of Innmind\Filesystem\File such as Innmind\Filesystem\Directory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                return Yaml::parse((string) $dir->/** @scrutinizer ignore-call */ get('neo4j.yml')->content());
Loading history...
63
            }
64
65 4
            $dir = $dir->get('neo4j');
66 4
            $config = [];
67
68 4
            foreach ($dir as $file) {
69 4
                if ($file instanceof Directory) {
70 4
                    continue;
71
                }
72
73 4
                $config = array_merge(
74 4
                    $config,
75 4
                    Yaml::parse((string) $file->content())
76
                );
77
            }
78
79 4
            return $config;
80 4
        } catch (FileNotFound $e) {
81 4
            throw new NoEntityDefinitionFoundException;
82
        }
83
    }
84
}
85