Widget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 37 6
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Entitizer\Controller {
11
12
	use Modules\Entitizer, Utils\Validate;
13
14
	class Widget {
15
16
		private $widget = null;
17
18
		/**
19
		 * Constructor
20
		 */
21
22
		public function __construct(Entitizer\Entity\Widget $widget) {
23
24
			$this->widget = $widget;
25
		}
26
27
		/**
28
		 * Invoker
29
		 *
30
		 * @return true|string|array : true on success, otherwise an error code, or an array of type [$param_name, $error_code],
31
		 *         where $param_name is a name of param that has triggered the error,
32
		 *         and $error_code is a language phrase related to the error
33
		 */
34
35
		public function __invoke(array $post) {
36
37
			# Declare variables
38
39
			$title = ''; $name = ''; $active = ''; $contents = '';
40
41
			# Extract post array
42
43
			extract($post);
44
45
			# Validate name
46
47
			if (false === ($name = Validate::templateComponentName($name))) return ['name', 'WIDGET_ERROR_NAME_INVALID'];
48
49
			# Check name exists
50
51
			if (false === ($check_name = $this->widget->check($name, 'name'))) return 'WIDGET_ERROR_MODIFY';
52
53
			if ($check_name === 1) return ['name', 'WIDGET_ERROR_NAME_DUPLICATE'];
54
55
			# Modify widget
56
57
			$data = [];
58
59
			$data['title']              = $title;
60
			$data['name']               = $name;
61
			$data['active']             = $active;
62
			$data['contents']           = $contents;
63
64
			$modifier = ((0 === $this->widget->id) ? 'create' : 'edit');
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Entity\Widget>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
65
66
			if (!$this->widget->$modifier($data)) return 'WIDGET_ERROR_MODIFY';
67
68
			# ------------------------
69
70
			return true;
71
		}
72
	}
73
}
74