Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

CacheServiceProvider::getCacheService()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 45
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 8
nop 1
crap 7
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Providers;
10
11
use Joomla\Cache\{
12
	AbstractCacheItemPool, Adapter as CacheAdapter, CacheItemPoolInterface
13
};
14
use Joomla\DI\{
15
	Container, ServiceProviderInterface
16
};
17
use Psr\Cache\CacheItemPoolInterface as PsrCacheItemPoolInterface;
18
19
/**
20
 * Cache service provider
21
 *
22
 * @since  1.0
23
 */
24
class CacheServiceProvider implements ServiceProviderInterface
25
{
26
	/**
27
	 * Registers the service provider with a DI container.
28
	 *
29
	 * @param   Container  $container  The DI container.
30
	 *
31
	 * @return  void
32
	 *
33
	 * @since   1.0
34
	 */
35 1
	public function register(Container $container)
36
	{
37 1
		$container->alias('cache', PsrCacheItemPoolInterface::class)
38 1
			->alias(CacheItemPoolInterface::class, PsrCacheItemPoolInterface::class)
39 1
			->alias(AbstractCacheItemPool::class, PsrCacheItemPoolInterface::class)
40 1
			->share(PsrCacheItemPoolInterface::class, [$this, 'getCacheService'], true);
41 1
	}
42
43
	/**
44
	 * Get the `cache` service
45
	 *
46
	 * @param   Container  $container  The DI container.
47
	 *
48
	 * @return  PsrCacheItemPoolInterface
49
	 *
50
	 * @since   1.0
51
	 */
52 6
	public function getCacheService(Container $container) : PsrCacheItemPoolInterface
53
	{
54
		/** @var \Joomla\Registry\Registry $config */
55 6
		$config = $container->get('config');
56
57
		// If caching isn't enabled then just return a void cache
58 6
		if (!$config->get('cache.enabled', false))
59
		{
60 1
			return new CacheAdapter\None;
61
		}
62
63 5
		$adapter = $config->get('cache.adapter', 'file');
64
65
		switch ($adapter)
66
		{
67 5
			case 'filesystem':
68 2
				$path = $config->get('cache.filesystem.path', 'cache');
69
70
				// If no path is given, fall back to the system's temporary directory
71 2
				if (empty($path))
72
				{
73 1
					$path = sys_get_temp_dir();
74
				}
75
76
				// If the path is relative, make it absolute... Sorry Windows users, this breaks support for your environment
77 2
				if (substr($path, 0, 1) !== '/')
78
				{
79 1
					$path = APPROOT . '/' . $path;
80
				}
81
82
				$options = [
83 2
					'file.path' => $path,
84
				];
85
86 2
				return new CacheAdapter\File($options);
87
88 3
			case 'none':
89 1
				return new CacheAdapter\None;
90
91 2
			case 'runtime':
92 1
				return new CacheAdapter\Runtime;
93
		}
94
95 1
		throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter));
96
	}
97
}
98