Passed
Push — master ( 57d36e...277087 )
by Fabio
05:57
created

TShellDbCronAction::getModuleClass()   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
/**
4
 * TShellDbCronAction class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
 
11
namespace Prado\Util\Cron;
12
13
use Prado\Exceptions\TInvalidDataValueException;
14
use Prado\Exceptions\TApplicationException;
15
use Prado\Prado;
16
use Prado\Shell\TShellAppAction;
17
use Prado\Shell\TShellWriter;
18
19
/**
20
 * TShellDbCronAction class.
21
 *
22
 * TShellDbCronAction extends {@link TShellCronAction} to implement
23
 * additional commands {@link addTask add}, {@link updateTask update},
24
 * and {@link removeTask}.
25
 *
26
 * @author Brad Anderson <[email protected]>
27
 * @package Prado\Util\Cron
28
 * @since 4.2.0
29
 */
30
class TShellDbCronAction extends TShellCronAction
31
{
32
	protected $description = 'Runs the Application internal TDbCronModule Pending Tasks.
33
		commands are: tasks, list, help, add, update, remove';
34
	
35
	/**
36
	 * Overrides parent getModuleClass to return the TDbCronModule class.
37
	 * @return string the DbCron Class to find
38
	 */
39
	public function getModuleClass()
40
	{
41
		return 'Prado\\Util\\Cron\\TDbCronModule';
42
	}
43
	
44
	/**
45
	 * Displays the help for specific tasks, or in general
46
	 * @param string $helpcmd the module servicing the action
47
	 */
48
	public function cronHelp($helpcmd)
49
	{
50
		if ($helpcmd == 'add') {
51
			$this->_outWriter->writeLine("\nhelp for cron add command");
52
			$this->_outWriter->write("usage: ");
53
			$this->_outWriter->writeLine("add (task name) (task id) (schedule) [property=value] [otherProperties=values]", [TShellWriter::BLUE, TShellWriter::BOLD]);
54
			$this->_outWriter->writeLine("example: php prado-cli.php app ./app_dir cron add newTaskName taskID '* * * * *' ModuleId=mymodule UserId=admin PropertyA=value333\n");
55
		} elseif ($helpcmd == 'update') {
56
			$this->_outWriter->writeLine("\nhelp for cron update command");
57
			$this->_outWriter->write("usage: ");
58
			$this->_outWriter->writeLine("update (task name) [schedule='_ _ _ _ _'] [property=value] [otherProperties=values]", [TShellWriter::BLUE, TShellWriter::BOLD]);
59
			$this->_outWriter->writeLine("example: php prado-cli.php app ./app_dir cron update aTaskName 'schedule=* * * * *' ModuleId=mymodule UserId=admin PropertyA=value333\n");
60
		} elseif ($helpcmd == 'remove') {
61
			$this->_outWriter->writeLine("\nhelp for cron remove command");
62
			$this->_outWriter->write("usage: ");
63
			$this->_outWriter->writeLine("remove (task name)", [TShellWriter::BLUE, TShellWriter::BOLD]);
64
			$this->_outWriter->writeLine("example: php prado-cli.php app ./app_dir cron remove aTaskName\n");
65
		} elseif (parent::cronHelp($helpcmd)) {
66
			$this->_outWriter->writeLine("	add - adds a task from the task infos.");
67
			$this->_outWriter->writeLine("	update - this updates a task.");
68
			$this->_outWriter->writeLine("	remove - removes a task.");
69
		}
70
	}
71
	
72
	/**
73
	 * Overrides parent cronCommand to handle "add", "update", and "remove" actions.
74
	 * @param Prado\Util\Cron\TCronModule $module the module servicing the action
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\Cron\TCronModule 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...
75
	 * @param string $cmd the command being executed
76
	 * @param array $args the arguments to the shell command
77
	 * @return string the DbCron Class to find
78
	 */
79
	public function cronCommand($module, $cmd, $args)
80
	{
81
		$handled = false;
82
		
83
		if ($cmd == 'add') {
84
			$this->addTask($module, $args);
85
			$handled = true;
86
		} elseif ($cmd == 'update') {
87
			$this->updateTask($module, $args);
88
			$handled = true;
89
		} elseif ($cmd == 'remove') {
90
			$this->removeTask($module, $args);
91
			$handled = true;
92
		}
93
		
94
		return $handled;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $handled returns the type boolean which is incompatible with the documented return type string.
Loading history...
95
	}
96
	
97
	/**
98
	 * adds a task to the database with its name, task id, schedule, and other properties.
99
	 * @param Prado\Util\Cron\TDbCronModule $module the module servicing the action
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\Cron\TDbCronModule 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...
100
	 * @param array $args command arguments
101
	 */
102
	public function addTask($module, $args)
103
	{
104
		$taskName = $args[4] ?? null;
105
		$id = $args[5] ?? null;
106
		$schedule = $args[6] ?? null;
107
		
108
		if (!$taskName) {
109
			$this->_outWriter->writeError("Cannot add a task without a name");
110
			return;
111
		}
112
		if (!$id) {
113
			$this->_outWriter->writeError("Cannot add a task without a task id");
114
			return;
115
		}
116
		if (!$schedule) {
117
			$this->_outWriter->writeError("Cannot add a task without a schedule");
118
			return;
119
		}
120
		
121
		$exists = $module->taskExists($taskName);
122
		if ($exists) {
123
			$this->_outWriter->writeError("'{$taskName}' already exists in the database");
124
			return;
125
		}
126
		$infos = $module->getTaskInfos();
127
		$info = null;
128
		foreach ($infos as $i) {
129
			if ($i->getName() == $id) {
130
				$info = $i;
131
				break;
132
			}
133
		}
134
		if (!$info) {
135
			$this->_outWriter->writeError("Task ID '{$id}' could not be found");
136
			return;
137
		}
138
		$s = new TTimeScheduler();
139
		try {
140
			$s->setSchedule($schedule);
141
		} catch (TInvalidDataValueException $e) {
142
			$this->_outWriter->writeError("Schedule '{$schedule}' is not a valid schedule");
143
			return;
144
		}
145
		
146
		$task = $module->instanceTask($info->getTask());
147
		$task->setName($taskName);
148
		$task->setSchedule($schedule);
149
		for ($i = 7; $i < count($args); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
150
			$parts = explode('=', $args[$i]);
151
			$parts[0] = trim($parts[0]);
152
			$property = strtolower($parts[0]);
153
			if ($task->canSetProperty($property)) {
154
				$property = 'set' . $property;
155
				$task->$property(trim($parts[1]));
156
			} else {
157
				$this->_outWriter->writeError("Task Property '{$parts[0]}' is not found");
158
				return;
159
			}
160
		}
161
		$module->addTask($task);
162
		
163
		$this->_outWriter->writeLine("Task '{$taskName}' was added to the database\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
164
	}
165
	
166
	/**
167
	 * updates a task in the database by its name for its schedule, userid, moduleid, and other properties.
168
	 * @param Prado\Util\Cron\TDbCronModule $module the module servicing the action
169
	 * @param array $args command arguments
170
	 */
171
	public function updateTask($module, $args)
172
	{
173
		$taskName = $args[4] ?? null;
174
		
175
		if (!$taskName) {
176
			$this->_outWriter->writeError("Cannot update a task without a name");
177
			return;
178
		}
179
		
180
		$task = $module->getTask($taskName);
181
		if (!$task) {
182
			$this->_outWriter->writeError("Task '{$taskName}' is not found");
183
			return;
184
		}
185
		if (count($args) <= 5) {
186
			$this->_outWriter->writeError("No given properties to change");
187
			return;
188
		}
189
		for ($i = 5; $i < count($args); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
190
			$parts = explode('=', $args[$i]);
191
			$parts[0] = trim($parts[0]);
192
			$property = strtolower($parts[0]);
193
			if ($task->canSetProperty($property)) {
194
				if ($property === 'schedule') {
195
					$s = new TTimeScheduler();
196
					try {
197
						$s->setSchedule($parts[1]);
198
					} catch (TInvalidDataValueException $e) {
199
						$this->_outWriter->writeError("Schedule '{$parts[1]}' is not a valid schedule");
200
						return;
201
					}
202
				}
203
				$property = 'set' . $property;
204
				$task->$property(trim($parts[1]));
205
			} else {
206
				$this->_outWriter->writeError("Task Property '{$parts[0]}' is not found");
207
				return;
208
			}
209
		}
210
		$module->updateTask($task);
211
		$this->_outWriter->writeLine("Task '{$taskName}' was updated in the database\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
212
	}
213
	
214
	/**
215
	 * rumoves a task in the database by its name.
216
	 * @param Prado\Util\Cron\TDbCronModule $module the module servicing the action
217
	 * @param array $args command arguments
218
	 */
219
	public function removeTask($module, $args)
220
	{
221
		if (!($taskName = $args[4] ?? null)) {
222
			$this->_outWriter->writeError("Cannot remove a task without a name");
223
			return;
224
		}
225
		$exists = $module->taskExists($taskName);
226
		if (!$exists) {
227
			$this->_outWriter->writeError("'{$taskName}' does not exist in the database");
228
			return;
229
		}
230
		$result = $module->removeTask($taskName);
231
		
232
		if ($result) {
233
			$this->_outWriter->writeLine("'{$taskName}' was successfully removed.\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
234
		} else {
235
			$this->_outWriter->writeError("'{$taskName}' could not be removed.\n");
236
		}
237
	}
238
}
239