Test Failed
Push — master ( befc15...123c13 )
by Jean-Christophe
18:33
created

ModelsCacheTrait::createOrmModelCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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
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
	private static $modelsDatabaseKey = 'models' . \DIRECTORY_SEPARATOR . '_modelsDatabases';
28
29 35
	public static function createOrmModelCache($classname) {
30 35
		$key = self::getModelCacheKey ( $classname );
31 35
		if (isset ( self::$cache )) {
32 35
			$p = new ModelParser ();
33 35
			$p->parse ( $classname );
34 35
			self::$cache->store ( $key, $p->asArray (), 'models' );
35 35
			return self::$cache->fetch ( $key );
36
		}
37
	}
38
39 40
	public static function getOrmModelCache($classname) {
40 40
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
41
	}
42
43 75
	public static function getModelCacheKey($classname) {
44 75
		return \str_replace ( "\\", \DS, $classname );
45
	}
46
47 12
	public static function modelCacheExists($classname) {
48 12
		$key = self::getModelCacheKey ( $classname );
49 12
		if (isset ( self::$cache ))
50 12
			return self::$cache->exists ( $key );
51
		return false;
52
	}
53
54 35
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
55 35
		$modelsDb = [ ];
56 35
		$files = self::getAllModelsFiles( $config, $silent );
57 35
		foreach ( $files as $file ) {
58 35
			if (\is_file ( $file )) {
59 35
				$model = ClassUtils::getClassFullNameFromFile ( $file );
60 35
				if(!\class_exists($model)){
61
					if(\file_exists($file)){
62
						include $file;
63
					}
64
				}
65 35
				if (! $forChecking) {
66 35
					self::createOrmModelCache ( $model );
67 35
					$db = 'default';
68 35
					$ret = Reflexion::getAnnotationClass ( $model, 'database' );
69 35
					if (\count ( $ret ) > 0) {
70 4
						$db = $ret [0]->name;
71 4
						if (! isset ( $config ['database'] [$db] )) {
72
							throw new UbiquityException ( $db . ' connection is not defined in config array' );
73
						}
74
					}
75 35
					$modelsDb [$model] = $db;
76 35
					ValidatorsManager::initClassValidators ( $model );
77
				}
78
			}
79
		}
80 35
		if (! $forChecking) {
81 35
			self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
82
		}
83 35
		if (! $silent) {
84 6
			echo "Models cache reset\n";
85
		}
86 35
	}
87
88
	/**
89
	 * Checks if the models cache is up to date
90
	 *
91
	 * @param array $config
92
	 * @return boolean|array
93
	 */
94 12
	public static function modelsCacheUpdated(&$config) {
95 12
		$result = false;
96 12
		$files = self::getModelsFiles ( $config, true );
97 12
		foreach ( $files as $file ) {
98 12
			if (\is_file ( $file )) {
99 12
				$model = ClassUtils::getClassFullNameFromFile ( $file );
100 12
				$p = new ModelParser ();
101 12
				$p->parse ( $model );
102 12
				if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
103
					$result [$model] = true;
104
				}
105
			}
106
		}
107 12
		return $result;
108
	}
109
110
	/**
111
	 * Returns an array of files corresponding to models
112
	 *
113
	 * @param array $config
114
	 * @param boolean $silent
115
	 * @return array
116
	 */
117 49
	public static function getModelsFiles(&$config, $silent = false) {
118 49
		return self::_getFiles ( $config, 'models', $silent );
119
	}
120
121
	/**
122
	 * Returns an array of all model files
123
	 *
124
	 * @param array $config
125
	 * @param boolean $silent
126
	 * @return array
127
	 */
128 5
	public static function getAllModelsFiles(&$config, $silent = false) {
129 5
		return self::_getAllFiles ( $config, 'models', $silent );
0 ignored issues
show
Bug introduced by
The method _getAllFiles() does not exist on Ubiquity\cache\traits\ModelsCacheTrait. Did you maybe mean _getFiles()? ( Ignorable by Annotation )

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

129
		return self::/** @scrutinizer ignore-call */ _getAllFiles ( $config, 'models', $silent );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130 5
	}
131 5
132 5
	/**
133 5
	 * Returns an array of the models class names
134 5
	 *
135 5
	 * @param array $config
136 5
	 * @param boolean $silent
137 3
	 * @param ?string $databaseOffset
138
	 * @return string[]
139 5
	 */
140 5
	public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
141
		$result = [];
142
		$files = self::getModelsFiles($config, $silent);
143
		foreach ($files as $file) {
144 5
			$className = ClassUtils::getClassFullNameFromFile($file);
145
			if (\class_exists($className, true)) {
146
				$db = 'default';
147 72
				$ret = Reflexion::getAnnotationClass($className, 'database');
148 72
				if (\count($ret) > 0) {
149 72
					$db = $ret[0]->name;
150
				}
151
				if ($databaseOffset==null || $db === $databaseOffset) {
1 ignored issue
show
Bug introduced by
It seems like you are loosely comparing $databaseOffset of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
152
					$result[] = $className;
153
				}
154
			}
155
		}
156
		return $result;
157
	}
158
159
	public static function getModelsNamespace(array &$config,string $databaseOffset='default'): ?string {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

159
	public static function getModelsNamespace(/** @scrutinizer ignore-unused */ array &$config,string $databaseOffset='default'): ?string {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
		$modelsDatabases=self::getModelsDatabases();
161
		foreach ($modelsDatabases as $model=>$offset){
162
			if($offset===$databaseOffset){
163
				$rc=new \ReflectionClass($model);
164
				return $rc->getNamespaceName();
165
			}
166
		}
167
		return null;
168
	}
169 39
170
	public static function getModelsDatabases(): array {
171
		if (self::$cache->exists ( self::$modelsDatabaseKey )) {
172
			return self::$cache->fetch ( self::$modelsDatabaseKey );
173
		}
174
		return [ ];
175
	}
176
177
	public static function getDatabases(): array {
178
		return \array_keys(\array_flip(self::getModelsDatabases()));
179
	}
180
181
	/**
182
	 * Preloads models metadatas.
183
	 * To use only with async servers (Swoole, Workerman)
184
	 *
185
	 * @param array $config
186
	 * @param string $offset
187
	 * @param ?array $models
188
	 */
189
	public static function warmUpModels(&$config, $offset = 'default', $models = null) {
190
		$models ??= self::getModels ( $config, true, $offset );
191
		foreach ( $models as $model ) {
192
			OrmUtils::getModelMetadata ( $model );
193
			Reflexion::getPropertiesAndValues ( new $model () );
194
		}
195
	}
196
}