Completed
Push — develop ( ab0bf6...a87c7d )
by Jens
03:28
created

HandlebarsExtension   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 129
Duplicated Lines 8.53 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 11
loc 129
ccs 75
cts 75
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 27 3
B getFlags() 11 23 5
A setupAssetic() 0 13 3
A configurePath() 0 5 1
B addContainerPath() 0 26 4
A normalizeBundleNameForLoaderNamespace() 0 8 2
A addConfigPath() 0 18 4

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\Loader\LoaderInterface;
12
use Symfony\Component\Config\Resource\FileExistenceResource;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17
18
class HandlebarsExtension extends Extension
19
{
20 7
    public function load(array $configs, ContainerBuilder $container)
21
    {
22 7
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
23 7
        $loader->load('handlebars.xml');
24
25 7
        $configuration = new Configuration();
26
27 7
        $config = $this->processConfiguration($configuration, $configs);
28
29 7
        $config['flags'] = $this->getFlags($config);
30
31
32 7
        foreach ($config['translation'] as $key => $value) {
33 7
            $container->setParameter('handlebars.translation.'.$key, $value);
34
        }
35 7
        foreach ($config['cms'] as $key => $value) {
36 7
            $container->setParameter('handlebars.cms.'.$key, $value);
37
        }
38
39 7
        $this->setupAssetic($loader, $config, $container);
40 7
        $this->configurePath($config, $container);
41
42 7
        $container->getDefinition('handlebars.cache')->replaceArgument(0, $config['cache']);
43 7
        $container->getDefinition('handlebars.cache')->replaceArgument(1, $config['debug']);
44
45 7
        $container->getDefinition('handlebars')->replaceArgument(2, $config);
46 7
    }
47
48 7
    private function getFlags($config) {
49 7
        $flags = 0;
50 7 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...
51 7
            foreach ($config['flags'] as $flag) {
52 2
                $flags = $flags | constant('LightnCandy\LightnCandy::'.$flag);
53
            }
54
        }
55 7 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...
56 7
            foreach ($config['excludeFlags'] as $flag) {
57 1
                $flags = $flags & ~constant('LightnCandy\LightnCandy::'.$flag);
58
            }
59 7
            unset($config['excludeFlags']);
60
        }
61
        // ensure base functionality with flag standalone disabled
62 7
        $flags = ($flags | LightnCandy::FLAG_BESTPERFORMANCE |
63 7
                LightnCandy::FLAG_HANDLEBARSJS |
64 7
                LightnCandy::FLAG_RUNTIMEPARTIAL |
65 7
                LightnCandy::FLAG_HANDLEBARSLAMBDA |
66 7
                LightnCandy::FLAG_EXTHELPER |
67 7
                LightnCandy::FLAG_ERROR_EXCEPTION) & ~LightnCandy::FLAG_STANDALONEPHP;
68
69 7
        return $flags;
70
    }
71
72 7
    private function setupAssetic(LoaderInterface $loader, $config, ContainerBuilder $container)
73
    {
74
        // Enable AsseticExtension if undefined
75 7
        if (!isset($config['assetic'])) {
76 6
            $config['assetic'] = array_key_exists('AsseticBundle', $container->getParameter('kernel.bundles'));
77
        }
78
        // Assetic Extension
79 7
        if (true === $config['assetic']) {
80 1
            $loader->load('assetic.xml');
81
        }
82 7
        $container->setParameter('handlebars.assetic', $config['assetic']);
83
84 7
    }
85
86 7
    private function configurePath($config, ContainerBuilder $container)
87
    {
88 7
        $this->addConfigPath($config, $container);
89 7
        $this->addContainerPath($container);
90 7
    }
91
92 7
    private function addContainerPath(ContainerBuilder $container)
93
    {
94 7
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
95
96
        // register bundles as Handlebars namespaces
97 7
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
98 2
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
99 2
            if (is_dir($dir)) {
100 2
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array(
101 2
                    $dir,
102 2
                    $this->normalizeBundleNameForLoaderNamespace($bundle)
103
                ));
104
            }
105 2
            $container->addResource(new FileExistenceResource($dir));
106
107 2
            $reflection = new \ReflectionClass($class);
108 2
            $dir = dirname($reflection->getFileName()).'/Resources/views';
109 2
            if (is_dir($dir)) {
110 2
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array(
111 2
                    $dir,
112 2
                    $this->normalizeBundleNameForLoaderNamespace($bundle)
113
                ));
114
            }
115 2
            $container->addResource(new FileExistenceResource($dir));
116
        }
117 7
    }
118
119 2
    private function normalizeBundleNameForLoaderNamespace($bundle)
120
    {
121 2
        if ('Bundle' === substr($bundle, -6)) {
122 2
            $bundle = substr($bundle, 0, -6);
123
        }
124
125 2
        return $bundle;
126
    }
127
128 7
    private function addConfigPath($config, ContainerBuilder $container)
129
    {
130 7
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
131
132
        // register user-configured paths
133 7
        foreach ($config['paths'] as $path => $namespace) {
134 1
            if (!$namespace) {
135 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
136
            } else {
137 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
138
            }
139
        }
140 7
        $dir = $container->getParameter('kernel.root_dir').'/Resources/views';
141 7
        if (is_dir($dir)) {
142 7
            $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
143
        }
144 7
        $container->addResource(new FileExistenceResource($dir));
145 7
    }
146
}
147