Completed
Push — master ( 230500...031d4a )
by Nazar
04:09
created

storages::admin_storages_get_engines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015-2016, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller\admin;
11
use
12
	cs\Config,
13
	cs\Core,
14
	cs\ExitException,
15
	cs\Language,
16
	cs\Page;
17
18
trait storages {
19
	/**
20
	 * Get array of storages
21
	 */
22
	static function admin_storages_get () {
23
		$Config      = Config::instance();
24
		$Core        = Core::instance();
25
		$storages    = $Config->storage;
26
		$storages[0] = [
27
			'host'       => $Core->storage_host,
28
			'connection' => $Core->storage_type,
29
			'user'       => '',
30
			'url'        => $Core->storage_url ?: url_by_source(PUBLIC_STORAGE)
31
		];
32
		foreach ($storages as $i => &$storage) {
33
			$storage['index'] = $i;
34
		}
35
		Page::instance()->json(array_values($storages));
36
	}
37
	/**
38
	 * Update storage settings
39
	 *
40
	 * @param \cs\Request $Request
41
	 *
42
	 * @throws ExitException
43
	 */
44
	static function admin_storages_patch ($Request) {
45
		$storage_index = $Request->route_ids(0);
46
		$data          = $Request->data('url', 'host', 'connection', 'user', 'password');
47
		if (
48
			!$storage_index ||
49
			!$data ||
50
			!strlen($data['host']) ||
51
			!in_array($data['connection'], static::admin_storages_get_engines())
52
		) {
53
			throw new ExitException(400);
54
		}
55
		$Config   = Config::instance();
56
		$storages = &$Config->storage;
57
		if (!isset($storages[$storage_index])) {
58
			throw new ExitException(404);
59
		}
60
		$storage = &$storages[$storage_index];
61
		$storage = $data + $storage;
62
		if (!$Config->save()) {
63
			throw new ExitException(500);
64
		}
65
	}
66
	/**
67
	 * Create storage
68
	 *
69
	 * @param \cs\Request $Request
70
	 *
71
	 * @throws ExitException
72
	 */
73
	static function admin_storages_post ($Request) {
74
		$data = $Request->data('url', 'host', 'connection', 'user', 'password');
75
		if (
76
			!$data ||
77
			!strlen($data['host']) ||
78
			!in_array($data['connection'], static::admin_storages_get_engines())
79
		) {
80
			throw new ExitException(400);
81
		}
82
		$Config            = Config::instance();
83
		$Config->storage[] = $data;
84
		if (!$Config->save()) {
85
			throw new ExitException(500);
86
		}
87
	}
88
	/**
89
	 * Delete storage
90
	 *
91
	 * @param \cs\Request $Request
92
	 *
93
	 * @throws ExitException
94
	 */
95
	static function admin_storages_delete ($Request) {
96
		$storage_index = $Request->route_ids(0);
97
		if (!$storage_index) {
98
			throw new ExitException(400);
99
		}
100
		$Config   = Config::instance();
101
		$storages = &$Config->storage;
102
		if (!isset($storages[$storage_index])) {
103
			throw new ExitException(404);
104
		}
105
		static::admin_storages_delete_check_usages($storage_index);
106
		unset($storages[$storage_index]);
107
		if (!$Config->save()) {
108
			throw new ExitException(500);
109
		}
110
	}
111
	protected static function admin_storages_delete_check_usages ($storage_index) {
112
		$Config  = Config::instance();
113
		$used_by = [];
114
		foreach ($Config->components['modules'] as $module => $module_data) {
115
			if (isset($module_data['storage']) && is_array($module_data['storage'])) {
116
				foreach ($module_data['storage'] as $index) {
117
					if ($index == $storage_index) {
118
						$used_by[] = $module;
119
					}
120
				}
121
			}
122
		}
123
		if ($used_by) {
124
			throw new ExitException(
125
				Language::instance()->storage_used_by_modules.': '.implode(', ', $used_by),
126
				409
127
			);
128
		}
129
	}
130
	/**
131
	 * Get array of available storage engines
132
	 */
133
	static function admin_storages_engines () {
134
		Page::instance()->json(
135
			static::admin_storages_get_engines()
136
		);
137
	}
138
	/**
139
	 * @return string[]
140
	 */
141
	protected static function admin_storages_get_engines () {
142
		return _mb_substr(get_files_list(ENGINES.'/Storage', '/^[^_].*?\.php$/i', 'f'), 0, -4);
143
	}
144
	/**
145
	 * Test storage connection
146
	 *
147
	 * @param \cs\Request $Request
148
	 *
149
	 * @throws ExitException
150
	 */
151
	static function admin_storages_test ($Request) {
152
		$data    = $Request->data('url', 'host', 'connection', 'user', 'password');
153
		$engines = static::admin_storages_get_engines();
154
		if (!$data || !in_array($data['connection'], $engines, true)) {
155
			throw new ExitException(400);
156
		}
157
		$connection_class = "\\cs\\Storage\\$data[connection]";
158
		/**
159
		 * @var \cs\Storage\_Abstract $connection
160
		 */
161
		$connection = new $connection_class($data['url'], $data['host'], $data['user'], $data['password']);
162
		if (!$connection->connected()) {
163
			throw new ExitException(500);
164
		}
165
	}
166
}
167