Passed
Push — master ( d0407d...3b1a32 )
by Jean-Christophe
14:53
created

RestCacheTrait::getRestResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Ubiquity\cache\traits;
4
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\cache\parser\RestControllerParser;
7
use Ubiquity\utils\base\UArray;
8
use Ubiquity\exceptions\RestException;
9
10
/**
11
 *
12
 * Ubiquity\cache\traits$RestCacheTrait
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.0
17
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
18
 */
19
trait RestCacheTrait {
20
21 6
	private static function initRestCache(&$config, $silent = false) {
22 6
		$restCache = [ ];
23 6
		$files = self::getControllersFiles ( $config );
24 6
		foreach ( $files as $file ) {
25 6
			if (is_file ( $file )) {
26 6
				$controller = ClassUtils::getClassFullNameFromFile ( $file );
27 6
				$parser = new RestControllerParser ();
28 6
				$parser->parse ( $controller, $config );
29 6
				if ($parser->isRest ())
30 6
					$restCache = \array_merge ( $restCache, $parser->asArray () );
31
			}
32
		}
33 6
		self::$cache->store ( 'controllers/rest', $restCache, 'controllers' );
0 ignored issues
show
Bug introduced by
$restCache of type array is incompatible with the type string expected by parameter $code of Ubiquity\cache\system\AbstractDataCache::store(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
		self::$cache->store ( 'controllers/rest', /** @scrutinizer ignore-type */ $restCache, 'controllers' );
Loading history...
34 6
		if (! $silent) {
35 6
			echo "Rest cache reset\n";
36
		}
37 6
	}
38
39 1
	public static function getRestRoutes() {
40 1
		$result = [ ];
41 1
		$restCache = self::getRestCache ();
42 1
		foreach ( $restCache as $controllerClass => $restAttributes ) {
43 1
			if (isset ( $restCache [$controllerClass] )) {
44 1
				$result [$controllerClass] = [ 'restAttributes' => $restAttributes,'routes' => self::getControllerRoutes ( $controllerClass, true ) ];
45
			}
46
		}
47 1
		return $result;
48
	}
49
50 22
	public static function getRestCache() {
51 22
		if (self::$cache->exists ( 'controllers/rest' ))
52 22
			return self::$cache->fetch ( 'controllers/rest' );
53
		throw new RestException ( 'Rest cache entry `' . self::$cache->getEntryKey ( 'controllers/rest' ) . '` is missing.\nTry to Re-init Rest cache.' );
54
	}
55
56 7
	public static function getRestResource($controllerClass) {
57 7
		$cacheControllerClass = self::getRestCacheController ( $controllerClass );
58 7
		if (isset ( $cacheControllerClass ))
59 7
			return $cacheControllerClass ['resource'];
60
		return null;
61
	}
62
63 13
	public static function getRestCacheController($controllerClass) {
64 13
		$cache = self::getRestCache ();
65 13
		if (isset ( $cache [$controllerClass] )) {
66 12
			return $cache [$controllerClass];
67
		}
68 1
		return null;
69
	}
70
}
71