Completed
Push — master ( 77c306...84aad0 )
by Michael
02:51
created

CacheServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 18.18%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 9
c 1
b 1
f 1
lcom 0
cbo 6
dl 36
loc 84
ccs 8
cts 44
cp 0.1818
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C register() 36 72 9

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
namespace Stats\Providers;
4
5
use Doctrine\Common\Cache;
6
use Doctrine\Common\Cache\Cache as CacheInterface;
7
use Joomla\DI\Container;
8
use Joomla\DI\ServiceProviderInterface;
9
10
/**
11
 * Cache service provider
12
 *
13
 * @since  1.0
14
 */
15
class CacheServiceProvider implements ServiceProviderInterface
16
{
17
	/**
18
	 * Registers the service provider with a DI container.
19
	 *
20
	 * @param   Container  $container  The DI container.
21
	 *
22
	 * @return  void
23
	 *
24
	 * @since   1.0
25
	 */
26 1
	public function register(Container $container)
27
	{
28 1
		$container->alias('cache', CacheInterface::class)
29 1
			->share(
30 1
				CacheInterface::class,
31 1
				function (Container $container)
32
				{
33
					/** @var \Joomla\Registry\Registry $config */
34
					$config = $container->get('config');
35
36
					// If caching isn't enabled then just return a void cache
37
					if (!$config->get('cache.enabled', false))
38
					{
39
						return new Cache\VoidCache;
40
					}
41
42
					$adapter = $config->get('cache.adapter', 'filesystem');
43
44
					switch ($adapter)
45
					{
46
						case 'array':
47
							$handler = new Cache\ArrayCache;
48
49
							break;
50
51 View Code Duplication
						case 'filesystem':
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...
52
							$path = $config->get('cache.filesystem.path', 'cache');
53
54
							// If no path is given, fall back to the system's temporary directory
55
							if (empty($path))
56
							{
57
								$path = sys_get_temp_dir();
58
							}
59
60
							// If the path is relative, make it absolute
61
							if (substr($path, 0, 1) !== '/')
62
							{
63
								$path = APPROOT . '/' . $path;
64
							}
65
66
							$handler = new Cache\FilesystemCache($path);
67
68
							break;
69
70 View Code Duplication
						case 'phpfile':
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...
71
							$path = $config->get('cache.phpfile.path', 'cache');
72
73
							// If no path is given, fall back to the system's temporary directory
74
							if (empty($path))
75
							{
76
								$path = sys_get_temp_dir();
77
							}
78
79
							// If the path is relative, make it absolute
80
							if (substr($path, 0, 1) !== '/')
81
							{
82
								$path = APPROOT . '/' . $path;
83
							}
84
85
							$handler = new Cache\PhpFileCache($path);
86
87
							break;
88
89
						default:
90
							throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter));
91
					}
92
93
					return $handler;
94 1
				},
95
				true
96 1
			);
97 1
	}
98
}
99