Completed
Push — pull-request/8125 ( 30dc5f...56d65c )
by Kamil
22:36
created

extractResourcesFilesFromSymfonyTranslator()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 8.6737
c 2
b 0
f 0
cc 6
eloc 13
nc 18
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ThemeBundle\Translation\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
final class TranslatorResourceProviderPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        try {
30
            $symfonyTranslator = $container->findDefinition('translator.default');
31
            $syliusResourceProvider = $container->findDefinition('sylius.theme.translation.resource_provider.default');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $syliusResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
32
        } catch (\InvalidArgumentException $exception) {
33
            return;
34
        }
35
36
        $symfonyResourcesFiles = $this->extractResourcesFilesFromSymfonyTranslator($symfonyTranslator);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $symfonyResourcesFiles exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
37
38
        $syliusResourceProvider->replaceArgument(0, array_merge(
39
            $syliusResourceProvider->getArgument(0),
40
            $symfonyResourcesFiles
41
        ));
42
    }
43
44
    /**
45
     * @param Definition $symfonyTranslator
46
     *
47
     * @return array
48
     */
49
    private function extractResourcesFilesFromSymfonyTranslator(Definition $symfonyTranslator)
50
    {
51
        $options = $symfonyTranslator->getArgument(3);
52
        if (!array_key_exists('resource_files', $options)) {
53
            try {
54
                $options = $symfonyTranslator->getArgument(4);
55
            } catch (OutOfBoundsException $exception) {
56
                $options = [];
57
            }
58
        }
59
60
        $languagesFiles = isset($options['resource_files']) ? $options['resource_files'] : [];
61
62
        $resourceFiles = [];
63
        foreach ($languagesFiles as $language => $files) {
64
            foreach ($files as $file) {
65
                $resourceFiles[] = $file;
66
            }
67
        }
68
69
        return $resourceFiles;
70
    }
71
}
72