CacheFactory::create()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 22.2499

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
nc 9
nop 2
dl 0
loc 53
ccs 13
cts 33
cp 0.3938
crap 22.2499
rs 8.1475
c 1
b 0
f 0

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 4
	public function __construct($namespace)
37
	{
38 4
		$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 4
	public function create($name, array $options = array())
52
	{
53
		switch ( $name ) {
54 4
			case 'chain':
55 4
				$valid_caches = array();
56
57 4
				foreach ( array_filter($options) as $cache_name ) {
58 4
					$valid_caches[] = self::create($cache_name);
0 ignored issues
show
Bug Best Practice introduced by
The method ConsoleHelpers\JiraCLI\C...\CacheFactory::create() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
					/** @scrutinizer ignore-call */ 
59
     $valid_caches[] = self::create($cache_name);
Loading history...
59
				}
60
61 4
				if ( !$valid_caches ) {
62
					throw new \LogicException('No valid caches provided for "chain" cache.');
63
				}
64
65 4
				$cache_driver = new ChainCache($valid_caches);
66 4
				$cache_driver->setNamespace($this->_namespace);
67
68 4
				return $cache_driver;
69
70 4
			case 'array':
71 4
				$cache_driver = new ArrayCache();
72 4
				$cache_driver->setNamespace($this->_namespace);
73
74 4
				return $cache_driver;
75
76
			case 'apc':
77
				$cache_driver = new ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated: since version 1.6, use ApcuCache instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
				$cache_driver = /** @scrutinizer ignore-deprecated */ new ApcCache();
Loading history...
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('localhost', 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