Completed
Push — master ( 23e35e...f8660b )
by Tom
01:53
created

Rule::getUpdateFrequency()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 8
nc 8
nop 0
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm;
8
class Rule {
9
	private $query;
10
	private $pseudo;
11
	private $depth;
12
	private $index;
13
    private $file;
14
	private $line;
15
	private $properties = [];
16
	private $lastRun = 0;
17
18
	const S = 1;
19
	const M = 60;
20
	const H = 3600;
21
	const D = 86400;
22
23
24
	public function __construct($query, $pseudo, $depth, $index, $file, $line, array $properties = []) {
25
		$this->query = $query;
26
		$this->pseudo = $pseudo;
27
		$this->depth = $depth;
28
		$this->index = $index;
29
        $this->file = $file;
30
		$this->line = $line;
31
		$this->properties = $properties;
32
	}
33
34
	public function __get($name) {
35
		return $this->$name;
36
	}
37
38
	public function __set($name, $value) {
39
		$this->$name = $value;
40
	}
41
42
	public function touch() {
43
		$this->lastRun = time();
44
	}
45
46
	private function timeFrequency($frequency, $time = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $frequency is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
		if ($time === null) $time = time();
48
49
		$offset = $this->getUpdateFrequency();
50
51
		if ($time > $this->lastRun + $offset) return true;
52
		else return false;
53
	}
54
55
	public function shouldRun($time = null) {
56
		if (isset($this->properties['update-frequency']) && $this->lastRun !== 0) {
57
			$frequency = $this->properties['update-frequency']->read();
58
			$static = ['always' => true, 'never' => false];
59
			if (isset($static[$frequency])) return $static[$frequency];
60
			else return $this->timeFrequency($frequency, $time);
61
		}
62
		else return true;
63
	}
64
65
	public function getUpdateFrequency() {
66
		$frequency = isset($this->properties['update-frequency']) ? $this->properties['update-frequency']->read() : false;
67
68
		if (empty($frequency)) return 0;
69
70
		$num = (int) $frequency;
71
		$unit = strtoupper(trim(str_replace($num, '', $frequency)));
72
		if ($frequency == 'always') return 0;
73
		else if ($frequency == 'never') return self::D*3650; //Not quite never, in 10 years will cause issues on 32 bit PHP builds re 2038 problem
74
75
		return $num * constant(self::class . '::' . $unit);
76
	}
77
}
78