Completed
Push — master ( ce95ca...829dc5 )
by Jacob
6s
created

MetadataCache::createCacheClearCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\Utility;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Loads the metadata cache service.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class MetadataCache implements ServiceLoaderInterface
16
{
17
    /**
18
     * Creates the bundle cache warmer definition.
19
     *
20
     * @param   string  $warmerName
21
     * @return  Definition
22
     */
23 View Code Duplication
    private function createBundleCacheWarmer($warmerName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
24
    {
25
        $definition = new Definition(
26
            Utility::getBundleClass('CacheWarmer\MetadataWarmer'),
27
            [new Reference($warmerName)]
28
        );
29
        $definition->setPublic(false);
30
        $definition->addTag('kernel.cache_warmer');
31
        return $definition;
32
    }
33
34
    /**
35
     * Creates the cache clear command definition.
36
     *
37
     * @param   string  $warmerName
38
     * @return  Definition
39
     */
40 View Code Duplication
    private function createCacheClearCommand($warmerName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        $definition = new Definition(
43
            Utility::getBundleClass('Command\Metadata\ClearCacheCommand'),
44
            [new Reference($warmerName)]
45
        );
46
        $definition->addTag('console.command');
47
        return $definition;
48
    }
49
50
    /**
51
     * Creates the cache warmer service definition.
52
     *
53
     * @return  Definition
54
     */
55
    private function createCacheWarmer()
56
    {
57
        $definition = new Definition(
58
            Utility::getLibraryClass('Metadata\Cache\CacheWarmer'),
59
            [new Reference(Utility::getAliasedName('metadata.factory'))]
60
        );
61
        $definition->setPublic(false);
62
        return $definition;
63
    }
64
65
    /**
66
     * Creates a file cache service definition.
67
     *
68
     * @param   string              $subClassName
69
     * @param   array               $cacheConfig
70
     * @param   ContainerBuilder    $container
71
     * @return  Definition
72
     */
73
    private function createFileCache($subClassName, array $cacheConfig, ContainerBuilder $container)
74
    {
75
        $cacheDir = $this->getFileCacheDir($cacheConfig, $container);
76
        Utility::appendParameter('dirs', 'metadata_cache_dir', $cacheDir, $container);
77
78
        return new Definition(
79
            Utility::getLibraryClass($subClassName),
80
            [$cacheDir]
81
        );
82
    }
83
84
    /**
85
     * Creates the redis cache service definition.
86
     *
87
     * @param   array               $cacheConfig
88
     * @return  Definition
89
     */
90
    private function createRedisCache(array $cacheConfig)
91
    {
92
        return new Definition(
93
            Utility::getLibraryClass('Metadata\Cache\RedisCache'),
94
            [new Reference($cacheConfig['parameters']['handler'])]
95
        );
96
    }
97
98
    /**
99
     * Gets the file cache directory.
100
     *
101
     * @param   array               $cacheConfig
102
     * @param   ContainerBuilder    $container
103
     * @return  string
104
     */
105
    private function getFileCacheDir(array $cacheConfig, ContainerBuilder $container)
106
    {
107
        $dir = sprintf('%s/as3_modlr', $container->getParameter('kernel.cache_dir'));
108
        if (isset($cacheConfig['parameters']['dir'])) {
109
            $dir = $cacheConfig['parameters']['dir'];
110
        }
111
        return $dir;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function load(array $config, ContainerBuilder $container)
118
    {
119
        // Load cache warming services.
120
        // Run always, regardless of cache enabling, so the command the warmers are always there.
121
        // The underlying cache warmer will not execute if cache is disabled.
122
        $this->loadCacheWarming($container);
123
124
        $cacheName = Utility::getAliasedName('metadata.cache');
125
        $cacheConfig = $config['metadata']['cache'];
126
        if (false === $cacheConfig['enabled']) {
127
            return $this;
128
        }
129
130
        if (isset($cacheConfig['service'])) {
131
            // Custom cache service.
132
            $container->setAlias($cacheName, Utility::cleanServiceName($cacheConfig['service']));
133
            return $this;
134
        }
135
136
        // Built-in cache service.
137
        switch ($cacheConfig['type']) {
138
            case 'file':
139
                $definition = $this->createFileCache('Metadata\Cache\FileCache', $cacheConfig, $container);
140
                break;
141
            case 'binary_file':
142
                $definition = $this->createFileCache('Metadata\Cache\BinaryFileCache', $cacheConfig, $container);
143
                break;
144
            case 'redis':
145
                $definition = $this->createRedisCache($cacheConfig);
146
                break;
147
            default:
148
                throw new \RuntimeException(sprintf('Unable to create a metadata cache service for type "%s"', $cacheConfig['type']));
149
        }
150
        $container->setDefinition($cacheName, $definition);
151
        return $this;
152
    }
153
154
    /**
155
     * Loads cache warming services.
156
     *
157
     * @param   ContainerBuilder    $container
158
     * @return  self
159
     */
160
    private function loadCacheWarming(ContainerBuilder $container)
161
    {
162
        // Root cache warmer
163
        $warmerName = Utility::getAliasedName('metadata.cache.warmer');
164
        $definition = $this->createCacheWarmer($container);
0 ignored issues
show
Unused Code introduced by
The call to MetadataCache::createCacheWarmer() has too many arguments starting with $container.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
165
        $container->setDefinition($warmerName, $definition);
166
167
        // Bundle wrapped cache warmer
168
        $definition = $this->createBundleCacheWarmer($warmerName);
169
        $container->setDefinition(Utility::getAliasedName('bundle.cache.warmer'), $definition);
170
171
        // Cache clear command
172
        $definition = $this->createCacheClearCommand($warmerName);
173
        $container->setDefinition(Utility::getAliasedName('command.metadata.cache_clear'), $definition);
174
175
        return $this;
176
    }
177
}
178