Passed
Push — master ( 5fd064...896ccb )
by Fabio
04:59
created

TShellDbCronAction::actionUpdate()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 41
rs 8.1795
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;
0 ignored issues
show
Bug introduced by
The type Prado\Shell\TShellAppAction 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...
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 $methods = ['run', 'tasks', 'index', 'add', 'update', 'remove'];
33
	protected $parameters = [null, null, null, ['task-name', 'task-id', 'schedule'], ['task-name'], ['task-name']];
34
	protected $optional = [null, null, null, ['parameters...'], ['parameters...'], []];
35
	protected $description = ['Manages DbCron time-based services',
36
		'Runs the DbCron Pending Tasks.',
37
		'Displays the Cron tasks configured in the application and database.',
38
		'Displays the registered Cron tasks information.',
39
		'Adds a Cron task to the database.',
40
		'Updates a Cron task in the database.',
41
		'Removes a Cron task from the database.'
42
	];
43
	
44
	/**
45
	 * Overrides parent getModuleClass to return the TDbCronModule class.
46
	 * @return string the DbCron Class to find
47
	 */
48
	public function getModuleClass()
49
	{
50
		return 'Prado\\Util\\Cron\\TDbCronModule';
51
	}
52
	
53
	/**
54
	 * adds a task to the database with its name, task id, schedule, and other properties.
55
	 * @param array $args command arguments
56
	 */
57
	public function actionAdd($args)
58
	{
59
		$module = $this->getCronModule();
60
		$taskName = $args[1] ?? null;
61
		$id = $args[2] ?? null;
62
		$schedule = $args[3] ?? null;
63
		
64
		if (!$taskName) {
65
			$this->_outWriter->writeError("Cannot add a task without a name");
66
			return true;
67
		}
68
		if (!$id) {
69
			$this->_outWriter->writeError("Cannot add a task without a task id");
70
			return true;
71
		}
72
		if (!$schedule) {
73
			$this->_outWriter->writeError("Cannot add a task without a schedule");
74
			return true;
75
		}
76
		
77
		$exists = $module->taskExists($taskName);
0 ignored issues
show
Bug introduced by
The method taskExists() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
		/** @scrutinizer ignore-call */ 
78
  $exists = $module->taskExists($taskName);
Loading history...
78
		if ($exists) {
79
			$this->_outWriter->writeError("'{$taskName}' already exists in the database");
80
			return true;
81
		}
82
		$infos = $module->getTaskInfos();
0 ignored issues
show
Bug introduced by
The method getTaskInfos() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
		/** @scrutinizer ignore-call */ 
83
  $infos = $module->getTaskInfos();
Loading history...
83
		$info = null;
84
		foreach ($infos as $i) {
85
			if ($i->getName() == $id) {
86
				$info = $i;
87
				break;
88
			}
89
		}
90
		if (!$info) {
91
			$this->_outWriter->writeError("Task ID '{$id}' could not be found");
92
			return true;
93
		}
94
		$s = new TTimeScheduler();
95
		try {
96
			$s->setSchedule($schedule);
97
		} catch (TInvalidDataValueException $e) {
98
			$this->_outWriter->writeError("Schedule '{$schedule}' is not a valid schedule");
99
			return true;
100
		}
101
		
102
		$task = $module->instanceTask($info->getTask());
0 ignored issues
show
Bug introduced by
The method instanceTask() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
		/** @scrutinizer ignore-call */ 
103
  $task = $module->instanceTask($info->getTask());
Loading history...
103
		$task->setName($taskName);
104
		$task->setSchedule($schedule);
105
		for ($i = 4; $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...
106
			$parts = explode('=', $args[$i]);
107
			$parts[0] = trim($parts[0]);
108
			$property = strtolower($parts[0]);
109
			if ($task->canSetProperty($property)) {
110
				$property = 'set' . $property;
111
				$task->$property(trim($parts[1]));
112
			} else {
113
				$this->_outWriter->writeError("Task Property '{$parts[0]}' is not found");
114
				return true;
115
			}
116
		}
117
		$module->addTask($task);
0 ignored issues
show
Bug introduced by
The method addTask() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
		$module->/** @scrutinizer ignore-call */ 
118
           addTask($task);
Loading history...
118
		
119
		$this->_outWriter->writeLine("Task '{$taskName}' was added to the database\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
120
		return true;
121
	}
122
	
123
	/**
124
	 * updates a task in the database by its name for its schedule, username, moduleid, and other properties.
125
	 * @param array $args command arguments
126
	 */
127
	public function actionUpdate($args)
128
	{
129
		$module = $this->getCronModule();
130
		if (!($taskName = ($args[1] ?? null))) {
131
			$this->_outWriter->writeError("Cannot update a task without a name");
132
			return true;
133
		}
134
		
135
		$task = $module->getTask($taskName);
0 ignored issues
show
Bug introduced by
The method getTask() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
		/** @scrutinizer ignore-call */ 
136
  $task = $module->getTask($taskName);
Loading history...
136
		if (!$task) {
137
			$this->_outWriter->writeError("Task '{$taskName}' is not found");
138
			return true;
139
		}
140
		if (count($args) < 3) {
141
			$this->_outWriter->writeError("No given properties to change");
142
			return true;
143
		}
144
		for ($i = 2; $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...
145
			$parts = explode('=', $args[$i]);
146
			$parts[0] = trim($parts[0]);
147
			$property = strtolower($parts[0]);
148
			if ($task->canSetProperty($property)) {
149
				if ($property === 'schedule') {
150
					$s = new TTimeScheduler();
151
					try {
152
						$s->setSchedule($parts[1]);
153
					} catch (TInvalidDataValueException $e) {
154
						$this->_outWriter->writeError("Schedule '{$parts[1]}' is not a valid schedule");
155
						return true;
156
					}
157
				}
158
				$property = 'set' . $property;
159
				$task->$property(trim($parts[1]));
160
			} else {
161
				$this->_outWriter->writeError("Task Property '{$parts[0]}' is not found");
162
				return true;
163
			}
164
		}
165
		$module->updateTask($task);
0 ignored issues
show
Bug introduced by
The method updateTask() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
		$module->/** @scrutinizer ignore-call */ 
166
           updateTask($task);
Loading history...
166
		$this->_outWriter->writeLine("Task '{$taskName}' was updated in the database\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
167
		return true;
168
	}
169
	
170
	/**
171
	 * removes a task in the database by its name.
172
	 * @param array $args command arguments
173
	 */
174
	public function actionRemove($args)
175
	{
176
		$module = $this->getCronModule();
177
		if (!($taskName = $args[1] ?? null)) {
178
			$this->_outWriter->writeError("Cannot remove a task without a name");
179
			return true;
180
		}
181
		$exists = $module->taskExists($taskName);
182
		if (!$exists) {
183
			$this->_outWriter->writeError("'{$taskName}' does not exist in the database");
184
			return true;
185
		}
186
		$result = $module->removeTask($taskName);
0 ignored issues
show
Bug introduced by
The method removeTask() does not exist on Prado\TModule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
		/** @scrutinizer ignore-call */ 
187
  $result = $module->removeTask($taskName);
Loading history...
187
		
188
		if ($result) {
189
			$this->_outWriter->writeLine("'{$taskName}' was successfully removed.\n", [TShellWriter::GREEN, TShellWriter::BOLD]);
190
		} else {
191
			$this->_outWriter->writeError("'{$taskName}' could not be removed.\n");
192
		}
193
		return true;
194
	}
195
}
196