Passed
Push — master ( 5fd064...896ccb )
by Fabio
04:59
created

TDbParameterAction::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * TDbParameterAction class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Shell\Actions
9
 */
10
11
namespace Prado\Shell\Actions;
12
13
use Prado\Prado;
14
use Prado\Shell\TShellAction;
15
use Prado\Shell\TShellWriter;
16
use Prado\TPropertyValue;
17
18
/**
19
 * TDbParameterAction class.
20
 *
21
 * The indexes, gets, and sets the TDbParameterModule.
22
 *
23
 * @author Brad Anderson <belisoful[at]icloud[dot]com>
24
 * @package Prado\Shell\Actions
25
 * @since 4.2.0
26
 */
27
class TDbParameterAction extends TShellAction
28
{
29
	protected $action = 'param';
30
	protected $methods = ['index', 'get', 'set'];
31
	protected $parameters = [null, 'param-key', ['param-key', 'param-value']];
32
	protected $optional = [null, null, null];
33
	protected $description = [
34
		'Provides indexing, getting and setting parameters in the Database.',
35
		'Displays all the variables in the database.',
36
		'Gets a specific parameter by <param-key>.',
37
		'Sets a specific parameter <param-key> to <param-value>.'];
38
	
39
	private $_allParams = false;
40
	
41
	/**
42
	 *
43
	 */
44
	public function getAll()
45
	{
46
		return $this->_allParams;
47
	}
48
	
49
	/**
50
	 * @param bool $value If this is called, set the property to true
51
	 */
52
	public function setAll($value)
53
	{
54
		$this->_allParams = TPropertyValue::ensureBoolean($value === '' ? true : $value);
0 ignored issues
show
introduced by
The condition $value === '' is always false.
Loading history...
55
	}
56
	
57
	/**
58
	 * Properties for the action set by parameter
59
	 * @param string $actionID the action being executed
60
	 * @return array properties for the $actionID
61
	 */
62
	public function options($actionID): array
63
	{
64
		if ($actionID === 'index') {
65
			return ['all'];
66
		}
67
		return [];
68
	}
69
	
70
	/**
71
	 * Aliases for the properties to be set by parameter
72
	 * @param string $actionID the action being executed
73
	 * @return array<alias, property> properties for the $actionID
74
	 */
75
	public function optionAliases(): array
76
	{
77
		return ['a' => 'all'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('a' => 'all') returns the type array<string,string> which is incompatible with the documented return type Prado\Shell\Actions\property[].
Loading history...
78
	}
79
	
80
	/**
81
	 * display the database parameter key values.
82
	 * @param array $args parameters
83
	 * @return bool is the action handled
84
	 */
85
	public function actionIndex($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
	public function actionIndex(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
	{
87
		$writer = $this->getWriter();
88
		if (!($module = $this->getDbParameterModule())) {
89
			$writer->writeError('No TDbParameterModule found to set parameters');
90
			return;
91
		}
92
		
93
		$params = Prado::getApplication()->getParameters();
94
		$len = 0;
95
		foreach ($params as $key => $value) {
96
			$_len = strlen($key);
97
			if ($len < $_len) {
98
				$len = $_len;
99
			}
100
		}
101
		$writer->writeLine();
102
		$writer->write($writer->pad($writer->format('Parameter Key', TShellWriter::UNDERLINE), $len + 1));
103
		$writer->writeLine('Parameter Key', TShellWriter::UNDERLINE);
104
		foreach ($params as $key => $value) {
105
			if (!$this->getAll() && !$module->exists($key)) {
106
				continue;
107
			}
108
			$writer->write($writer->pad($key, $len + 1));
109
			if (is_object($value)) {
110
				$value = '(object)';
111
			}
112
			if (is_array($value)) {
113
				$value = '(array)';
114
			}
115
			$writer->writeLine($writer->wrapText($value, $len + 1));
116
		}
117
		$writer->writeLine();
118
		return true;
119
	}
120
	
121
	/**
122
	 * gets a parameter value
123
	 * @param array $args parameters
124
	 * @return bool is the action handled
125
	 */
126
	public function actionGet($args)
127
	{
128
		$writer = $this->getWriter();
129
		$writer->writeLine();
130
		
131
		if (!($key = ($args[1] ?? null))) {
132
			$writer->writeError('Get Parameter needs a key');
133
			return true;
134
		}
135
		$writer->write('Parameter ');
136
		$writer->write($key, [TShellWriter::BLUE, TShellWriter::BOLD]);
137
		$writer->write(': ');
138
		$value = Prado::getApplication()->getParameters()[$key];
139
		
140
		$writer->writeLine(Prado::varDump($value));
141
		$writer->writeLine();
142
		return true;
143
	}
144
	
145
	/**
146
	 * Sets a parameter value
147
	 * @param array $args parameters
148
	 * @return bool is the action handled
149
	 */
150
	public function actionSet($args)
151
	{
152
		$writer = $this->getWriter();
153
		$writer->writeLine();
154
		
155
		if (!($key = ($args[1] ?? null))) {
156
			$this->getWriter()->writeError('Get Parameter needs a key');
157
			return true;
158
		}
159
		if (!($value = ($args[2] ?? null))) {
160
			$this->getWriter()->writeError('Set Parameter needs a key and Value');
161
			return true;
162
		}
163
		$autoload = TPropertyValue::ensureBoolean($args[3] ?? true);
164
		
165
		if (!($module = $this->getDbParameterModule())) {
166
			$writer->writeError('No TDbParameterModule found to set parameters');
167
			return true;
168
		}
169
		
170
		$module->set($key, $value, $autoload, false);
171
		
172
		$writer->write('Set Parameter ');
173
		$writer->write($key, [TShellWriter::BLUE, TShellWriter::BOLD]);
174
		$writer->write(' To: ');
175
		$writer->writeLine(Prado::varDump($value));
176
		$writer->writeLine();
177
		return true;
178
	}
179
	
180
	/**
181
	 * get the TDBParameterModule from the Application
182
	 * @return null|Prado\Util\TDBParameterModule
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\TDBParameterModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
	 */
184
	public function getDbParameterModule()
185
	{
186
		foreach (Prado::getApplication()->getModulesByType('Prado\\Util\\TDbParameterModule') as $module) {
187
			if ($module) {
188
				return $module;
189
			}
190
		}
191
		return null;
192
	}
193
}
194