anonymous//app/Cli.php$0   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 19
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * CLI file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
/**
15
 * CLI class.
16
 */
17
class Cli
18
{
19
	/** @var \League\CLImate\CLImate CLImate instance. */
20
	public $climate;
21
22
	/** @var bool Php support exec */
23
	public $exec = true;
24
25
	/**
26
	 * Construct.
27
	 */
28
	public function __construct()
29
	{
30
		$this->exec = \function_exists('exec');
31
		$this->climate = new \League\CLImate\CLImate();
32
		if (!$this->exec) {
33
			$this->climate->setUtil(new \League\CLImate\Util\UtilFactory(new class() extends \League\CLImate\Util\System\System {
34
				public function width()
35
				{
36
					return 120;
37
				}
38
39
				public function height()
40
				{
41
					return 40;
42
				}
43
44
				protected function systemHasAnsiSupport()
45
				{
46
					return true;
47
				}
48
49
				public function exec($command, $full = false)
50
				{
51
					return '';
52
				}
53
			}));
54
		}
55
		$this->climate->clear();
56
		if (\function_exists('getmyuid') && getmyuid() !== fileowner(__FILE__)) {
57
			$this->climate->to('error')->lightRed('Error:  YetiForce CLI works only on the OS user who owns the CRM files');
58
			return;
59
		}
60
		if (\PHP_SAPI !== 'cli') {
61
			$this->climate->to('error')->lightRed('Error: YetiForce CLI only works from the operating system console (CLI)');
62
			return;
63
		}
64
		$this->climate->lightGreen()->border('─', 200);
65
		$this->climate->tab(2)->lightGreen('Y e t i F o r c e     C L I');
66
		$this->climate->lightGreen()->border('─', 200);
67
		$this->climate->white('Version: ' . Version::get() . ' | CRM URL: ' . \Config\Main::$site_URL);
0 ignored issues
show
Bug introduced by
The type Config\Main 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...
68
		$this->climate->lightGreen()->border('─', 200);
69
		\App\User::setCurrentUserId(\Users::getActiveAdminId());
70
		\App\Language::setTemporaryLanguage('en-US');
71
72
		$this->climate->arguments->add([
73
			'module' => [
74
				'prefix' => 'm',
75
				'description' => 'Module name',
76
			],
77
			'action' => [
78
				'prefix' => 'a',
79
				'description' => 'Module action name',
80
			],
81
			'help' => [
82
				'prefix' => 'h',
83
				'description' => 'Help',
84
			],
85
		]);
86
		$this->climate->arguments->parse();
87
		if ($this->climate->arguments->defined('help')) {
88
			$this->showHelp();
89
			$this->climate->usage();
90
		} elseif ($this->climate->arguments->defined('module') && !$this->climate->arguments->defined('action') && !empty($this->climate->arguments->get('module'))) {
91
			$this->actionsList($this->climate->arguments->get('module'));
92
		} elseif ($this->climate->arguments->defined('module') && $this->climate->arguments->defined('action')) {
93
			$className = "\\App\\Cli\\{$this->climate->arguments->get('module')}";
94
			$instance = new $className($this);
95
			if (!method_exists($instance, $this->climate->arguments->get('action'))) {
96
				$this->climate->to('error')->lightRed("Error: Action '{$this->climate->arguments->get('action')}' does not exist in '{$this->climate->arguments->get('module')}'");
97
				return;
98
			}
99
			$this->climate->backgroundBlue()->out($instance->methods[$this->climate->arguments->get('action')]);
100
			$this->climate->border('─', 200);
101
			\call_user_func([$instance, $this->climate->arguments->get('action')]);
102
		} else {
103
			$this->modulesList();
104
		}
105
	}
106
107
	/**
108
	 * Show modules list.
109
	 *
110
	 * @return void
111
	 */
112
	public function modulesList(): void
113
	{
114
		if (!$this->exec) {
115
			$this->showHelp();
116
			$this->climate->usage();
117
			return;
118
		}
119
		$modules = $this->getModulesList();
120
		$modules['Exit'] = 'Exit';
121
		$input = $this->climate->radio('Module:', $modules);
122
		$module = $input->prompt();
123
		if ('Exit' === $module || empty($module)) {
124
			return;
125
		}
126
		$this->climate->clear();
127
		$this->actionsList($module);
128
	}
129
130
	/**
131
	 * Get modules list.
132
	 *
133
	 * @return string[]
134
	 */
135
	private function getModulesList(): array
136
	{
137
		$modules = [];
138
		foreach (new \DirectoryIterator(ROOT_DIRECTORY . '/app/Cli') as $fileInfo) {
139
			if ($fileInfo->isFile() && 'Base' !== $fileInfo->getBasename('.php')) {
140
				$module = $fileInfo->getBasename('.php');
141
				$className = "\\App\\Cli\\{$module}";
142
				$instance = new $className($this);
143
				$modules[$module] = $instance->moduleName;
144
			}
145
		}
146
		return $modules;
147
	}
148
149
	/**
150
	 * Show actions list.
151
	 *
152
	 * @param string $module
153
	 *
154
	 * @return void
155
	 */
156
	public function actionsList(string $module): void
157
	{
158
		$className = "\\App\\Cli\\{$module}";
159
		if (!class_exists($className)) {
160
			$this->climate->to('error')->lightRed("Error: Module '$module' does not exist");
161
			return;
162
		}
163
		if (!$this->exec) {
164
			$this->showHelp();
165
			$this->climate->usage();
166
			return;
167
		}
168
		$instance = new $className($this);
169
		$input = $this->climate->radio('Action:', array_merge($instance->methods, ['Exit' => 'Exit']));
170
		$action = $input->prompt();
171
		$this->climate->clear();
172
		if ('Exit' === $action) {
173
			$this->modulesList();
174
		} else {
175
			\call_user_func([$instance, $action]);
176
		}
177
	}
178
179
	/**
180
	 * Show help.
181
	 *
182
	 * @return void
183
	 */
184
	private function showHelp(): void
185
	{
186
		if ($this->climate->arguments->defined('module')) {
187
			$module = $this->climate->arguments->get('module');
188
			$className = "\\App\\Cli\\{$module}";
189
			if (!class_exists($className)) {
190
				$this->climate->to('error')->lightRed("Error: Module '{$this->climate->arguments->get('module')}' does not exist");
191
				return;
192
			}
193
			$instance = new $className($this);
194
			if ($this->climate->arguments->defined('action') && !empty($this->climate->arguments->get('action'))) {
195
				if (!method_exists($instance, $this->climate->arguments->get('action'))) {
196
					$this->climate->to('error')->lightRed("Error: Action '{$this->climate->arguments->get('action')}' does not exist in '{$this->climate->arguments->get('module')}'");
197
					return;
198
				}
199
				$instance->helpMode = true;
200
				\call_user_func([$instance, $this->climate->arguments->get('action')]);
201
			} else {
202
				$this->climate->white('Action list for module ' . $this->climate->arguments->get('module'));
203
				$this->climate->columns(array_merge([' > Action name <' => ' > Description <'], $instance->methods));
204
				$this->climate->lightGreen()->border('─', 200);
205
				foreach (array_keys($instance->methods) as $method) {
206
					$this->climate->white("php cli.php -m $module -a $method");
207
				}
208
				$this->climate->lightGreen()->border('─', 200);
209
			}
210
		} else {
211
			$modules = $this->getModulesList();
212
			$modules = array_keys($modules);
213
			$this->climate->white('Modules list:')->columns($modules);
214
			$this->climate->lightGreen()->border('─', 200);
215
			foreach ($modules as $module) {
216
				$this->climate->white("php cli.php -m $module");
217
			}
218
			$this->climate->lightGreen()->border('─', 200);
219
		}
220
	}
221
}
222