Passed
Pull Request — master (#817)
by Fabio
07:46
created

TShellCronLogBehavior::dyUpdateTaskInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * TShellCronLogBehavior class file.
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Util\Cron;
11
12
use Prado\Prado;
13
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...
14
use Prado\Util\TBehavior;
15
use Prado\Util\TCallChain;
16
17
/**
18
 * TShellCronLogBehavior class.
19
 *
20
 * Enables cron logging to the shell.  It also encapsulates the TShellWriter
21
 * and basic dyWrite, dyWriteLine, and dyFlush functionality.
22
 *
23
 * @author Brad Anderson <[email protected]>
24
 * @since 4.2.0
25
 */
26
class TShellCronLogBehavior extends TBehavior
27
{
28
	private $_outWriter;
29
30
	/**
31
	 * creates the TShellCronLogBehavior with a writer
32
	 * @param \Prado\IO\ITextWriter $writer where to write cron task info to
33
	 */
34
	public function __construct($writer = null)
35
	{
36
		$this->_outWriter = $writer;
37
	}
38
39
	/**
40
	 * @return \Prado\IO\ITextWriter the output writer from the shell
41
	 */
42
	public function getOutputWriter()
43
	{
44
		return $this->_outWriter;
45
	}
46
47
	/**
48
	 * @param \Prado\IO\ITextWriter $writer the output writer from the shell
49
	 */
50
	public function setOutputWriter($writer)
51
	{
52
		$this->_outWriter = $writer;
53
	}
54
55
	/**
56
	 * writes, with attributes, to the OutputWriter
57
	 * @param string $str
58
	 * @param array|int|\Prado\Util\TCallChain|string $p1
59
	 * @param null|\Prado\Util\TCallChain $p2
60
	 * @return mixed
61
	 */
62
	public function dyWrite($str, $p1, $p2 = null)
63
	{
64
		if ($p1 instanceof TCallChain) {
65
			$attr = null;
66
			$callchain = $p1;
67
		} else {
68
			$attr = $p1;
69
			$callchain = $p2;
70
		}
71
		$this->_outWriter->write($str, $attr);
0 ignored issues
show
Bug introduced by
The method write() does not exist on null. ( Ignorable by Annotation )

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

71
		$this->_outWriter->/** @scrutinizer ignore-call */ 
72
                     write($str, $attr);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to Prado\IO\ITextWriter::write() has too many arguments starting with $attr. ( Ignorable by Annotation )

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

71
		$this->_outWriter->/** @scrutinizer ignore-call */ 
72
                     write($str, $attr);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
		return $callchain->dyWrite($str, $p1, $p2);
0 ignored issues
show
Bug introduced by
The method dyWrite() does not exist on null. ( Ignorable by Annotation )

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

72
		return $callchain->/** @scrutinizer ignore-call */ dyWrite($str, $p1, $p2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
	}
74
75
	/**
76
	 * writes Line, with attributes, to the OutputWriter
77
	 * @param string $str
78
	 * @param array|int|\Prado\Util\TCallChain|string $p1
79
	 * @param null|\Prado\Util\TCallChain $p2
80
	 * @return mixed
81
	 */
82
	public function dyWriteLine($str, $p1, $p2 = null)
83
	{
84
		if ($p1 instanceof TCallChain) {
85
			$attr = null;
86
			$callchain = $p1;
87
		} else {
88
			$attr = $p1;
89
			$callchain = $p2;
90
		}
91
		$this->_outWriter->writeLine($str, $attr);
0 ignored issues
show
Bug introduced by
The method writeLine() does not exist on Prado\IO\ITextWriter. Did you maybe mean write()? ( Ignorable by Annotation )

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

91
		$this->_outWriter->/** @scrutinizer ignore-call */ 
92
                     writeLine($str, $attr);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
		return $callchain->dyWriteLine($str, $p1, $p2);
93
	}
94
95
96
	/**
97
	 * flushes the OutputWriter
98
	 * @param \Prado\Util\TCallChain $callchain
99
	 * @return string the accumulated text in the buffer
100
	 */
101
	public function dyFlush($callchain)
102
	{
103
		$result = $this->_outWriter->flush();
104
		return $result . $callchain->dyFlush();
105
	}
106
107
	/**
108
	 * Logs a when cron is run in the shell.
109
	 * @param int $numtasks number of tasks to run
110
	 * @param \Prado\Util\TCallChain $callchain the chain of methods
111
	 * @return mixed
112
	 */
113
	public function dyLogCron($numtasks, $callchain)
114
	{
115
		$this->_outWriter->writeLine(" Running {$numtasks} Cron Tasks @ " . date('Y-m-d H:i:s') . " \n");
116
117
		return $callchain->dyLogCron($numtasks);
118
	}
119
120
	/**
121
	 * Logs a single cron task when run in the shell.
122
	 * @param \Prado\Util\Cron\TCronTask $task the task to log
123
	 * @param string $username the user name running the task
124
	 * @param \Prado\Util\TCallChain $callchain the chain of methods
125
	 * @return mixed
126
	 */
127
	public function dyLogCronTask($task, $username, $callchain)
128
	{
129
		$this->_outWriter->writeLine("Running Task {$task->getName()} as {$username}");
130
		$this->_outWriter->flush();
131
132
		return $callchain->dyLogCronTask($task, $username);
133
	}
134
135
	/**
136
	 * Logs the end of a single cron task when run in the shell.
137
	 * @param \Prado\Util\Cron\TCronTask $task the tasks that was run
138
	 * @param \Prado\Util\TCallChain $callchain the chain of methods
139
	 * @return mixed
140
	 */
141
	public function dyLogCronTaskEnd($task, $callchain)
142
	{
143
		$this->_outWriter->writeLine("Ending Task {$task->getName()}\n");
144
145
		return $callchain->dyLogCronTaskEnd($task);
146
	}
147
}
148