Passed
Push — master ( 1f702a...efcd94 )
by Jean-Christophe
06:18
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 20
nop 11
crap 9

How to fix   Many Parameters   

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