Failed Conditions
Push — master ( 117895...2cd9b5 )
by Alexander
03:10
created

CacheFactory::create()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 39
cp 0
rs 7.4119
c 0
b 0
f 0
cc 8
eloc 34
nc 9
nop 2
crap 72

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
 * This file is part of the Jira-CLI library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/jira-cli
9
 */
10
11
namespace ConsoleHelpers\JiraCLI\Cache;
12
13
14
use Doctrine\Common\Cache\ApcCache;
15
use Doctrine\Common\Cache\ArrayCache;
16
use Doctrine\Common\Cache\CacheProvider;
17
use Doctrine\Common\Cache\ChainCache;
18
use Doctrine\Common\Cache\MemcacheCache;
19
use Doctrine\Common\Cache\MemcachedCache;
20
21
class CacheFactory
22
{
23
24
	/**
25
	 * Namespace.
26
	 *
27
	 * @var string
28
	 */
29
	private $_namespace;
30
31
	/**
32
	 * CacheFactory constructor.
33
	 *
34
	 * @param string $namespace Namespace.
35
	 */
36
	public function __construct($namespace)
37
	{
38
		$this->_namespace = $namespace;
39
	}
40
41
	/**
42
	 * Creates cache by name.
43
	 *
44
	 * @param string $name    Name.
45
	 * @param array  $options Options.
46
	 *
47
	 * @return CacheProvider
48
	 * @throws \InvalidArgumentException When cache provider with given name not found.
49
	 * @throws \LogicException When no caches provided for "chain" cache.
50
	 */
51
	public function create($name, array $options = array())
52
	{
53
		switch ( $name ) {
54
			case 'chain':
55
				$valid_caches = array();
56
57
				foreach ( array_filter($options) as $cache_name ) {
58
					$valid_caches[] = self::create($cache_name);
59
				}
60
61
				if ( !$valid_caches ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $valid_caches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
					throw new \LogicException('No valid caches provided for "chain" cache.');
63
				}
64
65
				$cache_driver = new ChainCache($valid_caches);
66
				$cache_driver->setNamespace($this->_namespace);
67
68
				return $cache_driver;
69
70
			case 'array':
71
				$cache_driver = new ArrayCache();
72
				$cache_driver->setNamespace($this->_namespace);
73
74
				return $cache_driver;
75
76
			case 'apc':
77
				$cache_driver = new ApcCache();
78
				$cache_driver->setNamespace($this->_namespace);
79
80
				return $cache_driver;
81
82
			case 'memcache':
83
				$memcache = new \Memcache();
84
				$memcache->connect('localhost', 11211);
85
86
				$cache_driver = new MemcacheCache();
87
				$cache_driver->setMemcache($memcache);
88
				$cache_driver->setNamespace($this->_namespace);
89
90
				return $cache_driver;
91
92
			case 'memcached':
93
				$memcached = new \Memcached();
94
				$memcached->addServer('memcache_host', 11211);
95
96
				$cache_driver = new MemcachedCache();
97
				$cache_driver->setMemcached($memcached);
98
				$cache_driver->setNamespace($this->_namespace);
99
100
				return $cache_driver;
101
		}
102
103
		throw new \InvalidArgumentException('Cache provider "' . $name . '" not found.');
104
	}
105
106
}
107