Completed
Push — master ( d5e807...536ced )
by Michael
05:17 queued 45s
created

CacheServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 74
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
C getCacheService() 0 45 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(PsrCacheItemPoolInterface::class, [$this, 'getCacheService'], true);
34 1
	}
35
36
	/**
37
	 * Get the `cache` service
38
	 *
39
	 * @param   Container  $container  The DI container.
40
	 *
41
	 * @return  PsrCacheItemPoolInterface
42
	 *
43
	 * @since   1.0
44
	 */
45 6
	public function getCacheService(Container $container)
46
	{
47
		/** @var \Joomla\Registry\Registry $config */
48 6
		$config = $container->get('config');
49
50
		// If caching isn't enabled then just return a void cache
51 6
		if (!$config->get('cache.enabled', false))
52 6
		{
53 1
			return new CacheAdapter\None;
54
		}
55
56 5
		$adapter = $config->get('cache.adapter', 'file');
57
58
		switch ($adapter)
59
		{
60 5
			case 'filesystem':
61 2
				$path = $config->get('cache.filesystem.path', 'cache');
62
63
				// If no path is given, fall back to the system's temporary directory
64 2
				if (empty($path))
65 2
				{
66 1
					$path = sys_get_temp_dir();
67 1
				}
68
69
				// If the path is relative, make it absolute... Sorry Windows users, this breaks support for your environment
70 2
				if (substr($path, 0, 1) !== '/')
71 2
				{
72 1
					$path = APPROOT . '/' . $path;
73 1
				}
74
75
				$options = [
76 2
					'file.path' => $path,
77 2
				];
78
79 2
				return new CacheAdapter\File($options);
80
81 3
			case 'none':
82 1
				return new CacheAdapter\None;
83
84 2
			case 'runtime':
85 1
				return new CacheAdapter\Runtime;
86
		}
87
88 1
		throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter));
89
	}
90
}
91