Completed
Push — master ( 51a51b...3888f6 )
by Michael
06:55
created

CacheServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 30.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 67
ccs 10
cts 33
cp 0.303
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 55 7
1
<?php
2
3
namespace Stats\Providers;
4
5
use Joomla\Cache\AbstractCacheItemPool;
6
use Joomla\Cache\Adapter as CacheAdapter;
7
use Joomla\Cache\CacheItemPoolInterface;
8
use Joomla\DI\Container;
9
use Joomla\DI\ServiceProviderInterface;
10
use Psr\Cache\CacheItemPoolInterface as PsrCacheItemPoolInterface;
11
12
/**
13
 * Cache service provider
14
 *
15
 * @since  1.0
16
 */
17
class CacheServiceProvider implements ServiceProviderInterface
18
{
19
	/**
20
	 * Registers the service provider with a DI container.
21
	 *
22
	 * @param   Container  $container  The DI container.
23
	 *
24
	 * @return  void
25
	 *
26
	 * @since   1.0
27
	 */
28 1
	public function register(Container $container)
29
	{
30 1
		$container->alias('cache', PsrCacheItemPoolInterface::class)
31 1
			->alias(CacheItemPoolInterface::class, PsrCacheItemPoolInterface::class)
32 1
			->alias(AbstractCacheItemPool::class, PsrCacheItemPoolInterface::class)
33 1
			->share(
34 1
				PsrCacheItemPoolInterface::class,
35 1
				function (Container $container)
36
				{
37
					/** @var \Joomla\Registry\Registry $config */
38
					$config = $container->get('config');
39
40
					// If caching isn't enabled then just return a void cache
41
					if (!$config->get('cache.enabled', false))
42
					{
43
						return new CacheAdapter\None;
44
					}
45
46
					$adapter = $config->get('cache.adapter', 'file');
47
48
					switch ($adapter)
49
					{
50
						case 'filesystem':
51
							$path = $config->get('cache.filesystem.path', 'cache');
52
53
							// If no path is given, fall back to the system's temporary directory
54
							if (empty($path))
55
							{
56
								$path = sys_get_temp_dir();
57
							}
58
59
							// If the path is relative, make it absolute... Sorry Windows users, this breaks support for your environment
60
							if (substr($path, 0, 1) !== '/')
61
							{
62
								$path = JPATH_ROOT . '/' . $path;
63
							}
64
65
							$options = [
66
								'file.path' => $path,
67
							];
68
69
							return new CacheAdapter\File($options);
70
71
						case 'none':
72
							return new CacheAdapter\None;
73
74
						case 'runtime':
75
							return new CacheAdapter\Runtime;
76
					}
77
78
					throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter));
79 1
				},
80
				true
81 1
			);
82 1
	}
83
}
84