Passed
Push — master ( 55710c...55bc39 )
by Jean-Christophe
06:21
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 20
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
39 20
		foreach ( $array as $element ) {
40 17
			$elementRef = $element->$elementAccessor ();
41 17
			if (($elementRef == $fkv) || (is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
42 17
				$ret [] = $element;
43
			}
44
		}
45 20
	}
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 14
	protected static function getClass_($instance) {
83 14
		if (is_object ( $instance )) {
84 12
			return get_class ( $instance );
85
		}
86 2
		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 4
	protected static function getValue_($instance, $member) {
97 4
		if (is_object ( $instance )) {
98 3
			return Reflexion::getMemberValue ( $instance, $member );
99
		}
100 1
		return $instance [1];
101
	}
102
103 10
	protected static function getFirstKeyValue_($instance) {
104 10
		if (is_object ( $instance )) {
105 9
			return OrmUtils::getFirstKeyValue ( $instance );
106
		}
107 1
		return $instance [1];
108
	}
109
110 23
	protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache) {
111 23
		$conditionParser->limitOne ();
112 23
		$retour = self::_getAll ( $className, $conditionParser, $included, $useCache );
113 23
		if (sizeof ( $retour ) < 1) {
114 2
			return null;
115
		}
116 23
		$result = \current ( $retour );
117 23
		EventsManager::trigger ( DAOEvents::GET_ONE, $result, $className );
118 23
		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 41
	protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
130 41
		$included = self::getIncludedForStep ( $included );
131 41
		$objects = array ();
132 41
		$invertedJoinColumns = null;
133 41
		$oneToManyFields = null;
134 41
		$manyToManyFields = null;
135
136 41
		$metaDatas = OrmUtils::getModelMetadata ( $className );
137 41
		$tableName = $metaDatas ["#tableName"];
138 41
		$hasIncluded = $included || (is_array ( $included ) && sizeof ( $included ) > 0);
139 41
		if ($hasIncluded) {
140 25
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
141
		}
142 41
		$condition = SqlUtils::checkWhere ( $conditionParser->getCondition () );
143 41
		$members = \array_diff ( $metaDatas ["#fieldNames"], $metaDatas ["#notSerializable"] );
144 41
		$query = self::$db->prepareAndExecute ( $tableName, $condition, $members, $conditionParser->getParams (), $useCache );
145 41
		$oneToManyQueries = [ ];
146 41
		$manyToOneQueries = [ ];
147 41
		$manyToManyParsers = [ ];
148 41
		$propsKeys = OrmUtils::getPropKeys ( $className );
149 41
		$accessors = OrmUtils::getAccessors ( $className, $members );
150 41
		$fields = array_flip ( $members );
151 41
		foreach ( $query as $row ) {
152 41
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToOneQueries, $manyToManyParsers, $accessors, $fields );
153 41
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
154 41
			$objects [$key] = $object;
155
		}
156 41
		if ($hasIncluded) {
157 25
			$classPropKey = OrmUtils::getFirstPropKey ( $className );
158 25
			self::_affectsRelationObjects ( $className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
159
		}
160 41
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
161 41
		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 41
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToOneQueries, &$manyToManyParsers, &$accessors, &$fields) {
178 41
		$o = new $className ();
179 41
		foreach ( $row as $k => $v ) {
180 41
			if (isset ( $fields [$k] )) {
181 41
				if (isset ( $accessors [$k] )) {
182 41
					$accesseur = $accessors [$k];
183 41
					$o->$accesseur ( $v );
184 40
				} elseif (isset ( $accessors [$fields [$k]] )) {
185 40
					$accesseur = $accessors [$fields [$k]];
186 40
					$o->$accesseur ( $v );
187
				}
188
			}
189 41
			$o->_rest [$k] = $v;
190 41
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
191 15
				$fk = "_" . $k;
192 15
				$o->$fk = $v;
193 34
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
194
			}
195
		}
196 41
		if (isset ( $oneToManyFields )) {
197 23
			foreach ( $oneToManyFields as $k => $annot ) {
198 20
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
199
			}
200
		}
201 41
		if (isset ( $manyToManyFields )) {
202 17
			foreach ( $manyToManyFields as $k => $annot ) {
203 17
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
204
			}
205
		}
206 41
		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