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

HandlebarsExtension::load()   F

Complexity

Conditions 14
Paths 960

Size

Total Lines 82
Code Lines 50

Duplication

Lines 11
Ratio 13.41 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 5
Bugs 1 Features 3
Metric Value
c 5
b 1
f 3
dl 11
loc 82
ccs 0
cts 65
cp 0
rs 2.439
cc 14
eloc 50
nc 960
nop 2
crap 210

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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