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