Completed
Push — develop ( 87f5bf...c17c7b )
by Jens
03:04
created

HandlebarsExtension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 12.94 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 14
c 5
b 1
f 3
lcom 0
cbo 7
dl 11
loc 85
ccs 0
cts 65
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F load() 11 82 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle\DependencyInjection;
8
9
use LightnCandy\LightnCandy;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\Config\Resource\FileExistenceResource;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
15
16
class HandlebarsExtension extends Extension
17
{
18
    public function load(array $configs, ContainerBuilder $container)
19
    {
20
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
21
        $loader->load('handlebars.xml');
22
23
        $configuration = new Configuration();
24
25
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
26
27
        $config = $this->processConfiguration($configuration, $configs);
28
29
        $flags = 0;
30 View Code Duplication
        if (isset($config['flags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            foreach ($config['flags'] as $flag) {
32
                $flags = $flags | constant('LightnCandy\LightnCandy::' . $flag);
33
            }
34
        }
35 View Code Duplication
        if (isset($config['excludeFlags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            foreach ($config['excludeFlags'] as $flag) {
37
                $flags = $flags & ~constant('LightnCandy\LightnCandy::' . $flag);
38
            }
39
            unset($config['excludeFlags']);
40
        }
41
        // ensure base functionality with flag standalone disabled
42
        $flags = ($flags | LightnCandy::FLAG_BESTPERFORMANCE |
43
            LightnCandy::FLAG_HANDLEBARSJS |
44
            LightnCandy::FLAG_RUNTIMEPARTIAL |
45
            LightnCandy::FLAG_HANDLEBARSLAMBDA |
46
            LightnCandy::FLAG_EXTHELPER |
47
            LightnCandy::FLAG_ERROR_EXCEPTION) & ~LightnCandy::FLAG_STANDALONEPHP;
48
49
        $config['flags'] = $flags;
50
51
        // Enable AsseticExtension if undefined
52
        if (!isset($config['assetic'])) {
53
            $config['assetic'] = array_key_exists('AsseticBundle', $container->getParameter('kernel.bundles'));
54
        }
55
        // Assetic Extension
56
        if (true === $config['assetic']) {
57
            $loader->load('assetic.xml');
58
        }
59
        $container->setParameter('handlebars.assetic', $config['assetic']);
60
61
        foreach ($config['translation'] as $key => $value) {
62
            $container->setParameter('handlebars.translation.' . $key, $value);
63
        }
64
65
        // register user-configured paths
66
        foreach ($config['paths'] as $path => $namespace) {
67
            if (!$namespace) {
68
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
69
            } else {
70
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
71
            }
72
        }
73
        $dir = $container->getParameter('kernel.root_dir').'/Resources/views';
74
        if (is_dir($dir)) {
75
            $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
76
        }
77
        $container->addResource(new FileExistenceResource($dir));
78
        
79
        // register bundles as Handlebars namespaces
80
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
81
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
82
            if (is_dir($dir)) {
83
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
84
            }
85
            $container->addResource(new FileExistenceResource($dir));
86
87
            $reflection = new \ReflectionClass($class);
88
            $dir = dirname($reflection->getFileName()).'/Resources/views';
89
            if (is_dir($dir)) {
90
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
91
            }
92
            $container->addResource(new FileExistenceResource($dir));
93
        }
94
95
        $container->getDefinition('handlebars.cache')->replaceArgument(0, $config['cache']);
96
        $container->getDefinition('handlebars.cache')->replaceArgument(1, $config['debug']);
97
98
        $container->getDefinition('handlebars')->replaceArgument(2, $config);
99
    }
100
}
101