Passed
Push — master ( 46bc20...476c9e )
by Fabio
07:16
created

TShellCronAction::actionTasks()   C

Complexity

Conditions 12
Paths 102

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 41
nc 102
nop 1
dl 0
loc 59
rs 6.95
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TShellCronAction 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\Prado;
14
use Prado\Shell\TShellAction;
15
use Prado\Shell\TShellWriter;
16
use Prado\Util\Cron\TCronModule;
17
18
/**
19
 * TShellCronAction class.
20
 *
21
 * Runs the TCronModule from the command line.   This will run all pending
22
 * tasks when the "cron" command is run.  Other sub-commands are "info" and
23
 * "tasks"; where "info" reports the possible cron tasks registered in the
24
 * application, and "tasks" reports the installed tasks being managed.
25
 *
26
 * @author Brad Anderson <[email protected]>
27
 * @since 4.2.0
28
 */
29
class TShellCronAction extends TShellAction
30
{
31
	protected $action = 'cron';
32
	protected $methods = ['run', 'tasks', 'index'];
33
	protected $parameters = [null, null, null];
34
	protected $optional = [null, null, null];
35
	protected $description = ['Manages Cron time-based services',
36
		'Runs the Cron Pending Tasks.',
37
		'Displays the Cron tasks configured in the application.',
38
		'Displays the registered Cron tasks information.'
39
	];
40
41
	private $_cron = false;
42
43
	/**
44
	 * @return string the Cron Class to find
45
	 */
46
	public function getModuleClass()
47
	{
48
		return 'Prado\\Util\\Cron\\TCronModule';
49
	}
50
51
	/**
52
	 * @return null|\Prado\Util\Cron\TCronModule returns the Cron Module of the applications
53
	 */
54
	public function getCronModule()
55
	{
56
		if ($this->_cron === false) {
57
			$app = Prado::getApplication();
58
			$moduleClass = $this->getModuleClass();
59
			$modules = $app->getModulesByType($moduleClass, false);
60
			$this->_cron = null;
61
			foreach ($modules as $id => $m) {
62
				if ($this->_cron = $app->getModule($id)) {
63
					break;
64
				}
65
			}
66
			if (!$this->_cron) {
67
				$this->_outWriter->writeError("A {$moduleClass} is not found");
68
				return null;
69
			}
70
		}
71
		if (!$this->_cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
72
			$this->_cron->attachBehavior(TCronModule::SHELL_LOG_BEHAVIOR, new TShellCronLogBehavior($this->getWriter()));
73
		}
74
		return $this->_cron;
75
	}
76
77
	/**
78
	 * @param null|\Prado\Util\Cron\TCronModule $cron sets the Cron Module
79
	 */
80
	public function setCronModule($cron)
81
	{
82
		$this->_cron = $cron;
83
	}
84
85
	/**
86
	 * @param string[] $args the arguments to the command line action
87
	 */
88
	public function actionRun($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

88
	public function actionRun(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
	{
90
		$module = $this->getCronModule();
91
		if (!$module) {
92
			return true;
93
		}
94
		$time = $module->getLastCronTime();
95
		$this->_outWriter->writeLine("\n Last cron run time was " . ($time == 0 ? 'never' : date('Y-m-d H:i:s TP', $time)) . '');
0 ignored issues
show
Bug introduced by
$time of type double is incompatible with the type integer|null expected by parameter $timestamp of date(). ( Ignorable by Annotation )

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

95
		$this->_outWriter->writeLine("\n Last cron run time was " . ($time == 0 ? 'never' : date('Y-m-d H:i:s TP', /** @scrutinizer ignore-type */ $time)) . '');
Loading history...
96
		$module->processPendingTasks();
97
		return true;
98
	}
99
100
	/**
101
	 * Shows configured tasks and their run status.  For TDbCronModule, this also shows the
102
	 * database tasks as well.
103
	 * @param mixed $args
104
	 */
105
	public function actionTasks($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

105
	public function actionTasks(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
	{
107
		$module = $this->getCronModule();
108
109
		$time = $module->getLastCronTime();
110
		$this->_outWriter->writeLine("\n Last cron run was " . ($time == 0 ? 'never' : date('Y-m-d H:i:s', (int) $time)) . "");
111
		$this->_outWriter->writeLine("The system time is " . date('Y-m-d H:i:s TP') . "\n");
112
		$tasks = $module->getTasks();
113
114
		$this->_outWriter->writeLine("  Application Cron Tasks: ", [TShellWriter::BOLD]);
115
		if (!count($tasks)) {
116
			$this->_outWriter->writeLine("     **  There are no configured Cron Tasks.  **\n");
117
			return true;
118
		}
119
		$rows = [];
120
		foreach ($tasks as $task) {
121
			$lastExecTime = $task->getLastExecTime();
122
			$count = $task->getProcessCount();
123
			if ($lastExecTime === null) {
124
				$lastrun = '-';
125
			} else {
126
				if (abs(time() - $lastExecTime) > 86400) {
127
					$f = 'Y-m-d H:i:s';
128
				} else {
129
					$f = 'H:i:s';
130
				}
131
				$lastrun = date($f, $lastExecTime);
0 ignored issues
show
Bug introduced by
$lastExecTime of type Prado\Util\Cron\numeric is incompatible with the type integer|null expected by parameter $timestamp of date(). ( Ignorable by Annotation )

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

131
				$lastrun = date($f, /** @scrutinizer ignore-type */ $lastExecTime);
Loading history...
132
				if (!$count) {
133
					$lastrun = '- (' . $lastrun . ')';
134
				}
135
			}
136
137
			$trigger = $task->getNextTriggerTime();
138
			if ($trigger === null) {
139
				$nextrun = '-';
140
			} else {
141
				if (abs($trigger - time()) > 86400) {
142
					$f = 'Y-m-d H:i:s';
143
				} else {
144
					$f = 'H:i:s';
145
				}
146
				$nextrun = date($f, $trigger) . ($task->getIsPending() ? '*' : '');
147
			}
148
149
			if (($user = $task->getUserName()) == null) {
150
				$user = $module->getDefaultUserName();
151
			}
152
153
			if ($task->getIsPending()) {
154
				$nextrun = $this->_outWriter->format($nextrun, [TShellWriter::GREEN, TShellWriter::BOLD]);
155
			}
156
157
			$rows[] = [$task->getName(), $task->getSchedule(), $task->getTask(), $lastrun, $nextrun, '@' . $user, $count];
158
		}
159
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Name', 'Schedule', 'Task', 'Last Run', 'Next Run', 'User', 'Run #'],
160
			'rows' => $rows]));
161
		$this->_outWriter->writeLine("\nAny 'next run' with a * means it is Pending\n");
162
163
		return true;
164
	}
165
166
	/**
167
	 * shows the registered tasks from the application for possible configuration
168
	 * or addition to TDbCronModule.
169
	 * @param mixed $args
170
	 */
171
	public function actionIndex($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

171
	public function actionIndex(/** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
	{
173
		$module = $this->getCronModule();
174
175
		$infos = $module->getTaskInfos(true);
176
		$this->_outWriter->writeLine();
177
		$this->_outWriter->writeLine("  Registered Cron Task Information: ", [TShellWriter::BOLD]);
178
		if (!count($infos)) {
179
			$this->_outWriter->writeLine("		(** No registered application tasks **)");
180
			return true;
181
		}
182
		$rows = [];
183
		foreach ($infos as $taskinfo) {
184
			$rows[] = [
185
					$this->_outWriter->format($taskinfo->getName(), [TShellWriter::BLUE, TShellWriter::BOLD]),
186
					$taskinfo->getTask(), $taskinfo->getModuleId(), $taskinfo->getTitle()
187
				];
188
			$rows[] = ['span' => $this->_outWriter->format('      ' . $taskinfo->getDescription(), TShellWriter::DARK_GRAY)];
189
		}
190
		$this->_outWriter->write($this->_outWriter->tableWidget(['headers' => ['Task ID', 'Task', 'Module ID', 'Title'],
191
			'rows' => $rows]));
192
		$this->_outWriter->writeLine();
193
		return true;
194
	}
195
}
196