RepeatableTask   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 78
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A run() 0 13 4
A cancel() 0 6 1
A getRepeatInterval() 0 4 1
A setRepeatInterval() 0 4 1
A getExpiryTime() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2017 NanoSector
5
 *
6
 * You should have received a copy of the MIT license with the project.
7
 * See the LICENSE file for more information.
8
 */
9
10
namespace Yoshi2889\Tasks;
11
12
13
class RepeatableTask implements TaskInterface
14
{
15
	/**
16
	 * @var int
17
	 */
18
	protected $repeatInterval = 0;
0 ignored issues
show
Coding Style introduced by
Protected member variable "repeatInterval" must contain a leading underscore
Loading history...
19
20
	/**
21
	 * @var int
22
	 */
23
	protected $expiryTime = 0;
0 ignored issues
show
Coding Style introduced by
Protected member variable "expiryTime" must contain a leading underscore
Loading history...
24
25
	/**
26
	 * @var TaskInterface
27
	 */
28
	protected $childTask;
0 ignored issues
show
Coding Style introduced by
Protected member variable "childTask" must contain a leading underscore
Loading history...
29
30
	/**
31
	 * RepeatableTask constructor.
32
	 *
33
	 * @param TaskInterface $childTask
34
	 * @param int $repeatInterval
35
	 */
36 6
	public function __construct(TaskInterface $childTask, int $repeatInterval)
37
	{
38 6
		$this->expiryTime = time() + $repeatInterval;
39 6
		$this->repeatInterval = $repeatInterval;
40 6
		$this->childTask = $childTask;
41 6
	}
42
43
	/**
44
	 * @return null|TaskInterface
45
	 */
46 4
	public function run(): ?TaskInterface
47
	{
48 4
		if (time() >= $this->childTask->getExpiryTime())
49
		{
50 4
			$result = $this->childTask->run();
51
52 4
			if ($result instanceof TaskInterface)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
53 1
				$this->childTask = $result;
54
		}
55
56 4
		$this->expiryTime = $this->getExpiryTime() + $this->getRepeatInterval();
57 4
		return $this->expiryTime > 0 ? $this : null;
58
	}
59
60 2
	public function cancel(): void
61
	{
62 2
		$this->childTask->cancel();
63 2
		$this->repeatInterval = 0;
64 2
		$this->expiryTime = 0;
65 2
	}
66
67
	/**
68
	 * @return int
69
	 */
70 6
	public function getRepeatInterval(): int
71
	{
72 6
		return $this->repeatInterval;
73
	}
74
75
	/**
76
	 * @param int $repeatInterval
77
	 */
78 1
	public function setRepeatInterval(int $repeatInterval)
79
	{
80 1
		$this->repeatInterval = $repeatInterval;
81 1
	}
82
83
	/**
84
	 * @return int
85
	 */
86 5
	public function getExpiryTime(): int
87
	{
88 5
		return $this->expiryTime;
89
	}
90
}