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

Handler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 11
Metric Value
wmc 11
lcom 1
cbo 11
dl 10
loc 104
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getContents() 0 28 1
A handleAjax() 4 19 4
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
			# Remove item
53
54 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...
55
56
				if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove));
57
			}
58
59
			# ------------------------
60
61
			return $ajax;
62
		}
63
64
		# Handle request
65
66
		public function handle() {
67
68
			# Create parent
69
70
			$this->parent = Filemanager::open(Request::get('parent'));
71
72
			# Create entity
73
74
			$this->entity = Filemanager::get(static::$type, $this->parent);
75
76
			# Init entity
77
78 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...
79
80
				$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
81
82
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager' . $query);
83
			}
84
85
			# Handle ajax request
86
87
			if (Request::isAjax()) return $this->handleAjax();
88
89
			# Create form
90
91
			$this->form = new Filemanager\Form\Rename($this->entity);
92
93
			# Handle form
94
95
			if ($this->form->handle(new Filemanager\Controller\Rename($this->entity))) {
96
97
				$query = ('?parent=' . $this->parent->path() . '&name=' . $this->entity->name() . '&submitted');
98
99
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$type . $query);
100
			}
101
102
			# Display success message
103
104
			if (false !== Request::get('submitted')) Messages::success(Language::get(static::$message_success_rename));
105
106
			# ------------------------
107
108
			return $this->getContents();
109
		}
110
	}
111
}
112