Completed
Push — master ( b86211...ed7d82 )
by Tom
02:45
created

Rule::touch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Transphporm;
3
class Rule {
4
	private $query;
5
	private $pseudo;
6
	private $depth;
7
	private $index;
8
	private $properties = [];
9
	private $lastRun = 0;
10
11
	const S = 1;
12
	const M = 60;
13
	const H = self::M*60;
14
	const D = self::H*24;
15
16
17
	public function __construct($query, $pseudo, $depth, $index, array $properties = []) {
18
		$this->query = $query;
19
		$this->pseudo = $pseudo;
20
		$this->depth = $depth;
21
		$this->index = $index;
22
		$this->properties = $properties;
23
	}
24
25
	public function __get($name) {
26
		return $this->$name;
27
	}
28
29
	public function __set($name, $value) {
30
		$this->$name = $value;
31
	}
32
33
	public function touch() {
34
		$this->lastRun = time();
35
	}
36
37
	public function shouldRun($time) {
38
		if (isset($this->properties['update-frequency']) && $this->lastRun !== 0) {
39
			$frequency = $this->properties['update-frequency'];
40
			$static = ['always' => true, 'never' => false];
41
			if (isset($static[$frequency])) return $static[$frequency];
42
43
			$num = (int) $frequency;
44
			$unit = strtoupper(trim(str_replace($num, '', $frequency)));
45
			
46
			$offset = $num * constant(self::class . '::' . $unit);
47
48
			if ($time > $this->lastRun + $offset) return true;
49
			else return false;
50
51
		}
52
		else return true;
53
	}
54
}