Completed
Push — master ( f07209...e7c824 )
by Anton
04:34
created

Handler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 9.01 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 10
loc 111
rs 10
c 1
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getContents() 0 28 1
B handleAjax() 4 26 5
B handle() 6 44 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Modules\Filemanager\Utils {
4
5
	use Modules\Filemanager, Modules\Informer, Utils\Messages, Utils\View, Ajax, Language, Request;
6
7
	abstract class Handler {
8
9
		protected $parent = null, $entity = null, $form = null;
10
11
		# Get contents
12
13
		private function getContents() {
14
15
			$contents = View::get(static::$view);
16
17
			# Set breadcrumbs
18
19
			$contents->breadcrumbs = $this->parent->breadcrumbs();
20
21
			# Set parent
22
23
			$contents->parent = $this->parent->path();
24
25
			# Set name
26
27
			$contents->name = $this->entity->name();
28
29
			# Implement form
30
31
			$this->form->implement($contents);
32
33
			# Set info
34
35
			$this->processInfo($contents->block('info'));
36
37
			# ------------------------
38
39
			return $contents;
40
		}
41
42
		# Handle ajax request
43
44
		private function handleAjax() {
45
46
			$ajax = Ajax::response();
47
48
			# Check for demo mode
49
50
			if (Informer::isDemoMode()) return $ajax->error(Language::get('DEMO_MODE_RESTRICTION'));
51
52
			# Init entity
53
54
			if (!$this->entity->init(Request::get('name'))) {
55
56
				return $ajax->error(Language::get(static::$message_error_remove));
57
			}
58
59
			# Process remove action
60
61 View Code Duplication
			if (Request::post('action') === 'remove') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
63
				if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove));
64
			}
65
66
			# ------------------------
67
68
			return $ajax;
69
		}
70
71
		# Handle request
72
73
		public function handle() {
74
75
			# Create parent
76
77
			$this->parent = Filemanager::open(Request::get('parent'));
78
79
			# Create entity
80
81
			$this->entity = Filemanager::get(static::$type, $this->parent);
82
83
			# Handle ajax request
84
85
			if (Request::isAjax()) return $this->handleAjax();
86
87
			# Init entity
88
89 View Code Duplication
			if (!$this->entity->init(Request::get('name'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
91
				$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
92
93
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager' . $query);
94
			}
95
96
			# Create form
97
98
			$this->form = new Filemanager\Form\Rename($this->entity);
99
100
			# Handle form
101
102
			if ($this->form->handle(new Filemanager\Controller\Rename($this->entity))) {
103
104
				$query = ('?parent=' . $this->parent->path() . '&name=' . $this->entity->name() . '&submitted');
105
106
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$type . $query);
107
			}
108
109
			# Display success message
110
111
			if (false !== Request::get('submitted')) Messages::success(Language::get(static::$message_success_rename));
112
113
			# ------------------------
114
115
			return $this->getContents();
116
		}
117
	}
118
}
119