Passed
Push — master ( c0dc60...96b474 )
by Jean-Christophe
08:33
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 40

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11

Importance

Changes 5
Bugs 0 Features 3
Metric Value
eloc 21
c 5
b 0
f 3
dl 0
loc 31
ccs 22
cts 22
cp 1
rs 7.3166
cc 11
nc 40
nop 10
crap 11

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\log\Logger;
9
use Ubiquity\orm\OrmUtils;
10
use Ubiquity\orm\parser\ConditionParser;
11
use Ubiquity\orm\parser\Reflexion;
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.0.5
20
 *
21
 * @property \Ubiquity\db\Database $db
22
 * @property boolean $useTransformers
23
 * @property string $transformerOp
24
 *
25
 */
26
trait DAOCoreTrait {
27
	protected static $accessors = [ ];
28
	protected static $fields = [ ];
29
30
	abstract protected static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache);
31
32
	abstract protected static function prepareManyToMany(&$ret, $instance, $member, $annot = null);
33
34
	abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray);
35
36
	abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null);
37
38
	abstract protected static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields);
39
40
	abstract protected static function getIncludedForStep($included);
41
42 33
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
43 33
		foreach ( $array as $element ) {
44 29
			$elementRef = $element->$elementAccessor ();
45 29
			if (($elementRef == $fkv) || (is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
46 29
				$ret [] = $element;
47
			}
48
		}
49 33
	}
50
51
	private static function getManyToManyFromArray($instance, $array, $class, $parser) {
52
		$ret = [ ];
53
		$continue = true;
54
		$accessorToMember = "get" . ucfirst ( $parser->getInversedBy () );
55
		$myPkAccessor = "get" . ucfirst ( $parser->getMyPk () );
56
		$pk = self::getFirstKeyValue_ ( $instance );
57
58
		if (sizeof ( $array ) > 0) {
59
			$continue = method_exists ( current ( $array ), $accessorToMember );
60
		}
61
		if ($continue) {
62
			foreach ( $array as $targetEntityInstance ) {
63
				$instances = $targetEntityInstance->$accessorToMember ();
64
				if (is_array ( $instances )) {
65
					foreach ( $instances as $inst ) {
66
						if ($inst->$myPkAccessor () == $pk)
67
							array_push ( $ret, $targetEntityInstance );
68
					}
69
				}
70
			}
71
		} else {
72
			Logger::warn ( "DAO", "L'accesseur au membre " . $parser->getInversedBy () . " est manquant pour " . $parser->getTargetEntity (), "ManyToMany" );
73
		}
74
		return $ret;
75
	}
76
77 15
	protected static function getClass_($instance) {
78 15
		if (is_object ( $instance )) {
79 12
			return get_class ( $instance );
80
		}
81 3
		return $instance [0];
82
	}
83
84
	protected static function getInstance_($instance) {
85
		if (is_object ( $instance )) {
86
			return $instance;
87
		}
88
		return $instance [0];
89
	}
90
91
	protected static function getValue_($instance, $member) {
92
		if (is_object ( $instance )) {
93
			return Reflexion::getMemberValue ( $instance, $member );
94
		}
95
		return $instance [1];
96
	}
97
98 11
	protected static function getFirstKeyValue_($instance) {
99 11
		if (is_object ( $instance )) {
100 9
			return OrmUtils::getFirstKeyValue ( $instance );
101
		}
102 2
		return $instance [1];
103
	}
104
105 35
	protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache) {
106 35
		$conditionParser->limitOne ();
107 35
		$included = self::getIncludedForStep ( $included );
108 35
		$object = null;
109 35
		$invertedJoinColumns = null;
110 35
		$oneToManyFields = null;
111 35
		$manyToManyFields = null;
112
113 35
		$metaDatas = OrmUtils::getModelMetadata ( $className );
114 35
		$tableName = $metaDatas ["#tableName"];
115 35
		$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0);
116 35
		if ($hasIncluded) {
117 23
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
118
		}
119 35
		$transformers = $metaDatas ["#transformers"] [self::$transformerOp] ?? [ ];
120 35
		$query = self::$db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
121 35
		if ($query && \sizeof ( $query ) > 0) {
122 35
			$oneToManyQueries = [ ];
123 35
			$manyToOneQueries = [ ];
124 35
			$manyToManyParsers = [ ];
125 35
			$accessors = $metaDatas ["#accessors"];
126 35
			$object = self::loadObjectFromRow ( \current ( $query ), $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers );
127 35
			if ($hasIncluded) {
128 23
				self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache );
129
			}
130 35
			EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className );
131
		}
132 35
		return $object;
133
	}
134
135
	/**
136
	 *
137
	 * @param string $className
138
	 * @param ConditionParser $conditionParser
139
	 * @param boolean|array $included
140
	 * @param boolean|null $useCache
141
	 * @return array
142
	 */
143 52
	protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
144 52
		$included = self::getIncludedForStep ( $included );
145 52
		$objects = array ();
146 52
		$invertedJoinColumns = null;
147 52
		$oneToManyFields = null;
148 52
		$manyToManyFields = null;
149
150 52
		$metaDatas = OrmUtils::getModelMetadata ( $className );
151 52
		$tableName = $metaDatas ["#tableName"];
152 52
		$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0);
153 52
		if ($hasIncluded) {
154 24
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
155
		}
156 52
		$transformers = $metaDatas ["#transformers"] [self::$transformerOp] ?? [ ];
157 52
		$query = self::$db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
158 52
		$oneToManyQueries = [ ];
159 52
		$manyToOneQueries = [ ];
160 52
		$manyToManyParsers = [ ];
161 52
		$propsKeys = OrmUtils::getPropKeys ( $className );
162 52
		$accessors = $metaDatas ["#accessors"];
163 52
		foreach ( $query as $row ) {
164 52
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers );
165 52
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
166 52
			$objects [$key] = $object;
167
		}
168 52
		if ($hasIncluded) {
169 24
			self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
170
		}
171 52
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
172 52
		return $objects;
173
	}
174
175 58
	protected static function getFieldList($tableName, $metaDatas) {
176 58
		if (! isset ( self::$fields [$tableName] )) {
177 58
			$members = \array_diff ( $metaDatas ["#fieldNames"], $metaDatas ["#notSerializable"] );
178 58
			self::$fields = SqlUtils::getFieldList ( $members, $tableName );
179
		}
180 58
		return self::$fields;
181
	}
182
183
	/**
184
	 *
185
	 * @param array $row
186
	 * @param string $className
187
	 * @param array $invertedJoinColumns
188
	 * @param array $manyToOneQueries
189
	 * @param array $accessors
190
	 * @return object
191
	 */
192 58
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$manyToOneQueries, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, &$accessors, &$transformers) {
193 58
		$o = new $className ();
194 58
		if (self::$useTransformers) {
195 8
			foreach ( $transformers as $field => $transformer ) {
196 4
				$transform = self::$transformerOp;
197 4
				$row [$field] = $transformer::$transform ( $row [$field] );
198
			}
199
		}
200 58
		foreach ( $row as $k => $v ) {
201 58
			if (isset ( $accessors [$k] )) {
202 58
				$accesseur = $accessors [$k];
203 58
				$o->$accesseur ( $v );
204
			}
205 58
			$o->_rest [$k] = $v;
206 58
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
207 23
				$fk = "_" . $k;
208 23
				$o->$fk = $v;
209 46
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
210
			}
211
		}
212 58
		if (isset ( $oneToManyFields )) {
213 36
			foreach ( $oneToManyFields as $k => $annot ) {
214 33
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
215
			}
216
		}
217 58
		if (isset ( $manyToManyFields )) {
218 23
			foreach ( $manyToManyFields as $k => $annot ) {
219 23
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
220
			}
221
		}
222 58
		return $o;
223
	}
224
225 3
	private static function parseKey(&$keyValues, $className) {
226 3
		if (! is_array ( $keyValues )) {
227 3
			if (strrpos ( $keyValues, "=" ) === false && strrpos ( $keyValues, ">" ) === false && strrpos ( $keyValues, "<" ) === false) {
228 3
				$keyValues = "`" . OrmUtils::getFirstKey ( $className ) . "`='" . $keyValues . "'";
229
			}
230
		}
231 3
	}
232
}
233