Completed
Push — master ( ed7d82...0b58b7 )
by Tom
02:40
created

Rule::timeFrequency()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 3
eloc 7
nc 4
nop 2
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
	private function timeFrequency($frequency, $time = null) {
38
		if ($time === null) $time = time();
39
		$num = (int) $frequency;
40
		$unit = strtoupper(trim(str_replace($num, '', $frequency)));
41
			
42
		$offset = $num * constant(self::class . '::' . $unit);
43
44
		if ($time > $this->lastRun + $offset) return true;
45
		else return false;
46
	}
47
48
	public function shouldRun($time = null) {
49
		if (isset($this->properties['update-frequency']) && $this->lastRun !== 0) {
50
			$frequency = $this->properties['update-frequency'];
51
			$static = ['always' => true, 'never' => false];
52
			if (isset($static[$frequency])) return $static[$frequency];
53
			else return $this->timeFrequency($frequency, $time);
54
55
		}
56
		else return true;
57
	}
58
}