Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Rename::__invoke()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 34
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 6.7273
cc 7
eloc 10
nc 6
nop 1
1
<?php
2
3
namespace Modules\Filemanager\Controller {
4
5
	use Modules\Filemanager, Modules\Informer;
6
7
	class Rename {
8
9
		protected $entity = null;
10
11
		# Constructor
12
13
		public function __construct(Filemanager\Utils\Entity $entity) {
14
15
			$this->entity = $entity;
16
		}
17
18
		# Invoker
19
20
		public function __invoke(array $post) {
21
22
			# Declare variables
23
24
			$name = '';
25
26
			# Extract post array
27
28
			extract($post);
29
30
			# Check for demo mode
31
32
			if (Informer::isDemoMode()) return 'DEMO_MODE_RESTRICTION';
33
34
			# Validate name
35
36
			if (false === ($name = Filemanager\Validate::name($name))) return 'FILEMANAGER_ERROR_NAME_INVALID';
37
38
			# Check if item exists
39
40
			if ((0 !== strcasecmp($this->entity->name(), $name)) &&
41
42
				@file_exists($this->entity->parent()->pathFull() . $name)) return 'FILEMANAGER_ERROR_EXISTS';
43
44
			# Rename item
45
46
			if (!$this->entity->rename($name)) return (($this->entity->type() === FILEMANAGER_TYPE_DIR) ?
47
48
				'FILEMANAGER_ERROR_DIR_RENAME' : 'FILEMANAGER_ERROR_FILE_RENAME');
49
50
			# ------------------------
51
52
			return true;
53
		}
54
	}
55
}
56