Completed
Push — symfony-3.3 ( 2453fc...dce4db )
by Kamil
21:24
created

TranslatorResourceProviderPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 2
B extractResourcesFilesFromSymfonyTranslator() 0 23 6
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
        try {
52
            $options = $symfonyTranslator->getArgument(3);
53
54
            if (!array_key_exists('resource_files', $options)) {
55
                $options = $symfonyTranslator->getArgument(4);
56
            }
57
        } catch (OutOfBoundsException $exception) {
58
            $options = [];
59
        }
60
61
        $languagesFiles = isset($options['resource_files']) ? $options['resource_files'] : [];
62
63
        $resourceFiles = [];
64
        foreach ($languagesFiles as $language => $files) {
65
            foreach ($files as $file) {
66
                $resourceFiles[] = $file;
67
            }
68
        }
69
70
        return $resourceFiles;
71
    }
72
}
73