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

TDbCronCleanLogTask::execute()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 10
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 16
rs 8.4444
1
<?php
2
3
/**
4
 * TDbCronCleanLogTask 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\Shell\TShellWriter;
14
use Prado\TPropertyValue;
15
use Prado\Exceptions\TInvalidDataValueException;
16
use Prado\Util\Cron\TCronModule;
17
18
/**
19
 * TDbCronCleanLogTask class.
20
 *
21
 * TDbCronCleanLogTask Cleans the cron log of old entries older than the
22
 * age of {@link getTimePeriod} seconds.
23
 *
24
 * @author Brad Anderson <[email protected]>
25
 * @package Prado\Util\Cron
26
 * @since 4.2.0
27
 */
28
class TDbCronCleanLogTask extends TCronTask
29
{
30
	/**
31
	 * @var int the time period in seconds of valid log items, default 28 days.
32
	 */
33
	private $_timeperiod = 2419200; //86400 seconds/day * 28 days
34
	
35
	/**
36
	 * This clears the log of the TDBCronModule specified by the ModuleId,
37
	 * or if none specified, then the cron executing this task.
38
	 * @param Prado\Util\Cron\TDbCronModule $cron
0 ignored issues
show
Bug introduced by
The type Prado\Util\Cron\Prado\Util\Cron\TDbCronModule was not found. Did you mean Prado\Util\Cron\TDbCronModule? If so, make sure to prefix the type with \.
Loading history...
39
	 */
40
	public function execute($cron)
41
	{
42
		if ($mid = $this->getModuleId()) {
43
			$cron = $this->getApplication()->getModule($mid);
44
			if ($cron === null) {
45
				throw new TInvalidDataValueException('dbcronclean_moduleid_is_null', $mid);
46
			}
47
		}
48
		if (is_object($cron) && $cron->isa('Prado\\Util\\Cron\\TDbCronModule')) {
0 ignored issues
show
Bug introduced by
The method isa() does not exist on Prado\IModule. It seems like you code against a sub-type of said class. However, the method does not exist in Prado\Util\IDbModule or Prado\Util\IPluginModule. Are you sure you never get one of those? ( Ignorable by Annotation )

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

48
		if (is_object($cron) && $cron->/** @scrutinizer ignore-call */ isa('Prado\\Util\\Cron\\TDbCronModule')) {
Loading history...
49
			$count = $cron->clearCronLog($this->getTimePeriod());
0 ignored issues
show
Bug introduced by
The method clearCronLog() does not exist on Prado\IModule. It seems like you code against a sub-type of said class. However, the method does not exist in Prado\Util\IDbModule or Prado\Util\IPluginModule. Are you sure you never get one of those? ( Ignorable by Annotation )

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

49
			/** @scrutinizer ignore-call */ 
50
   $count = $cron->clearCronLog($this->getTimePeriod());
Loading history...
50
			
51
			if ($cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
0 ignored issues
show
Bug introduced by
The method asa() does not exist on Prado\IModule. It seems like you code against a sub-type of said class. However, the method does not exist in Prado\Util\IDbModule or Prado\Util\IPluginModule. Are you sure you never get one of those? ( Ignorable by Annotation )

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

51
			if ($cron->/** @scrutinizer ignore-call */ asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
Loading history...
52
				$cron->getOutputWriter()->writeLine("Cleared {$count} Cron Task Logs", TShellWriter::GREEN);
0 ignored issues
show
Bug introduced by
The method getOutputWriter() does not exist on Prado\IModule. It seems like you code against a sub-type of said class. However, the method does not exist in Prado\Util\IDbModule or Prado\Util\IPluginModule. Are you sure you never get one of those? ( Ignorable by Annotation )

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

52
				$cron->/** @scrutinizer ignore-call */ 
53
           getOutputWriter()->writeLine("Cleared {$count} Cron Task Logs", TShellWriter::GREEN);
Loading history...
53
			}
54
		} elseif (is_object($cron) && $cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) {
55
			$cron->getOutputWriter()->writeLine("No DB Cron Module to clean", TShellWriter::RED);
56
		}
57
	}
58
	
59
	/**
60
	 * @return int number of seconds, before which cron logs are to be deleted
61
	 */
62
	public function getTimePeriod()
63
	{
64
		return $this->_timeperiod;
65
	}
66
	
67
	/**
68
	 *
69
	 * @param int $timeperiod number of seconds, before which cron logs are to be deleted
70
	 */
71
	public function setTimePeriod($timeperiod)
72
	{
73
		$this->_timeperiod = TPropertyValue::ensureInteger($timeperiod);
74
	}
75
}
76