InMemoryDataLoadCompilerPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 65
ccs 14
cts 35
cp 0.4
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B process() 0 33 5
1
<?php
2
3
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\Config\FileLocatorInterface;
6
use Symfony\Component\Config\Resource\FileResource;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\Finder\SplFileInfo;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Compiler pass which guess and load all memory data stores.
15
 */
16
class InMemoryDataLoadCompilerPass implements CompilerPassInterface
17
{
18
    /**
19
     * @var FileLocatorInterface
20
     */
21
    protected $fileLocator;
22
23
    /**
24
     * @var OptionsResolver
25
     */
26
    protected $optionsResolver;
27
28
    /**
29
     * Construct.
30
     *
31
     * @param FileLocator $fileLocator
32
     */
33 2
    public function __construct(FileLocatorInterface $fileLocator)
34
    {
35 2
        $this->fileLocator = $fileLocator;
36 2
        $this->optionsResolver = new OptionsResolver();
37 2
        $this->optionsResolver->setDefaults(array(
38 2
            'callback' => 'registerData',
39 1
        ));
40 2
        $this->optionsResolver->setDefined('file');
41 2
        $this->optionsResolver->setDefined('parameter');
42 2
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function process(ContainerBuilder $container)
48
    {
49 2
        $inMemoryFilesTags = $container->findTaggedServiceIds('majora.loader.in_memory');
50
51 2
        foreach ($inMemoryFilesTags as $serviceId => $tags) {
52
            foreach ($tags as $attributes) {
53
                $options = $this->optionsResolver->resolve($attributes);
54
                $loaderDefinition = $container->getDefinition($serviceId);
55
56
                // File loading
57
                if (!empty($options['file'])) {
58
                    $file = new SplFileInfo(
59
                        $this->fileLocator->locate($options['file']),
0 ignored issues
show
Bug introduced by
It seems like $this->fileLocator->locate($options['file']) targeting Symfony\Component\Config...atorInterface::locate() can also be of type array; however, Symfony\Component\Finder...FileInfo::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
                        '',
61
                        ''
62
                    );
63
                    $loaderDefinition->addMethodCall($options['callback'], array(
64
                        Yaml::parse($file->getContents()),
65
                    ));
66
67
                    $container->addResource(new FileResource($file->getRealPath()));
68
                }
69
70
                // Parameter loading
71
                if (!empty($options['parameter'])) {
72
                    $loaderDefinition->addMethodCall($options['callback'], array(
73
                        $container->getParameter($options['parameter']),
74
                    ));
75
                    $container->getParameterBag()->remove($options['parameter']);
76
                }
77
            }
78 1
        }
79 2
    }
80
}
81