InMemoryDataLoadCompilerPass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
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