Passed
Push — 4.x ( 12d91e...ca6fdf )
by Jeroen
19:10 queued 14s
created

Tool::getDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Groups;
4
5
use Elgg\Collections\CollectionItemInterface;
6
7
/**
8
 * Represents a registered group tool option
9
 *
10
 * @property string      $name          Module name
11
 * @property string      $label         Module title
12
 * @property bool        $default_on    Enabled by default
13
 * @property int         $priority      Priority
14
 */
15
class Tool implements CollectionItemInterface {
16
17
	/**
18
	 * @var string
19
	 */
20
	public $name;
21
22
	/**
23
	 * @var array
24
	 */
25
	protected $options;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param string $name    Tool name
31
	 * @param array  $options Tool options
32
	 */
33 886
	public function __construct($name, array $options = []) {
34 886
		$this->name = $name;
35
36
		$defaults = [
37
			'label' => null,
38
			'default_on' => true,
39
			'priority' => 500,
40
		];
41
42 886
		$this->options = array_merge($defaults, $options);
43
	}
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48 2
	public function __get($name) {
49 2
		switch ($name) {
50 2
			case 'label' :
51 2
				return $this->getLabel();
52
53 2
			case 'default_on' :
54 1
				return $this->isEnabledByDefault();
55
		}
56
57 2
		return elgg_extract($name, $this->options);
58
	}
59
60
	/**
61
	 *
62
	 */
63 1
	public function __set($name, $value) {
64 1
		$this->options[$name] = $value;
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70 886
	public function getID() {
71 886
		return $this->name;
72
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77
	public function getPriority() {
78
		return $this->priority;
79
	}
80
81
	/**
82
	 * Get tool label
83
	 *
84
	 * @return string
85
	 */
86 2
	public function getLabel() {
87 2
		$label = elgg_extract('label', $this->options);
88 2
		if (isset($label)) {
89 1
			return $label;
90
		}
91
92 2
		return elgg_echo("groups:tool:{$this->name}");
93
	}
94
95
	/**
96
	 * Get tool description
97
	 *
98
	 * @return string|null
99
	 */
100 1
	public function getDescription(): ?string {
101 1
		$lan_key = "groups:tool:{$this->name}:description";
102 1
		if (!elgg_language_key_exists($lan_key)) {
103 1
			return null;
104
		}
105
		
106 1
		return elgg_echo($lan_key);
107
	}
108
109
	/**
110
	 * Is the tool enabled by default?
111
	 * @return bool
112
	 */
113 3
	public function isEnabledByDefault() {
114 3
		$default_on = elgg_extract('default_on', $this->options, true);
115
116 3
		if ($default_on === true || $default_on === 'yes') {
117 2
			return true;
118
		}
119
120 2
		return false;
121
	}
122
123
	/**
124
	 * Get metadata name for DB storage
125
	 * @return string
126
	 */
127 47
	public function mapMetadataName() {
128 47
		return "{$this->name}_enable";
129
	}
130
131
	/**
132
	 * Get metadata value for DB storage
133
	 *
134
	 * @param mixed $value Input value
135
	 *                     Defaults to 'default_on' property
136
	 *
137
	 * @return string
138
	 */
139 47
	public function mapMetadataValue($value = null) {
140 47
		if (!isset($value)) {
141 1
			$value = $this->default_on;
142
		}
143
144 47
		if ($value === 'yes' || $value === true) {
145 23
			return 'yes';
146
		}
147
148 27
		return 'no';
149
	}
150
}
151