Completed
Push — develop ( bd5627...a32047 )
by Jens
02:58
created

HandlebarsExtension   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 111
Duplicated Lines 9.91 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 19
c 7
b 1
f 3
lcom 1
cbo 8
dl 11
loc 111
ccs 65
cts 65
cp 1
rs 10

6 Methods

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