CacheExtension   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 19.86 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.65%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 28
loc 141
ccs 63
cts 68
cp 0.9265
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 36 6
A findServiceIds() 0 10 4
A verifyDoctrineBridgeExists() 0 11 2
A getAlias() 0 4 1
B registerServices() 28 43 6

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
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\DependencyInjection;
13
14
use Cache\Bridge\Doctrine\DoctrineCacheBridge;
15
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
16
use Cache\CacheBundle\Command\CacheFlushCommand;
17
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
18
use Cache\CacheBundle\Factory\RouterFactory;
19
use Cache\CacheBundle\Factory\SessionHandlerFactory;
20
use Cache\CacheBundle\Factory\ValidationFactory;
21
use Cache\CacheBundle\Routing\CachingRouter;
22
use Cache\SessionHandler\Psr6SessionHandler;
23
use Symfony\Component\Config\FileLocator;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Loader;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
28
29
/**
30
 * @author Aaron Scherer <[email protected]>
31
 * @author Tobias Nyholm <[email protected]>
32
 */
33
class CacheExtension extends Extension
34
{
35
    /**
36
     * Loads the configs for Cache and puts data into the container.
37
     *
38
     * @param array            $configs   Array of configs
39
     * @param ContainerBuilder $container Container Object
40
     */
41 2
    public function load(array $configs, ContainerBuilder $container)
42
    {
43 2
        $config = $this->processConfiguration(new Configuration(), $configs);
44
45 2
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
47
        // Make sure config values are in the parameters
48 2
        foreach (['router', 'session', 'doctrine', 'logging', 'annotation', 'serializer', 'validation'] as $section) {
49 2
            if ($config[$section]['enabled']) {
50 2
                $container->setParameter('cache.'.$section, $config[$section]);
51
            }
52
        }
53
54 2
        if ($config['doctrine']['enabled']) {
55
            $this->verifyDoctrineBridgeExists('doctrine');
56
        }
57
58 2
        $this->registerServices($container, $config);
59
60
        // Add toolbar and data collector if we are debugging
61 2
        if (!isset($config['data_collector']['enabled'])) {
62 2
            $config['data_collector']['enabled'] = $container->getParameter('kernel.debug');
63
        }
64
65 2
        if ($config['data_collector']['enabled']) {
66 1
            $loader->load('data-collector.yml');
67
        }
68
69
        // Get a list of the psr-6 services we are using.
70 2
        $serviceIds = [];
71 2
        $this->findServiceIds($config, $serviceIds);
72 2
        $container->setParameter('cache.provider_service_ids', $serviceIds);
73
74 2
        $container->register(CacheFlushCommand::class, CacheFlushCommand::class)
75 2
            ->addTag('console.command', ['command' => 'cache:flush']);
76 2
    }
77
78
    /**
79
     * Find service ids that we configured. These services should be tagged so we can use them in the debug toolbar.
80
     *
81
     * @param array $config
82
     * @param array $serviceIds
83
     */
84 2
    protected function findServiceIds(array $config, array &$serviceIds)
85
    {
86 2
        foreach ($config as $name => $value) {
87 2
            if (is_array($value)) {
88 2
                $this->findServiceIds($value, $serviceIds);
89 2
            } elseif ($name === 'service_id') {
90 2
                $serviceIds[] = $value;
91
            }
92
        }
93 2
    }
94
95
    /**
96
     * Make sure the DoctrineBridge is installed.
97
     *
98
     * @param string $name
99
     *
100
     * @throws \Exception
101
     */
102 1
    private function verifyDoctrineBridgeExists($name)
103
    {
104 1
        if (!class_exists('Cache\Bridge\Doctrine\DoctrineCacheBridge')) {
105
            throw new \Exception(
106
                sprintf(
107
                    'You need the DoctrineCacheBridge to be able to use "%s". Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.',
108
                    $name
109
                )
110
            );
111
        }
112 1
    }
113
114
    /**
115
     * @return string
116
     */
117 2
    public function getAlias()
118
    {
119 2
        return 'cache';
120
    }
121
122
    /**
123
     * Register services. All service ids will start witn "cache.service.".
124
     *
125
     * @param ContainerBuilder $container
126
     * @param                  $config
127
     *
128
     * @throws \Exception
129
     */
130 2
    private function registerServices(ContainerBuilder $container, $config)
131
    {
132 2 View Code Duplication
        if ($config['annotation']['enabled']) {
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...
133 1
            $this->verifyDoctrineBridgeExists('annotation');
134 1
            $container->register('cache.service.annotation', DoctrineCacheBridge::class)
135 1
                ->setFactory([DoctrineBridgeFactory::class, 'get'])
136 1
                ->addArgument(new Reference($config['annotation']['service_id']))
137 1
                ->addArgument($config['annotation'])
138 1
                ->addArgument(['annotation']);
139
        }
140
141 2 View Code Duplication
        if ($config['serializer']['enabled']) {
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...
142 1
            $this->verifyDoctrineBridgeExists('serializer');
143 1
            $container->register('cache.service.serializer', DoctrineCacheBridge::class)
144 1
                ->setFactory([DoctrineBridgeFactory::class, 'get'])
145 1
                ->addArgument(new Reference($config['serializer']['service_id']))
146 1
                ->addArgument($config['serializer'])
147 1
                ->addArgument(['serializer']);
148
        }
149
150 2 View Code Duplication
        if ($config['validation']['enabled']) {
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...
151 1
            $container->register('cache.service.validation', SymfonyValidatorBridge::class)
152 1
                ->setFactory([ValidationFactory::class, 'get'])
153 1
                ->addArgument(new Reference($config['validation']['service_id']))
154 1
                ->addArgument($config['validation']);
155
        }
156
157 2 View Code Duplication
        if ($config['session']['enabled']) {
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...
158 1
            $container->register('cache.service.session', Psr6SessionHandler::class)
159 1
                ->setFactory([SessionHandlerFactory::class, 'get'])
160 1
                ->addArgument(new Reference($config['session']['service_id']))
161 1
                ->addArgument($config['session']);
162
        }
163
164 2
        if ($config['router']['enabled']) {
165 2
            $container->register('cache.service.router', CachingRouter::class)
166 2
                ->setFactory([RouterFactory::class, 'get'])
167 2
                ->setDecoratedService('router', null, 10)
168 2
                ->addArgument(new Reference($config['router']['service_id']))
169 2
                ->addArgument(new Reference('cache.service.router.inner'))
170 2
                ->addArgument($config['router']);
171
        }
172 2
    }
173
}
174