Completed
Push — master ( 7665c8...9af7b9 )
by Robbert
61:21 queued 52:46
created

ar_template::getStorageLayer()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 22.9396

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 6
eloc 15
nc 3
nop 1
dl 0
loc 23
rs 8.5906
c 3
b 0
f 2
ccs 4
cts 18
cp 0.2222
crap 22.9396
1
<?php
2
3
	class ar_template extends arBase {
4
		private $cache = [];
5
6 36
		private function getStorageLayer($path) {
7 36
			global $AR;
8 36
			if (isset($AR->templateStore)) {
9
				$layers = array_keys($AR->templateStore);
10
				// fallback
11
				$layer = array_reduce($layers, function($carry, $item) use ($path) {
12
					if(strpos($path, $item) === 0) {
13
						// item is a prefix of item
14
						if(!isset($carry) || (strlen($carry) < strlen($item) )) {
15
							// item is more specific
16
							$carry = $item;
17
						}
18
					}
19
					return $carry;
20
				}, null);
21
				if(isset($layer)) {
22
					$config =  $AR->templateStore[$layer];
23
					$classname = 'ar_template_' . $config['driver'];
24
					return new $classname($layer, $config);
25
				}
26
			}
27 36
			return new ar_template_filestore('/',[]);
0 ignored issues
show
Unused Code introduced by
The call to ar_template_filestore::__construct() has too many arguments starting with '/'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
		}
29
30 12
		public function get($path, $name){
31 12
			return self::getStorageLayer($path)->get($path, $name);
32
		}
33
34
		public function save($path, $name, $template, $local=null, $private=null) {
35
			return self::getStorageLayer($path)->save($path, $name, $template, $local, $private);
36
		}
37
38
		public function load($path, $name) {
39
			return self::getStorageLayer($path)->load($path, $name);
40
		}
41
42 60
		public function ls($path) {
43 60
			if (!isset($this->cache[$path])){
44 28
				$this->cache[$path] = self::getStorageLayer($path)->ls($path);
45 21
			}
46 60
			return $this->cache[$path];
47
		}
48
49
		public function rm($path, $name){
50
			return self::getStorageLayer($path)->rm($path, $name);
51
		}
52
53 12
		public function exists($path, $name) {
54 12
			return self::getStorageLayer($path)->exists($path, $name);
55
		}
56
57
		public function compile($path, $name) {
58
			return self::getStorageLayer($path)->compile($path, $name);
59
		}
60
	}
61