Rename::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Filemanager
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Filemanager\Controller {
11
12
	use Modules\Filemanager, Utils\Validate;
13
14
	class Rename {
15
16
		protected $parent = null, $entity = null;
17
18
		/**
19
		 * Constructor
20
		 */
21
22
		public function __construct(Filemanager\Utils\Entity $entity) {
23
24
			$this->parent = $entity->getParent(); $this->entity = $entity;
25
		}
26
27
		/**
28
		 * Invoker
29
		 *
30
		 * @return true|string|array : true on success, otherwise an error code, or an array of type [$param_name, $error_code],
31
		 *         where $param_name is a name of param that has triggered the error,
32
		 *         and $error_code is a language phrase related to the error
33
		 */
34
35
		public function __invoke(array $post) {
36
37
			# Declare variables
38
39
			$name = '';
40
41
			# Extract post array
42
43
			extract($post);
44
45
			# Validate name
46
47
			if (false === ($name = Validate::fileName($name))) return ['name', 'FILEMANAGER_ERROR_NAME_INVALID'];
48
49
			if ($this->parent->isIgnoreHidden() && preg_match('/^\./', $name)) return ['name', 'FILEMANAGER_ERROR_HIDDEN'];
50
51
			# Check if name is used
52
53
			if (!$this->entity->check($name)) return ['name', 'FILEMANAGER_ERROR_EXISTS'];
54
55
			# Rename entity
56
57
			if (!$this->entity->rename($name)) return (($this->entity->getType() === 'dir') ?
58
59
				'FILEMANAGER_ERROR_DIR_RENAME' : 'FILEMANAGER_ERROR_FILE_RENAME');
60
61
			# ------------------------
62
63
			return true;
64
		}
65
	}
66
}
67