Completed
Push — master ( 8b6bb0...9b3eef )
by Morris
09:38
created

RedisFactory::isAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * @author Robin Appelman <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC;
23
24
class RedisFactory {
25
	/** @var  \Redis */
26
	private $instance;
27
28
	/** @var  SystemConfig */
29
	private $config;
30
31
	/**
32
	 * RedisFactory constructor.
33
	 *
34
	 * @param SystemConfig $config
35
	 */
36
	public function __construct(SystemConfig $config) {
37
		$this->config = $config;
38
	}
39
40
	private function create() {
41
		$this->instance = new \Redis();
42
		// TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays
43
		$config = $this->config->getValue('redis', array());
44
		if (isset($config['host'])) {
45
			$host = $config['host'];
46
		} else {
47
			$host = '127.0.0.1';
48
		}
49
		if (isset($config['port'])) {
50
			$port = $config['port'];
51
		} else {
52
			$port = 6379;
53
		}
54
		if (isset($config['timeout'])) {
55
			$timeout = $config['timeout'];
56
		} else {
57
			$timeout = 0.0; // unlimited
58
		}
59
60
		$this->instance->connect($host, $port, $timeout);
61
		if (isset($config['password']) && $config['password'] !== '') {
62
			$this->instance->auth($config['password']);
63
		}
64
65
		if (isset($config['dbindex'])) {
66
			$this->instance->select($config['dbindex']);
67
		}
68
	}
69
70
	public function getInstance() {
71
		if (!$this->isAvailable()) {
72
			throw new \Exception('Redis support is not available');
73
		}
74
		if (!$this->instance instanceof \Redis) {
0 ignored issues
show
Bug introduced by
The class Redis does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
75
			$this->create();
76
		}
77
78
		return $this->instance;
79
	}
80
81
	public function isAvailable() {
82
		return extension_loaded('redis')
83
		&& version_compare(phpversion('redis'), '2.2.5', '>=');
84
	}
85
}
86