Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Handler   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 147
Duplicated Lines 2.72 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 29
lcom 1
cbo 9
dl 4
loc 147
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processParent() 0 21 4
A processSelector() 0 8 2
C getContents() 0 42 8
A handleAjax() 4 19 3
C handle() 0 38 12

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\Entitizer\Utils {
4
5
	use Modules\Entitizer, Utils\Messages, Utils\View, Ajax, Language, Number, Request, Template;
6
7
	abstract class Handler {
8
9
		protected $create = false, $parent = null, $entity = null, $form = null;
10
11
		# Process parent block
12
13
		private function processParent(Template\Asset\Block $parent) {
14
15
			# Set parent id
16
17
			$parent->id = $this->parent->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Template\Asset\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
18
19
			# Set create button
20
21
			$parent->block('create')->class = ($this->create ? 'active item' : 'item');
22
23
			$parent->block('create')->id = $this->parent->id;
24
25
			# Set edit button
26
27
			if (0 === $this->parent->id) $parent->block('edit')->disable(); else {
28
29
				$parent->block('edit')->class = (!$this->create ? 'active item' : 'item');
30
31
				$parent->block('edit')->id = $this->parent->id;
32
			}
33
		}
34
35
		# Process selector block
36
37
		private function processSelector(Template\Asset\Block $selector) {
38
39
			if ($this->create) return $selector->disable();
40
41
			$parent = Entitizer::get(static::$type, $this->entity->parent_id);
42
43
			$selector->set(static::$naming, $parent->__get(static::$naming));
44
		}
45
46
		# Get contents
47
48
		private function getContents() {
49
50
			$contents = View::get(static::$view);
51
52
			# Set id
53
54
			$contents->id = $this->entity->id;
55
56
			# Set path / title
57
58
			if (static::$nesting) $contents->path = $this->parent->path;
59
60
			else $contents->title = ($this->create ? Language::get(static::$naming_new) : $this->entity->__get(static::$naming));
61
62
			# Set link
63
64
			$link = (INSTALL_PATH . static::$link . '/');
65
66
			if (static::$nesting) $contents->link = ($link . ($this->create ? 'create' : 'edit') . '?id=' . $this->parent->id);
67
68
			else $contents->link = ($link . ($this->create ? 'create' : ('edit?id=' . $this->entity->id)));
69
70
			# Process parent block
71
72
			if (static::$nesting) $this->processParent($contents->block('parent'));
73
74
			# Process selector block
75
76
			if (static::$nesting) $this->processSelector($contents->block('selector'));
77
78
			# Implement form
79
80
			$this->form->implement($contents);
81
82
			# Add additional data for specific entity
83
84
			$this->processEntity($contents);
85
86
			# ------------------------
87
88
			return $contents;
89
		}
90
91
		# Handle ajax request
92
93
		private function handleAjax() {
94
95
			$ajax = Ajax::response();
96
97
			# Create entity
98
99
			$this->entity = Entitizer::get(static::$type, Number::format(Request::get('id')));
100
101
			# Process remove action
102
103 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...
104
105
				if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove));
106
			}
107
108
			# ------------------------
109
110
			return $ajax;
111
		}
112
113
		# Handle request
114
115
		public function handle(bool $create = false) {
116
117
			if (!($this->create = $create) && Request::isAjax()) return $this->handleAjax();
118
119
			# Create entity
120
121
			if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('id')));
122
123
			$this->entity = Entitizer::get(static::$type, (!$this->create ? Number::format(Request::get('id')) : 0));
124
125
			# Redirect if entity not found
126
127
			if (!$this->create && (0 === $this->entity->id)) return Request::redirect(INSTALL_PATH . static::$link);
128
129
			# Create form
130
131
			$this->form = new static::$form_class($this->entity);
132
133
			if (static::$nesting && $this->create) $this->form->get('parent_id')->set($this->parent->id);
134
135
			# Handle form
136
137
			if ($this->form->handle(new static::$controller($this->entity))) {
138
139
				Request::redirect(INSTALL_PATH . static::$link . '/edit?id=' . $this->entity->id . '&submitted');
140
			}
141
142
			# Display success message
143
144
			if (!$this->create && (false !== Request::get('submitted'))) {
145
146
				Messages::success(Language::get(static::$message_success_save));
147
			}
148
149
			# ------------------------
150
151
			return $this->getContents();
152
		}
153
	}
154
}
155