Completed
Push — master ( 08c162...c8025b )
by Warnar
22:18 queued 19:38
created

createLoaderRepository()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 8.8054

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 64
ccs 33
cts 43
cp 0.7674
rs 6.8233
cc 8
eloc 46
nc 15
nop 4
crap 8.8054

How to fix   Long Method   

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
namespace Boekkooi\Bundle\TwigJackBundle\DependencyInjection;
3
4
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
5
use Symfony\Component\Config\Definition\Processor;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\Config\Loader\LoaderInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\DefinitionDecorator;
10
use Symfony\Component\DependencyInjection\Loader;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
14
/**
15
 * @author Warnar Boekkooi <[email protected]>
16
 */
17
class BoekkooiTwigJackExtension extends Extension
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 15
    public function load(array $config, ContainerBuilder $container)
23
    {
24 15
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
25
26 15
        $processor = new Processor();
27 15
        $config = $processor->processConfiguration(new Configuration(), $config);
28
29 12
        $this->loadDefer($container, $loader, $config);
30 12
        $this->loadConstraint($container, $loader, $config);
31 12
        $this->loadExclamation($container, $loader, $config);
32
33 12
        $this->loadLoaders($container, $loader, $config);
34 11
    }
35
36 12
    private function loadLoaders(ContainerBuilder $container, LoaderInterface $configLoader, array $config)
37
    {
38 12
        if (empty($config['loaders'])) {
39 6
            return;
40
        }
41
42 6
        $configLoader->load('loader.yml');
43 6
        $useFactoryMethod = method_exists($container->getDefinition('boekkooi.twig_jack.loader.abstract'), 'setFactory');
44
45 6
        foreach ($config['loaders'] as $name => $loader) {
46 6
            $this->setupLoader($container, $name, $loader, $useFactoryMethod);
47 5
        }
48 5
    }
49
50 6
    private function setupLoader(ContainerBuilder $container, $name, array $loaderConfig, $useFactoryMethod)
51
    {
52 6
        $repositoryService = $this->createLoaderRepository($container, $name, $loaderConfig, $useFactoryMethod);
53
54
        // Create loader
55 5
        $loader = $container->setDefinition(sprintf('boekkooi.twig_jack.loaders.%s', $name), new DefinitionDecorator('boekkooi.twig_jack.loader.abstract'));
56 5
        $loader->setPublic(true);
57 5
        $loader->addTag('twig.loader');
58
59
        $loader
60 5
            ->replaceArgument(0, new Reference($repositoryService))
61 5
            ->replaceArgument(1, (string) $loaderConfig['prefix'])
62 5
            ->replaceArgument(2, !empty($loaderConfig['locale_callable']) ? new Reference($loaderConfig['locale_callable']) : null);
63 5
    }
64
65 6
    private function createLoaderRepository(ContainerBuilder $container, $loaderName, array $loaderConfig, $useFactoryMethod)
66
    {
67 6
        switch ($loaderConfig['type']) {
68 6
            case 'custom':
69 2
                if (empty($loaderConfig['repository'])) {
70 1
                    throw new InvalidConfigurationException(sprintf('No repository option provided for %s', $loaderName));
71
                }
72
73 1
                return $loaderConfig['repository'];
74 4
            case 'orm':
75 2
                $managerService = 'doctrine';
76 2
                break;
77 2
            case 'mongo':
78 1
                $managerService = 'doctrine_mongodb';
79 1
                break;
80 1
            case 'couch':
81 1
                $managerService = 'doctrine_couchdb';
82 1
                break;
83
            // @codeCoverageIgnoreStart
84
            default:
85
                throw new InvalidConfigurationException(sprintf('Unknown loader type provided for %s', $loaderName));
86
            // @codeCoverageIgnoreEnd
87 4
        }
88
89 4
        $repositoryService = sprintf('boekkooi.twig_jack.loaders.%s.object_repository', $loaderName);
90 4
        $entityManagerService = sprintf('boekkooi.twig_jack.loaders.%s.object_manager', $loaderName);
91
92 4
        $modelClass = ltrim($loaderConfig['model_class'], '\\');
93
94
        // Create factory to get the entity manager for the entity
95
        $container
96 4
            ->setDefinition($entityManagerService, new DefinitionDecorator('boekkooi.twig_jack.doctrine.object_manager.abstract'))
97 4
            ->setPublic(true)
98 4
            ->setArguments(array($modelClass));
99 4
        if ($useFactoryMethod) {
100
            $container->getDefinition($entityManagerService)
101
                ->setFactory(array(
102
                    new Reference($managerService),
103
                    'getManagerForClass'
104
                ));
105
        } else {
106 4
            $container->getDefinition($entityManagerService)
0 ignored issues
show
Bug introduced by
The method setFactoryService() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean setFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
107 4
                ->setFactoryService($managerService)
108 4
                ->setFactoryMethod('getManagerForClass');
109
        }
110
111
        // Create factory to get the repository for the entity
112
        $container
113 4
            ->setDefinition($repositoryService, new DefinitionDecorator('boekkooi.twig_jack.doctrine.object_repository.abstract'))
114 4
            ->setArguments(array($modelClass));
115 4
        if ($useFactoryMethod) {
116
            $container->getDefinition($repositoryService)
117
                ->setFactory(array(
118
                    new Reference($entityManagerService),
119
                    'getRepository'
120
                ));
121
        } else {
122 4
            $container->getDefinition($repositoryService)
0 ignored issues
show
Bug introduced by
The method setFactoryService() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean setFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
123 4
                ->setFactoryService($entityManagerService)
124 4
                ->setFactoryMethod('getRepository');
125
        }
126
127 4
        return $repositoryService;
128
    }
129
130 12
    private function loadDefer(ContainerBuilder $container, LoaderInterface $loader, array $config)
131
    {
132 12
        if (!$config['defer']['enabled']) {
133 1
            return;
134
        }
135
136 11
        $container->setParameter('boekkooi.twig_jack.defer.prefix', $config['defer']['prefix']);
137 11
        $loader->load('defer.yml');
138 11
    }
139
140 12
    private function loadConstraint(ContainerBuilder $container, LoaderInterface $loader, array $config)
141
    {
142 12
        if (!$config['constraint']['enabled']) {
143 10
            return;
144
        }
145
146 2
        $loader->load('constraint.yml');
147 2
        $container->getDefinition('boekkooi.twig_jack.constraint_validator')
148 2
            ->replaceArgument(0, new Reference($config['constraint']['environment']));
149 2
    }
150
151
    /**
152
     * @param array $config
153
     * @param ContainerBuilder $container
154
     * @param $loader
155
     */
156 12
    public function loadExclamation(ContainerBuilder $container, $loader, array $config)
157
    {
158 12
        if (!$config['exclamation']) {
159 2
            return;
160
        }
161
162 10
        $loader->load('exclamation.yml');
163
164 10
        $container->getDefinition('templating.name_parser')
165 10
            ->setClass('%templating.name_parser.class%');
166
167 10
        $container->getDefinition('templating.cache_warmer.template_paths')
168 10
            ->setClass('%templating.cache_warmer.template_paths.class%');
169
170 10
        $container->getDefinition('assetic.twig_formula_loader')
171 10
            ->setClass('%assetic.twig_formula_loader.class%');
172
173
174 10
        $container->setParameter('boekkooi.twig_jack.exclamation', true);
175 10
    }
176
}
177