Completed
Push — master ( d4e951...14a860 )
by Jean-Christophe
04:01
created

RestCacheTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 14
eloc 30
dl 0
loc 50
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRestCacheController() 0 6 2
A getRestCache() 0 4 2
A getRestResource() 0 5 2
A getRestRoutes() 0 9 3
A initRestCache() 0 15 5
1
<?php
2
namespace Ubiquity\cache\traits;
3
4
use Ubiquity\cache\ClassUtils;
5
use Ubiquity\cache\parser\RestControllerParser;
6
use Ubiquity\utils\base\UArray;
7
use Ubiquity\exceptions\RestException;
8
9
/**
10
 * @author jc
11
 * @staticvar array $cache
12
 *
13
 */
14
trait RestCacheTrait{
15
16 1
	private static function initRestCache(&$config,$silent=false) {
17 1
		$restCache=[];
18 1
		$files=self::getControllersFiles($config);
19 1
		foreach ( $files as $file ) {
20 1
			if (is_file($file)) {
21 1
				$controller=ClassUtils::getClassFullNameFromFile($file);
22 1
				$parser=new RestControllerParser();
23 1
				$parser->parse($controller,$config);
24 1
				if($parser->isRest())
25 1
					$restCache=\array_merge($restCache,$parser->asArray());
26
			}
27
		}
28 1
		self::$cache->store("controllers/rest", "return " . UArray::asPhpArray($restCache, "array") . ";",'controllers');
29 1
		if(!$silent){
30 1
			echo "Rest cache reset\n";
31
		}
32 1
	}
33
34 1
	public static function getRestRoutes() {
35 1
		$result=[];
36 1
		$restCache=self::getRestCache();
37 1
		foreach ($restCache as $controllerClass=>$restAttributes){
38 1
			if(isset($restCache[$controllerClass])){
39 1
				$result[$controllerClass]=["restAttributes"=>$restAttributes,"routes"=>self::getControllerRoutes($controllerClass,true)];
40
			}
41
		}
42 1
		return $result;
43
	}
44
45 5
	public static function getRestCache() {
46 5
		if (self::$cache->exists("controllers/rest"))
47 3
			return self::$cache->fetch("controllers/rest");
48 3
			throw new RestException("Rest cache entry `".self::$cache->getEntryKey("controllers/rest")."` is missing.\nTry to Re-init Rest cache.");
49
	}
50
51 1
	public static function getRestResource($controllerClass){
52 1
		$cacheControllerClass=self::getRestCacheController($controllerClass);
53 1
		if(isset($cacheControllerClass))
54 1
			return $cacheControllerClass["resource"];
55
		return null;
56
	}
57
58 3
	public static function getRestCacheController($controllerClass){
59 3
		$cache=self::getRestCache();
60 2
		if(isset($cache[$controllerClass])){
61 2
			return $cache[$controllerClass];
62
		}
63
		return null;
64
	}
65
}
66