Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Console/Command/Setting.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
use Redaxscript\Model;
6
use function str_pad;
7
8
/**
9
 * children class to execute the setting command
10
 *
11
 * @since 3.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Console
15
 * @author Henry Ruhs
16
 */
17
18
class Setting extends CommandAbstract
19
{
20
	/**
21
	 * array of the command
22
	 *
23
	 * @var array
24
	 */
25
26
	protected $_commandArray =
27
	[
28
		'setting' =>
29
		[
30
			'description' => 'Setting command',
31
			'argumentArray' =>
32
			[
33
				'list' =>
34
				[
35
					'description' => 'List the settings'
36
				],
37
				'set' =>
38
				[
39
					'description' => 'Set the setting',
40
					'optionArray' =>
41
					[
42
						'key' =>
43
						[
44
							'description' => 'Required setting key'
45
						],
46
						'value' =>
47
						[
48
							'description' => 'Required setting value'
49
						]
50
					]
51
				]
52
			]
53
		]
54
	];
55
56
	/**
57
	 * run the command
58
	 *
59
	 * @param string $mode name of the mode
60
	 *
61
	 * @since 3.0.0
62
	 *
63
	 * @return string|null
64
	 */
65
66 4
	public function run(string $mode = null) : ?string
67
	{
68 4
		$parser = new Parser($this->_request);
69 4
		$parser->init($mode);
70
71
		/* run command */
72
73 4
		$argumentKey = $parser->getArgument(1);
74 4
		$haltOnError = (bool)$parser->getOption('halt-on-error');
75 4
		if ($argumentKey === 'list')
76
		{
77 1
			return $this->_list();
78
		}
79 3
		if ($argumentKey === 'set')
80
		{
81 2
			return $this->_set($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Setting::_set() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82
		}
83 1
		return $this->getHelp();
84
	}
85
86
	/**
87
	 * list the settings
88
	 *
89
	 * @since 3.0.0
90
	 *
91
	 * @return string|null
92
	 */
93
94 1
	protected function _list() : ?string
95
	{
96 1
		$output = null;
97 1
		$settingModel = new Model\Setting();
98 1
		$settings = $settingModel->getAll();
99
100
		/* process settings */
101
102 1
		foreach ($settings as $setting)
103
		{
104 1
			$output .= str_pad($setting->name, 30) . $setting->value . PHP_EOL;
105
		}
106 1
		return $output;
107
	}
108
109
	/**
110
	 * set the setting
111
	 *
112
	 * @since 3.0.0
113
	 *
114
	 * @param array $optionArray
115
	 *
116
	 * @return bool
117
	 */
118
119 2
	protected function _set(array $optionArray = []) : bool
120
	{
121 2
		$settingModel = new Model\Setting();
122 2
		$key = $this->prompt('key', $optionArray);
123 2
		$value = $this->prompt('value', $optionArray);
124 2
		if ($key && $value)
125
		{
126 1
			return $settingModel->set($key, $value);
127
		}
128 1
		return false;
129
	}
130
}
131