Handler   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 147
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 7
loc 147
rs 9.1666
c 1
b 0
f 0
wmc 17
lcom 1
cbo 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getContents() 0 36 2
B handleAjax() 7 33 5
C handle() 0 57 10

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
/**
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\Utils {
11
12
	use Frames, Modules\Filemanager, Utils\Popup, Utils\View, Ajax, Language, Request, Template;
13
14
	abstract class Handler extends Frames\Admin\Area\Panel {
15
16
		protected $parent = null, $entity = null, $link = '', $forms = [], $controllers = [];
17
18
		/**
19
		 * Get the contents block
20
		 */
21
22
		private function getContents() : Template\Block {
23
24
			$contents = View::get(static::$view);
25
26
			# Set origin and title
27
28
			$contents->origin = static::$origin; $contents->title = Language::get(static::$title);
0 ignored issues
show
Documentation introduced by
The property origin does not exist on object<Template\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...
Documentation introduced by
The property title does not exist on object<Template\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...
29
30
			# Set breadcrumbs
31
32
			$contents->breadcrumbs = $this->parent->getBreadcrumbs();
0 ignored issues
show
Documentation introduced by
The property breadcrumbs does not exist on object<Template\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...
33
34
			# Set parent
35
36
			$contents->parent = $this->parent->getPath();
0 ignored issues
show
Documentation introduced by
The property parent does not exist on object<Template\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...
37
38
			# Set name
39
40
			$contents->name = $this->entity->getName();
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Template\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...
41
42
			# Set editor mode
43
44
			$contents->mode = Mime::getMode($this->entity->getExtension());
0 ignored issues
show
Documentation introduced by
The property mode does not exist on object<Template\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...
45
46
			# Implement forms
47
48
			foreach ($this->forms as $form) $form->implement($contents);
49
50
			# Process info block
51
52
			$this->processInfo($contents->getBlock('info'));
53
54
			# ------------------------
55
56
			return $contents;
57
		}
58
59
		/**
60
		 * Handle the ajax request
61
		 */
62
63
		private function handleAjax() : Ajax\Response {
64
65
			$ajax = Ajax::createResponse();
66
67
			# Create parent
68
69
			$this->parent = new static::$container_class(Request::get('parent'));
70
71
			# Create entity
72
73
			$this->entity = Filemanager::get($this->parent, Request::get('name'));
74
75
			# Init entity
76
77
			if (!$this->entity->isInited()) {
78
79
				return $ajax->setError(Language::get(static::$message_error_remove));
80
			}
81
82
			# Process remove action
83
84 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...
85
86
				if (!static::$permissions['manage'] || !$this->entity->remove()) {
87
88
					return $ajax->setError(Language::get(static::$message_error_remove));
89
				}
90
			}
91
92
			# ------------------------
93
94
			return $ajax;
95
		}
96
97
		/**
98
		 * Handle the request
99
		 *
100
		 * @return Template\Block|Ajax\Response : a block object if the ajax param was set to false, otherwise an ajax response
101
		 */
102
103
		protected function handle(bool $ajax = false) {
104
105
			# Handle ajax request
106
107
			if ($ajax) return $this->handleAjax();
108
109
			# Create parent
110
111
			$this->parent = new static::$container_class(Request::get('parent'));
112
113
			# Create entity
114
115
			$this->entity = Filemanager::get($this->parent, Request::get('name'));
116
117
			# Redirect if entity not found
118
119
			if (!$this->entity->isInited()) {
120
121
				$query = (('' !== $this->parent->getPath()) ? ('?parent=' . $this->parent->getPath()) : '');
122
123
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$origin . '/' . $query);
124
			}
125
126
			# Create rename form
127
128
			$this->forms['rename'] = new Filemanager\Form\Rename($this->entity, static::$permissions['manage']);
129
130
			$this->controllers['rename'] = new Filemanager\Controller\Rename($this->entity);
131
132
			# Create edit form
133
134
			if ($this->entity->isFile() && static::$permissions['edit']) {
135
136
				$this->forms['edit'] = new Filemanager\Form\Edit($this->entity);
137
138
				$this->controllers['edit'] = new Filemanager\Controller\Edit($this->entity);
139
			}
140
141
			# Handle form
142
143
			foreach ($this->forms as $name => $form) if ($form->handle($this->controllers[$name], true)) {
144
145
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$origin . '/' . static::$type .
146
147
					'?parent=' . $this->parent->getPath() . '&name=' . $this->entity->getName() . '&submitted=' . $name);
148
			}
149
150
			# Display success message
151
152
			if (Request::get('submitted') === 'rename') Popup::set('positive', Language::get(static::$message_success_rename));
153
154
			else if (Request::get('submitted') === 'edit') Popup::set('positive', Language::get(static::$message_success_edit));
155
156
			# ------------------------
157
158
			return $this->getContents();
159
		}
160
	}
161
}
162