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

CacheServiceProvider::register()   C

Complexity

Conditions 9
Paths 1

Size

Total Lines 72
Code Lines 33

Duplication

Lines 36
Ratio 50 %

Code Coverage

Tests 8
CRAP Score 53.3673

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 36
loc 72
ccs 8
cts 44
cp 0.1818
rs 6.0413
cc 9
eloc 33
nc 1
nop 1
crap 53.3673

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
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