Passed
Push — master ( f7ade8...7e9287 )
by Jean-Christophe
11:26
created

DAOCoreTrait   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Test Coverage

Coverage 86.92%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 47
eloc 92
c 6
b 1
f 1
dl 0
loc 211
ccs 93
cts 107
cp 0.8692
rs 8.64

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getModels() 0 8 3
A getFirstKeyValue_() 0 5 2
A parseKey() 0 4 5
A getInstance_() 0 5 2
A _loadObjectFromRow() 0 18 6
A loadManys() 0 9 5
A _getAll() 0 25 6
A _loadSimpleObjectFromRow() 0 10 3
A storeDbCache() 0 4 2
A getClass_() 0 5 2
A applyTransformers() 0 5 2
A getValue_() 0 5 2
A _getOne() 0 22 6
A _getFieldList() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like DAOCoreTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DAOCoreTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\db\SqlUtils;
6
use Ubiquity\events\DAOEvents;
7
use Ubiquity\events\EventsManager;
8
use Ubiquity\orm\OrmUtils;
9
use Ubiquity\orm\parser\ConditionParser;
10
use Ubiquity\orm\parser\Reflexion;
11
use Ubiquity\db\Database;
12
13
/**
14
 * Core Trait for DAO class.
15
 * Ubiquity\orm\traits$DAOCoreTrait
16
 * This class is part of Ubiquity
17
 *
18
 * @author jcheron <[email protected]>
19
 * @version 1.1.6
20
 *
21
 * @property array $db
22
 * @property boolean $useTransformers
23
 * @property string $transformerOp
24
 * @property array $modelsDatabase
25
 *
26
 */
27
trait DAOCoreTrait {
28
	protected static $accessors = [ ];
29
	protected static $fields = [ ];
30
31
	abstract public static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache): void;
32
33
	abstract protected static function prepareManyToMany($db, &$ret, $instance, $member, $annot = null);
34
35
	abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray);
36
37
	abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null);
38
39
	abstract public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void;
40
41
	abstract public static function _getIncludedForStep($included);
42
43
	abstract protected static function getDb($model);
44
45 17
	protected static function getClass_($instance) {
46 17
		if (\is_object ( $instance )) {
47 16
			return get_class ( $instance );
48
		}
49 1
		return $instance [0];
50
	}
51
52
	protected static function getInstance_($instance) {
53
		if (\is_object ( $instance )) {
54
			return $instance;
55
		}
56
		return $instance [0];
57
	}
58
59
	protected static function getValue_($instance, $member) {
60
		if (\is_object ( $instance )) {
61
			return Reflexion::getMemberValue ( $instance, $member );
62
		}
63
		return $instance [1];
64
	}
65
66 12
	protected static function getFirstKeyValue_($instance) {
67 12
		if (\is_object ( $instance )) {
68 11
			return OrmUtils::getFirstKeyValue ( $instance );
69
		}
70 1
		return $instance [1];
71
	}
72
73 50
	protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) {
74 50
		$conditionParser->limitOne ();
75 50
		$included = self::_getIncludedForStep ( $included );
76 50
		$object = $invertedJoinColumns = $oneToManyFields = $manyToManyFields = null;
77
78 50
		$metaDatas = OrmUtils::getModelMetadata ( $className );
79 50
		$tableName = $metaDatas ['#tableName'];
80 50
		$hasIncluded = $included || (\is_array ( $included ) && \count ( $included ) > 0);
81 50
		if ($hasIncluded) {
82 33
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
83
		}
84 50
		$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ];
85 50
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache, true );
86 50
		if ($query) {
87 50
			$oneToManyQueries =	$manyToOneQueries = $manyToManyParsers = [ ];
88 50
			$object = self::_loadObjectFromRow ( $db, $query, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers );
89 50
			if ($hasIncluded) {
90 33
				self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache );
91
			}
92 50
			EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className );
93
		}
94 50
		return $object;
95
	}
96
97
	/**
98
	 *
99
	 * @param Database $db
100
	 * @param string $className
101
	 * @param ConditionParser $conditionParser
102
	 * @param boolean|array $included
103
	 * @param boolean|null $useCache
104
	 * @return array
105
	 */
106 77
	protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
107 77
		$included = self::_getIncludedForStep ( $included );
108 77
		$objects = [];
109 77
		$invertedJoinColumns =$oneToManyFields = $manyToManyFields = null;
110
111 77
		$metaDatas = OrmUtils::getModelMetadata ( $className );
112 77
		$tableName = $metaDatas ['#tableName'];
113 77
		if ($hasIncluded = ($included || (\is_array ( $included ) && \count ( $included ) > 0))) {
114 31
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
115
		}
116 77
		$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ];
117 77
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
118
119 77
		$oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [ ];
120
121 77
		$propsKeys = OrmUtils::getPropKeys ( $className );
122 77
		foreach ( $query as $row ) {
123 77
			$object = self::_loadObjectFromRow ( $db, $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers );
124 77
			$objects [OrmUtils::getPropKeyValues ( $object, $propsKeys )] = $object;
125
		}
126 77
		if ($hasIncluded) {
127 31
			self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
128
		}
129 77
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
130 77
		return $objects;
131
	}
132
133 122
	public static function _getFieldList($tableName, $metaDatas) {
134 122
		return self::$fields [$tableName] ??= SqlUtils::getFieldList ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ), $tableName );
135
	}
136
137
	/**
138
	 *
139
	 * @param Database $db
140
	 * @param array $row
141
	 * @param string $className
142
	 * @param array $invertedJoinColumns
143
	 * @param array $manyToOneQueries
144
	 * @param array $oneToManyFields
145
	 * @param array $manyToManyFields
146
	 * @param array $oneToManyQueries
147
	 * @param array $manyToManyParsers
148
	 * @param array $memberNames
149
	 * @param array $accessors
150
	 * @param array $transformers
151
	 * @return object
152
	 */
153 86
	public static function _loadObjectFromRow(Database $db, $row, $className, $invertedJoinColumns, &$manyToOneQueries, $oneToManyFields, $manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, $memberNames, $accessors, $transformers) {
154 86
		$o = new $className ();
155 86
		if (self::$useTransformers) {
156 8
			self::applyTransformers ( $transformers, $row, $memberNames );
157
		}
158 86
		foreach ( $row as $k => $v ) {
159 86
			if ($accesseur = ($accessors [$k] ?? false)) {
160 86
				$o->$accesseur ( $v );
161
			}
162 86
			$o->_rest [$memberNames [$k] ?? $k] = $v;
163 86
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
164 26
				$fk = '_' . $k;
165 26
				$o->$fk = $v;
166 26
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
167
			}
168
		}
169 86
		self::loadManys ( $o, $db, $oneToManyFields, $oneToManyQueries, $manyToManyFields, $manyToManyParsers );
170 86
		return $o;
171
	}
172
173
	/**
174
	 *
175
	 * @param Database $db
176
	 * @param array $row
177
	 * @param string $className
178
	 * @param array $memberNames
179
	 * @param array $transformers
180
	 * @return object
181
	 */
182 4
	public static function _loadSimpleObjectFromRow(Database $db, $row, $className, $memberNames, $transformers) {
183 4
		$o = new $className ();
184 4
		if (self::$useTransformers) {
185
			self::applyTransformers ( $transformers, $row, $memberNames );
186
		}
187 4
		foreach ( $row as $k => $v ) {
188 4
			$o->$k = $v;
189 4
			$o->_rest [$memberNames [$k] ?? $k] = $v;
190
		}
191 4
		return $o;
192
	}
193
194 8
	protected static function applyTransformers($transformers, &$row, $memberNames) {
195 8
		foreach ( $transformers as $member => $transformer ) {
196 7
			$field = \array_search ( $member, $memberNames );
197 7
			$transform = self::$transformerOp;
198 7
			$row [$field] = $transformer::{$transform} ( $row [$field] );
199
		}
200 8
	}
201
202 86
	protected static function loadManys($o, $db, $oneToManyFields, &$oneToManyQueries, $manyToManyFields, &$manyToManyParsers) {
203 86
		if (isset ( $oneToManyFields )) {
204 48
			foreach ( $oneToManyFields as $k => $annot ) {
205 44
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
206
			}
207
		}
208 86
		if (isset ( $manyToManyFields )) {
209 26
			foreach ( $manyToManyFields as $k => $annot ) {
210 25
				self::prepareManyToMany ( $db, $manyToManyParsers, $o, $k, $annot );
211
			}
212
		}
213 86
	}
214
215 4
	private static function parseKey(&$keyValues, $className, $quote) {
216 4
		if (! \is_array ( $keyValues )) {
217 4
			if (\strrpos ( $keyValues, '=' ) === false && \strrpos ( $keyValues, '>' ) === false && \strrpos ( $keyValues, '<' ) === false) {
218 4
				$keyValues = $quote . OrmUtils::getFirstKey ( $className ) . $quote . "='" . $keyValues . "'";
219
			}
220
		}
221 4
	}
222
223
	public static function storeDbCache(string $model) {
224
		$offset = self::$modelsDatabase [$model] ?? 'default';
225
		if (isset ( self::$db [$offset] )) {
226
			self::$db [$offset]->storeCache ();
227
		}
228
	}
229
	
230 1
	public static function getModels($dbOffset='default'){
231 1
		$result=[];
232 1
		foreach ( self::$modelsDatabase as $model=>$offset){
233 1
			if($offset===$dbOffset){
234 1
				$result[]=$model;
235
			}
236
		}
237 1
		return $result;
238
	}
239
}