Passed
Push — master ( 896ccb...11e3a6 )
by Fabio
07:20 queued 01:06
created

TDbParameterAction::setDbParameterModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
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
	private $_dbparam = false;
42
	
43
	/**
44
	 *
45
	 */
46
	public function getAll()
47
	{
48
		return $this->_allParams;
49
	}
50
	
51
	/**
52
	 * @param bool $value If this is called, set the property to true
53
	 */
54
	public function setAll($value)
55
	{
56
		$this->_allParams = TPropertyValue::ensureBoolean($value === '' ? true : $value);
0 ignored issues
show
introduced by
The condition $value === '' is always false.
Loading history...
57
	}
58
	
59
	/**
60
	 * Properties for the action set by parameter
61
	 * @param string $actionID the action being executed
62
	 * @return array properties for the $actionID
63
	 */
64
	public function options($actionID): array
65
	{
66
		if ($actionID === 'index') {
67
			return ['all'];
68
		}
69
		return [];
70
	}
71
	
72
	/**
73
	 * Aliases for the properties to be set by parameter
74
	 * @param string $actionID the action being executed
75
	 * @return array<alias, property> properties for the $actionID
76
	 */
77
	public function optionAliases(): array
78
	{
79
		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...
80
	}
81
	
82
	/**
83
	 * display the database parameter key values.
84
	 * @param array $args parameters
85
	 * @return bool is the action handled
86
	 */
87
	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

87
	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...
88
	{
89
		$writer = $this->getWriter();
90
		if (!($module = $this->getDbParameterModule())) {
91
			$writer->writeError('No TDbParameterModule found to set parameters');
92
			return;
93
		}
94
		
95
		$params = Prado::getApplication()->getParameters();
96
		$len = 0;
97
		foreach ($params as $key => $value) {
98
			$_len = strlen($key);
99
			if ($len < $_len) {
100
				$len = $_len;
101
			}
102
		}
103
		$writer->writeLine();
104
		$writer->write($writer->pad($writer->format('Parameter Key', TShellWriter::UNDERLINE), $len + 1));
105
		$writer->writeLine('Parameter Key', TShellWriter::UNDERLINE);
106
		foreach ($params as $key => $value) {
107
			if (!$this->getAll() && !$module->exists($key)) {
108
				continue;
109
			}
110
			$writer->write($writer->pad($key, $len + 1));
111
			if (is_object($value)) {
112
				$value = '(object)';
113
			}
114
			if (is_array($value)) {
115
				$value = '(array)';
116
			}
117
			$writer->writeLine($writer->wrapText($value, $len + 1));
118
		}
119
		$writer->writeLine();
120
		return true;
121
	}
122
	
123
	/**
124
	 * gets a parameter value
125
	 * @param array $args parameters
126
	 * @return bool is the action handled
127
	 */
128
	public function actionGet($args)
129
	{
130
		$writer = $this->getWriter();
131
		$writer->writeLine();
132
		
133
		if (!($key = ($args[1] ?? null))) {
134
			$writer->writeError('Get Parameter needs a key');
135
			return true;
136
		}
137
		$writer->write('Parameter ');
138
		$writer->write($key, [TShellWriter::BLUE, TShellWriter::BOLD]);
139
		$writer->write(': ');
140
		$value = Prado::getApplication()->getParameters()[$key];
141
		
142
		$writer->writeLine(Prado::varDump($value));
143
		$writer->writeLine();
144
		return true;
145
	}
146
	
147
	/**
148
	 * Sets a parameter value
149
	 * @param array $args parameters
150
	 * @return bool is the action handled
151
	 */
152
	public function actionSet($args)
153
	{
154
		$writer = $this->getWriter();
155
		$writer->writeLine();
156
		
157
		if (!($key = ($args[1] ?? null))) {
158
			$this->getWriter()->writeError('Get Parameter needs a key');
159
			return true;
160
		}
161
		if (!($value = ($args[2] ?? null))) {
162
			$this->getWriter()->writeError('Set Parameter needs a key and Value');
163
			return true;
164
		}
165
		$autoload = TPropertyValue::ensureBoolean($args[3] ?? true);
166
		
167
		if (!($module = $this->getDbParameterModule())) {
168
			$writer->writeError('No TDbParameterModule found to set parameters');
169
			return true;
170
		}
171
		
172
		$module->set($key, $value, $autoload, false);
173
		
174
		$writer->write('Set Parameter ');
175
		$writer->write($key, [TShellWriter::BLUE, TShellWriter::BOLD]);
176
		$writer->write(' To: ');
177
		$writer->writeLine(Prado::varDump($value));
178
		$writer->writeLine();
179
		return true;
180
	}
181
	
182
	/**
183
	 * get the TDBParameterModule from the Application
184
	 * @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...
185
	 */
186
	public function getDbParameterModule()
187
	{
188
		if ($this->_dbparam === false) {
189
			$this->_dbparam = null;
190
			$app = Prado::getApplication();
191
			foreach ($app->getModulesByType('Prado\\Util\\TDbParameterModule') as $id => $module) {
192
				if ($this->_dbparam = $app->getModule($id)) {
193
					break;
194
				}
195
			}
196
		}
197
		return $this->_dbparam;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_dbparam also could return the type Prado\TModule|true which is incompatible with the documented return type Prado\Prado\Util\TDBParameterModule|null.
Loading history...
198
	}
199
	
200
	/**
201
	 * get the TPermissionsManager from the Application
202
	 * @param Prado\Util\TDbParameterModule $dbparam
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...
203
	 */
204
	public function setDbParameterModule($dbparam)
205
	{
206
		$this->_dbparam = $dbparam;
207
	}
208
}
209