Test Failed
Push — master ( 7df265...08d2f1 )
by Jean-Christophe
06:27
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 36

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 11

Importance

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