Passed
Push — master ( 567975...f21acc )
by Jean-Christophe
07:44
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 40

Size

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