SheetLoader   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 91
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A updateRequired() 0 3 1
A addImport() 0 4 1
A getCacheKey() 0 3 1
A processRules() 0 13 3
A getRules() 0 3 1
A setCacheKey() 0 12 4
A __construct() 0 6 2
A executeTssRule() 0 9 1
A sortRules() 0 9 4
A sortPseudo() 0 3 2
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\SheetLoader;
8
//Separates out TSS file loading/caching from parsing
9
class SheetLoader {
10
    private $tss;
11
    private $filePath;
12
    private $time;
13
    private $import = [];
14
15
    public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) {
16
    	$this->cache = $cache;
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        $this->filePath = $filePath;
18
        $this->tss = $tss;
19
        $this->time = isset($time) ? $time : time();
20
    }
21
22
	//Allows controlling whether any updates are required to the template
23
	//e.g. return false
24
	//	 1. If all update-frequencies  haven't expired
25
	//   2. If the data hasn't changed since the last run
26
	//If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code
27
	public function updateRequired($data) {
28
		return $this->tss->updateRequired($data);
29
	}
30
31
	public function addImport($import) {
32
		$this->filePath->addPath(dirname(realpath($this->filePath->getFilePath($import))));
33
		$this->import[] = $import;
34
	}
35
36
	public function setCacheKey($tokens) {
37
		$newTokens = [];
38
		foreach ($tokens as $token) {
39
			if ($token['type'] == \Transphporm\Parser\Tokenizer::NAME && $token['value'] == 'data') {
40
				$tokens->next();
41
				$newTokens = array_merge($newTokens, iterator_to_array($tokens->current()['value']));
42
			}
43
			else $newTokens[] = $token;
44
		}
45
46
		$this->tss->setCacheKey(new \Transphporm\Parser\Tokens($newTokens));
47
	}
48
49
	public function getCacheKey($data) {
50
		return $this->tss->getCacheKey($data);
51
	}
52
53
54
	public function processRules($template, \Transphporm\Config $config) {
55
		$rules = $this->getRules($config->getCssToXpath(), $config->getValueParser());
56
57
58
		usort($rules, [$this, 'sortRules']);
59
60
		foreach ($rules as $rule) {
61
			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
62
		}
63
64
		//if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
65
		$this->tss->write($rules, $this->import);
66
	}
67
68
	//Load the TSS
69
	public function getRules($cssToXpath, $valueParser, $indexStart = 0) {
70
		return $this->tss->getRules($cssToXpath, $valueParser, $this, $indexStart);
71
	}
72
73
	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
74
	private function executeTssRule($rule, $template, $config) {
75
		$rule->touch();
76
77
		$lineref = $config->getLine();
78
		$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
79
		$hook = new \Transphporm\Hook\PropertyHook($rule->properties, $lineref, $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath());
80
		$config->loadProperties($hook);
81
		$template->addHook($rule->query, $hook);
82
	}
83
84
85
	private function sortRules($a, $b) {
86
		//If they have the same depth, compare on index
87
		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
88
89
		if ($a->depth === $b->depth) $property = 'index';
90
		else $property = 'depth';
91
92
		return ($a->$property < $b->$property) ? -1 : 1;
93
	}
94
95
96
	private function sortPseudo($a, $b) {
97
		return count($a->pseudo) > count($b->pseudo)  ? 1 : -1;
98
	}
99
}
100