Completed
Push — master ( 8890e1...79ebb7 )
by Nazar
05:38
created

Storage::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
10
/**
11
 * @method static $this instance($check = false)
12
 */
13
class Storage {
14
	use    Singleton;
15
	/**
16
	 * @var Storage\_Abstract[]|False_class[]
17
	 */
18
	protected $connections            = [];
19
	protected $successful_connections = [];
20
	protected $failed_connections     = [];
21
	/**
22
	 * Get list of connections of specified type
23
	 *
24
	 * @param bool|null|string $status `null`    - returns array of connections with objects<br>
25
	 *                                 `true|1`  - returns array of names of successful connections<br>
26
	 *                                 `false|0` - returns array of names of failed connections
27
	 *
28
	 * @return array|null
29
	 */
30
	function get_connections_list ($status = null) {
31
		if ($status === null) {
32
			return $this->connections;
33
		} elseif ($status == 0) {
34
			return $this->failed_connections;
35
		} elseif ($status == 1) {
36
			return $this->successful_connections;
37
		}
38
		return null;
39
	}
40
	/**
41
	 * Processing of getting storage instance
42
	 *
43
	 * @param int $connection
44
	 *
45
	 * @return Storage\_Abstract|False_class
46
	 */
47
	function storage ($connection) {
48
		if (!is_int($connection) && $connection != '0') {
49
			return False_class::instance();
50
		}
51
		return $this->connecting($connection);
52
	}
53
	/**
54
	 * Processing of all storage requests
55
	 *
56
	 * @param int $connection
57
	 *
58
	 * @return Storage\_Abstract|False_class
59
	 */
60
	protected function connecting ($connection) {
61
		/**
62
		 * If connection found in list of failed connections - return instance of False_class
63
		 */
64
		if (isset($this->failed_connections[$connection])) {
65
			return False_class::instance();
66
		}
67
		/**
68
		 * If connection already exists - return reference on the instance of Storage engine object
69
		 */
70
		if (isset($this->connections[$connection])) {
71
			return $this->connections[$connection];
72
		}
73
		$Config = Config::instance();
74
		/**
75
		 * If connection to the local storage
76
		 */
77
		if ($connection == 0) {
78
			$Core                  = Core::instance();
79
			$storage['connection'] = $Core->storage_type;
80
			$storage['url']        = $Core->storage_url;
81
			$storage['host']       = $Core->storage_host;
82
			$storage['user']       = $Core->storage_user;
83
			$storage['password']   = $Core->storage_password;
84
		} elseif (isset($Config->storage[$connection])) {
85
			$storage = &$Config->storage[$connection];
86
		} else {
87
			return False_class::instance();
88
		}
89
		/**
90
		 * Create new Storage connection
91
		 */
92
		$engine_class                   = "\\cs\\Storage\\$storage[connection]";
93
		$this->connections[$connection] = new $engine_class($storage['url'], $storage['host'], $storage['user'], $storage['password']);
94
		/**
95
		 * If successfully - add connection to the list of success connections and return instance of DB engine object
96
		 */
97
		if (is_object($this->connections[$connection]) && $this->connections[$connection]->connected()) {
98
			$this->successful_connections[] = "$connection/$storage[host]/$storage[connection]";
99
			unset($storage);
100
			$this->$connection = $this->connections[$connection];
101
			return $this->connections[$connection];
102
			/**
103
			 * If failed - add connection to the list of failed connections and display connection error
104
			 */
105
		} else {
106
			unset($this->$connection);
107
			$this->failed_connections[$connection] = "$connection/$storage[host]/$storage[connection]";
108
			unset($storage);
109
			trigger_error('Error connecting to storage '.$this->failed_connections[$connection], E_USER_WARNING);
110
			$return        = False_class::instance();
111
			$return->error = 'Connection failed';
112
			return $return;
113
		}
114
	}
115
}
116