storages::admin_storages_patch()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 4
nop 1
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
ccs 0
cts 16
cp 0
crap 56
1
<?php
2
/**
3
 * @package    CleverStyle Framework
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @license    0BSD
8
 */
9
namespace cs\modules\System\api\Controller\admin;
10
use
11
	cs\Config,
12
	cs\Core,
13
	cs\ExitException,
14
	cs\Language,
15
	cs\Storage;
16
17
trait storages {
18
	/**
19
	 * Get array of storages
20
	 */
21
	public static function admin_storages_get () {
22
		$Config      = Config::instance();
23
		$Core        = Core::instance();
24
		$storages    = $Config->storage;
25
		$storages[0] = [
26
			'host'   => $Core->storage_host,
27
			'driver' => $Core->storage_driver,
28
			'user'   => $Core->storage_user,
29
			'url'    => Storage::instance()->storage(0)->base_url()
0 ignored issues
show
Bug introduced by
The method base_url() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

29
			'url'    => Storage::instance()->storage(0)->/** @scrutinizer ignore-call */ base_url()
Loading history...
30
		];
31
		foreach ($storages as $i => &$storage) {
32
			$storage['index'] = $i;
33
		}
34
		return array_values($storages);
35
	}
36
	/**
37
	 * Update storage settings
38
	 *
39
	 * @param \cs\Request $Request
40
	 *
41
	 * @throws ExitException
42
	 */
43
	public static function admin_storages_patch ($Request) {
44
		$storage_index = $Request->route_ids(0);
45
		$data          = $Request->data('url', 'host', 'driver', 'user', 'password');
0 ignored issues
show
Bug introduced by
'url' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

45
		$data          = $Request->data(/** @scrutinizer ignore-type */ 'url', 'host', 'driver', 'user', 'password');
Loading history...
46
		if (
47
			!$storage_index ||
48
			!$data ||
49
			!strlen($data['host']) ||
50
			!in_array($data['driver'], static::admin_storages_get_drivers())
51
		) {
52
			throw new ExitException(400);
53
		}
54
		$Config   = Config::instance();
55
		$storages = &$Config->storage;
56
		if (!isset($storages[$storage_index])) {
57
			throw new ExitException(404);
58
		}
59
		$storage = &$storages[$storage_index];
60
		$storage = $data + $storage;
61
		if (!$Config->save()) {
62
			throw new ExitException(500);
63
		}
64
	}
65
	/**
66
	 * Create storage
67
	 *
68
	 * @param \cs\Request $Request
69
	 *
70
	 * @throws ExitException
71
	 */
72
	public static function admin_storages_post ($Request) {
73
		$data = $Request->data('url', 'host', 'driver', 'user', 'password');
0 ignored issues
show
Bug introduced by
'url' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

73
		$data = $Request->data(/** @scrutinizer ignore-type */ 'url', 'host', 'driver', 'user', 'password');
Loading history...
74
		if (
75
			!$data ||
76
			!strlen($data['host']) ||
77
			!in_array($data['driver'], static::admin_storages_get_drivers())
78
		) {
79
			throw new ExitException(400);
80
		}
81
		$Config            = Config::instance();
82
		$Config->storage[] = $data;
83
		if (!$Config->save()) {
84
			throw new ExitException(500);
85
		}
86
	}
87
	/**
88
	 * Delete storage
89
	 *
90
	 * @param \cs\Request $Request
91
	 *
92
	 * @throws ExitException
93
	 */
94
	public static function admin_storages_delete ($Request) {
95
		$storage_index = $Request->route_ids(0);
96
		if (!$storage_index) {
97
			throw new ExitException(400);
98
		}
99
		$Config   = Config::instance();
100
		$storages = &$Config->storage;
101
		if (!isset($storages[$storage_index])) {
102
			throw new ExitException(404);
103
		}
104
		static::admin_storages_delete_check_usages($storage_index);
105
		unset($storages[$storage_index]);
106
		if (!$Config->save()) {
107
			throw new ExitException(500);
108
		}
109
	}
110
	protected static function admin_storages_delete_check_usages ($storage_index) {
111
		$Config  = Config::instance();
112
		$used_by = [];
113
		foreach ($Config->components['modules'] as $module => $module_data) {
114
			if (isset($module_data['storage']) && is_array($module_data['storage'])) {
115
				foreach ($module_data['storage'] as $index) {
116
					if ($index == $storage_index) {
117
						$used_by[] = $module;
118
					}
119
				}
120
			}
121
		}
122
		if ($used_by) {
123
			throw new ExitException(
124
				Language::instance()->storage_used_by_modules.': '.implode(', ', $used_by),
0 ignored issues
show
Bug Best Practice introduced by
The property storage_used_by_modules does not exist on cs\Language. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
				409
126
			);
127
		}
128
	}
129
	/**
130
	 * Get array of available storage drivers
131
	 */
132
	public static function admin_storages_drivers () {
133
		return static::admin_storages_get_drivers();
134
	}
135
	/**
136
	 * @return string[]
137
	 */
138
	protected static function admin_storages_get_drivers () {
139
		return _mb_substr(get_files_list(DIR.'/core/drivers/Storage', '/^[^_].*?\.php$/i', 'f'), 0, -4);
140
	}
141
	/**
142
	 * Test storage connection
143
	 *
144
	 * @param \cs\Request $Request
145
	 *
146
	 * @throws ExitException
147
	 */
148
	public static function admin_storages_test ($Request) {
149
		$data    = $Request->data('url', 'host', 'driver', 'user', 'password');
0 ignored issues
show
Bug introduced by
'url' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

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

149
		$data    = $Request->data(/** @scrutinizer ignore-type */ 'url', 'host', 'driver', 'user', 'password');
Loading history...
150
		$drivers = static::admin_storages_get_drivers();
151
		if (!$data || !in_array($data['driver'], $drivers, true)) {
152
			throw new ExitException(400);
153
		}
154
		$driver_class = "\\cs\\Storage\\$data[driver]";
155
		/**
156
		 * @var \cs\Storage\_Abstract $connection
157
		 */
158
		$connection = new $driver_class($data['url'], $data['host'], $data['user'], $data['password']);
159
		if (!$connection->connected()) {
160
			throw new ExitException(500);
161
		}
162
	}
163
}
164