Completed
Push — master ( 945a1c...6798bf )
by Henry
07:37
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->getOptionArray()) ? $this->success() : $this->error($haltOnError);
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)
0 ignored issues
show
The expression $settings of type array|object<IdiormResultSet>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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