Passed
Push — master ( 7ebb22...937a2c )
by Jean-Christophe
05:49
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 36

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 30
ccs 22
cts 22
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
34
	abstract protected static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields);
35
36
	abstract protected static function getIncludedForStep($included);
37
38 22
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
39 22
		foreach ( $array as $element ) {
40 18
			$elementRef = $element->$elementAccessor ();
41 18
			if (($elementRef == $fkv) || (is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
42 18
				$ret [] = $element;
43
			}
44
		}
45 22
	}
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
		return $ret;
80
	}
81
82 15
	protected static function getClass_($instance) {
83 15
		if (is_object ( $instance )) {
84 12
			return get_class ( $instance );
85
		}
86 3
		return $instance [0];
87
	}
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
			return Reflexion::getMemberValue ( $instance, $member );
99
		}
100
		return $instance [1];
101
	}
102
103 11
	protected static function getFirstKeyValue_($instance) {
104 11
		if (is_object ( $instance )) {
105 9
			return OrmUtils::getFirstKeyValue ( $instance );
106
		}
107 2
		return $instance [1];
108
	}
109
110 24
	protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache) {
111 24
		$conditionParser->limitOne ();
112 24
		$retour = self::_getAll ( $className, $conditionParser, $included, $useCache );
113 24
		if (sizeof ( $retour ) < 1) {
114 2
			return null;
115
		}
116 24
		$result = \current ( $retour );
117 24
		EventsManager::trigger ( DAOEvents::GET_ONE, $result, $className );
118 24
		return $result;
119
	}
120
121
	/**
122
	 *
123
	 * @param string $className
124
	 * @param ConditionParser $conditionParser
125
	 * @param boolean|array $included
126
	 * @param boolean|null $useCache
127
	 * @return array
128
	 */
129 43
	protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
130 43
		$included = self::getIncludedForStep ( $included );
131 43
		$objects = array ();
132 43
		$invertedJoinColumns = null;
133 43
		$oneToManyFields = null;
134 43
		$manyToManyFields = null;
135
136 43
		$metaDatas = OrmUtils::getModelMetadata ( $className );
137 43
		$tableName = $metaDatas ["#tableName"];
138 43
		$hasIncluded = $included || (is_array ( $included ) && sizeof ( $included ) > 0);
139 43
		if ($hasIncluded) {
140 27
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
141
		}
142 43
		$condition = SqlUtils::checkWhere ( $conditionParser->getCondition () );
143 43
		$members = \array_diff ( $metaDatas ["#fieldNames"], $metaDatas ["#notSerializable"] );
144 43
		$query = self::$db->prepareAndExecute ( $tableName, $condition, $members, $conditionParser->getParams (), $useCache );
145 43
		$oneToManyQueries = [ ];
146 43
		$manyToOneQueries = [ ];
147 43
		$manyToManyParsers = [ ];
148 43
		$propsKeys = OrmUtils::getPropKeys ( $className );
149 43
		$accessors = OrmUtils::getAccessors ( $className, $members );
150 43
		$fields = array_flip ( $members );
151 43
		foreach ( $query as $row ) {
152 43
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToOneQueries, $manyToManyParsers, $accessors, $fields );
153 43
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
154 43
			$objects [$key] = $object;
155
		}
156 43
		if ($hasIncluded) {
157 27
			$classPropKey = OrmUtils::getFirstPropKey ( $className );
158 27
			self::_affectsRelationObjects ( $className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
159
		}
160 43
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
161 43
		return $objects;
162
	}
163
164
	/**
165
	 *
166
	 * @param array $row
167
	 * @param string $className
168
	 * @param array $invertedJoinColumns
169
	 * @param array $oneToManyFields
170
	 * @param array $oneToManyQueries
171
	 * @param array $manyToOneQueries
172
	 * @param array $manyToManyParsers
173
	 * @param array $accessors
174
	 * @param array $fields
175
	 * @return object
176
	 */
177 43
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToOneQueries, &$manyToManyParsers, &$accessors, &$fields) {
178 43
		$o = new $className ();
179 43
		foreach ( $row as $k => $v ) {
180 43
			if (isset ( $fields [$k] )) {
181 43
				if (isset ( $accessors [$k] )) {
182 43
					$accesseur = $accessors [$k];
183 43
					$o->$accesseur ( $v );
184 42
				} elseif (isset ( $accessors [$fields [$k]] )) {
185 42
					$accesseur = $accessors [$fields [$k]];
186 42
					$o->$accesseur ( $v );
187
				}
188
			}
189 43
			$o->_rest [$k] = $v;
190 43
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
191 17
				$fk = "_" . $k;
192 17
				$o->$fk = $v;
193 36
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
194
			}
195
		}
196 43
		if (isset ( $oneToManyFields )) {
197 25
			foreach ( $oneToManyFields as $k => $annot ) {
198 22
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
199
			}
200
		}
201 43
		if (isset ( $manyToManyFields )) {
202 19
			foreach ( $manyToManyFields as $k => $annot ) {
203 19
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
204
			}
205
		}
206 43
		return $o;
207
	}
208
209 3
	private static function parseKey(&$keyValues, $className) {
210 3
		if (! is_array ( $keyValues )) {
211 3
			if (strrpos ( $keyValues, "=" ) === false && strrpos ( $keyValues, ">" ) === false && strrpos ( $keyValues, "<" ) === false) {
212 3
				$keyValues = "`" . OrmUtils::getFirstKey ( $className ) . "`='" . $keyValues . "'";
213
			}
214
		}
215 3
	}
216
}
217
218