Passed
Push — master ( 0ed261...e52b37 )
by Jean-Christophe
17:09
created

ModelsCacheTrait::initModelsCache()   B

Complexity

Conditions 11
Paths 51

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.2363

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 33
ccs 21
cts 24
cp 0.875
rs 7.3166
cc 11
nc 51
nop 3
crap 11.2363

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Ubiquity\orm\parser\Reflexion;
12
use Ubiquity\exceptions\UbiquityException;
13
use Ubiquity\orm\OrmUtils;
14
15
/**
16
 *
17
 * Ubiquity\cache\traits$ModelsCacheTrait
18
 * This class is part of Ubiquity
19
 *
20
 * @author jcheron <[email protected]>
21
 * @version 1.0.3
22
 * @staticvar \Ubiquity\cache\system\AbstractDataCache $cache
23
 */
24
trait ModelsCacheTrait {
25
26
	abstract protected static function _getFiles(&$config, $type, $silent = false);
27
	abstract protected static function _getAllFiles(&$config, $type, $silent = false): array ;
28
29
	private static $modelsDatabaseKey = 'models' . \DIRECTORY_SEPARATOR . '_modelsDatabases';
30
31 35
	public static function createOrmModelCache($classname) {
32 35
		$key = self::getModelCacheKey ( $classname );
33 35
		if (isset ( self::$cache )) {
34 35
			$p = new ModelParser ();
35 35
			$p->parse ( $classname );
36 35
			self::$cache->store ( $key, $p->asArray (), 'models' );
37 35
			return self::$cache->fetch ( $key );
38
		}
39
	}
40
41 40
	public static function getOrmModelCache($classname) {
42 40
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
43
	}
44
45 75
	public static function getModelCacheKey($classname) {
46 75
		return \str_replace ( "\\", \DS, $classname );
47
	}
48
49 12
	public static function modelCacheExists($classname) {
50 12
		$key = self::getModelCacheKey ( $classname );
51 12
		if (isset ( self::$cache ))
52 12
			return self::$cache->exists ( $key );
53
		return false;
54
	}
55
56 35
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
57 35
		$modelsDb = [ ];
58 35
		$files = self::getAllModelsFiles( $config, $silent );
59 35
		foreach ( $files as $file ) {
60 35
			if (\is_file ( $file )) {
61 35
				$model = ClassUtils::getClassFullNameFromFile ( $file );
62 35
				if($model!=null) {
63 35
					if (!\class_exists($model)) {
64
						if (\file_exists($file)) {
65
							include $file;
66
						}
67
					}
68 35
					if (!$forChecking) {
69 35
						self::createOrmModelCache($model);
70 35
						$db = 'default';
71 35
						$ret = Reflexion::getAnnotationClass($model, 'database');
72 35
						if (\count($ret) > 0) {
73 4
							$db = $ret [0]->name;
74 4
							if (!isset ($config ['database'] [$db])) {
75
								throw new UbiquityException ($db . ' connection is not defined in config array');
76
							}
77
						}
78 35
						$modelsDb [$model] = $db;
79 35
						ValidatorsManager::initClassValidators($model);
80
					}
81
				}
82
			}
83
		}
84 35
		if (! $forChecking) {
85 35
			self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
86
		}
87 35
		if (! $silent) {
88 6
			echo "Models cache reset\n";
89
		}
90 35
	}
91
92
	/**
93
	 * Checks if the models cache is up to date
94
	 *
95
	 * @param array $config
96
	 * @return boolean|array
97
	 */
98 12
	public static function modelsCacheUpdated(&$config) {
99 12
		$result = [];
100 12
		$files = self::getModelsFiles ( $config, true );
101 12
		foreach ( $files as $file ) {
102 12
			if (\is_file ( $file )) {
103 12
				$model = ClassUtils::getClassFullNameFromFile ( $file );
104 12
				$p = new ModelParser ();
105 12
				$p->parse ( $model );
106 12
				if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
107
					$result [$model] = true;
108
				}
109
			}
110
		}
111 12
		return $result;
112
	}
113
114
	/**
115
	 * Returns an array of files corresponding to models
116
	 *
117
	 * @param array $config
118
	 * @param boolean $silent
119
	 * @return array
120
	 */
121 14
	public static function getModelsFiles(&$config, $silent = false) {
122 14
		return self::_getFiles ( $config, 'models', $silent );
123
	}
124
125
	/**
126
	 * Returns an array of all model files
127
	 *
128
	 * @param array $config
129
	 * @param boolean $silent
130
	 * @return array
131
	 */
132 35
	public static function getAllModelsFiles(&$config, $silent = false) {
133 35
		return self::_getAllFiles ( $config, 'models', $silent );
134
	}
135
136
	/**
137
	 * Returns an array of the models class names
138
	 *
139
	 * @param array $config
140
	 * @param boolean $silent
141
	 * @param ?string $databaseOffset
142
	 * @return string[]
143
	 */
144 5
	public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
145 5
		$result = [];
146 5
		$files = self::getModelsFiles($config, $silent);
147 5
		foreach ($files as $file) {
148 5
			$className = ClassUtils::getClassFullNameFromFile($file);
149 5
			if($className!=null) {
150 5
				if (\class_exists($className, true)) {
151 5
					$db = 'default';
152 5
					$ret = Reflexion::getAnnotationClass($className, 'database');
153 5
					if (\count($ret) > 0) {
154 3
						$db = $ret[0]->name;
155
					}
156 5
					if ($databaseOffset == null || $db === $databaseOffset) {
157 5
						$result[] = $className;
158
					}
159
				}
160
			}
161
		}
162 5
		return $result;
163
	}
164
165
	public static function getModelsNamespace(string $databaseOffset='default'): ?string {
166
		$modelsDatabases=self::getModelsDatabases();
167
		foreach ($modelsDatabases as $model=>$offset){
168
			if($offset===$databaseOffset){
169
				$rc=new \ReflectionClass($model);
170
				return $rc->getNamespaceName();
171
			}
172
		}
173
		return null;
174
	}
175
176 72
	public static function getModelsDatabases(): array {
177 72
		if (self::$cache->exists ( self::$modelsDatabaseKey )) {
178 72
			return self::$cache->fetch ( self::$modelsDatabaseKey );
179
		}
180
		return [ ];
181
	}
182
183
	public static function storeModelsDatabases(array $modelsDatabases): void {
184
		self::$cache->store ( self::$modelsDatabaseKey, $modelsDatabases, 'models' );
185
	}
186
187
	public static function getDatabases(): array {
188
		return \array_keys(\array_flip(self::getModelsDatabases()));
189
	}
190
191
	/**
192
	 * Preloads models metadatas.
193
	 * To use only with async servers (Swoole, Workerman)
194
	 *
195
	 * @param array $config
196
	 * @param string $offset
197
	 * @param ?array $models
198
	 */
199
	public static function warmUpModels(&$config, $offset = 'default', $models = null) {
200
		$models ??= self::getModels ( $config, true, $offset );
201
		foreach ( $models as $model ) {
202
			OrmUtils::getModelMetadata ( $model );
203
			Reflexion::getPropertiesAndValues ( new $model () );
204
		}
205
	}
206
}
207