Completed
Push — mysql_improvements ( 2e95ce...dedbef )
by Michael
03:52
created

CacheServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 72
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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