Passed
Push — master ( 83996b...17a193 )
by Jean-Christophe
09:22
created

ModelsCacheTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 39
dl 0
loc 94
ccs 46
cts 48
cp 0.9583
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initModelsCache() 0 15 6
A getOrmModelCache() 0 2 1
A modelCacheExists() 0 5 2
A getModelCacheKey() 0 2 1
A createOrmModelCache() 0 7 2
A modelsCacheUpdated() 0 14 4
A getModelsFiles() 0 2 1
A getModels() 0 7 2
1
<?php
2
3
/**
4
 * Cache traits
5
 */
6
namespace Ubiquity\cache\traits;
7
8
use Ubiquity\orm\parser\ModelParser;
9
use Ubiquity\cache\ClassUtils;
10
use Ubiquity\contents\validation\ValidatorsManager;
11
12
/**
13
 *
14
 * Ubiquity\cache\traits$ModelsCacheTrait
15
 * This class is part of Ubiquity
16
 *
17
 * @author jcheron <[email protected]>
18
 * @version 1.0.0
19
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
20
 */
21
trait ModelsCacheTrait {
22
23
	abstract protected static function _getFiles(&$config, $type, $silent = false);
24
25 6
	public static function createOrmModelCache($classname) {
26 6
		$key = self::getModelCacheKey ( $classname );
27 6
		if (isset ( self::$cache )) {
28 6
			$p = new ModelParser ();
29 6
			$p->parse ( $classname );
30 6
			self::$cache->store ( $key, $p->__toString (), 'models' );
31 6
			return self::$cache->fetch ( $key );
32
		}
33
	}
34
35 36
	public static function getOrmModelCache($classname) {
36 36
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
37
	}
38
39 41
	public static function getModelCacheKey($classname) {
40 41
		return \str_replace ( "\\", \DS, $classname );
41
	}
42
43 2
	public static function modelCacheExists($classname) {
44 2
		$key = self::getModelCacheKey ( $classname );
45 2
		if (isset ( self::$cache ))
46 2
			return self::$cache->exists ( $key );
47
		return false;
48
	}
49
50 5
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
51 5
		$files = self::getModelsFiles ( $config, $silent );
52 5
		foreach ( $files as $file ) {
53 5
			if (is_file ( $file )) {
54 5
				$model = ClassUtils::getClassFullNameFromFile ( $file );
55 5
				if (! $forChecking) {
56 5
					self::createOrmModelCache ( $model );
57
				}
58
			}
59
		}
60 5
		if (! $forChecking) {
61 5
			ValidatorsManager::initModelsValidators ( $config );
62
		}
63 5
		if (! $silent) {
64 5
			echo "Models cache reset\n";
65
		}
66 5
	}
67
68
	/**
69
	 * Checks if the models cache is up to date
70
	 *
71
	 * @param array $config
72
	 * @return boolean|array
73
	 */
74 12
	public static function modelsCacheUpdated(&$config) {
75 12
		$result = false;
76 12
		$files = self::getModelsFiles ( $config, true );
77 12
		foreach ( $files as $file ) {
78 12
			if (is_file ( $file )) {
79 12
				$model = ClassUtils::getClassFullNameFromFile ( $file );
80 12
				$p = new ModelParser ();
81 12
				$p->parse ( $model );
82 12
				if (self::getOrmModelCache ( $model ) != $p->asArray ()) {
83 1
					$result [$model] = true;
84
				}
85
			}
86
		}
87 12
		return $result;
88
	}
89
90
	/**
91
	 * Returns an array of files corresponding to models
92
	 *
93
	 * @param array $config
94
	 * @param boolean $silent
95
	 * @return array
96
	 */
97 20
	public static function getModelsFiles(&$config, $silent = false) {
98 20
		return self::_getFiles ( $config, "models", $silent );
99
	}
100
101
	/**
102
	 * Returns an array of the models class names
103
	 *
104
	 * @param array $config
105
	 * @param boolean $silent
106
	 * @return string[]
107
	 */
108 11
	public static function getModels(&$config, $silent = false) {
109 11
		$result = [ ];
110 11
		$files = self::getModelsFiles ( $config, $silent );
111 11
		foreach ( $files as $file ) {
112 11
			$result [] = ClassUtils::getClassFullNameFromFile ( $file );
113
		}
114 11
		return $result;
115
	}
116
}
117