Passed
Push — master ( ba7917...6c027d )
by Jean-Christophe
06:26
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 40

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.0908

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 31
ccs 20
cts 22
cp 0.9091
rs 7.3166
c 0
b 0
f 0
cc 11
nc 40
nop 10
crap 11.0908

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 22
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
42 22
		foreach ( $array as $element ) {
43 18
			$elementRef = $element->$elementAccessor ();
44 18
			if (($elementRef == $fkv) || (is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
45 18
				$ret [] = $element;
46
			}
47
		}
48 22
	}
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 24
	protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache) {
105 24
		$conditionParser->limitOne ();
106 24
		$retour = self::_getAll ( $className, $conditionParser, $included, $useCache );
107 24
		if (sizeof ( $retour ) < 1) {
108 2
			return null;
109
		}
110 24
		$result = \current ( $retour );
111 24
		EventsManager::trigger ( DAOEvents::GET_ONE, $result, $className );
112 24
		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 43
	protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
124 43
		$included = self::getIncludedForStep ( $included );
125 43
		$objects = array ();
126 43
		$invertedJoinColumns = null;
127 43
		$oneToManyFields = null;
128 43
		$manyToManyFields = null;
129
130 43
		$metaDatas = OrmUtils::getModelMetadata ( $className );
131 43
		$tableName = $metaDatas ["#tableName"];
132 43
		$hasIncluded = $included || (is_array ( $included ) && sizeof ( $included ) > 0);
133 43
		if ($hasIncluded) {
134 27
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
135
		}
136 43
		$condition = SqlUtils::checkWhere ( $conditionParser->getCondition () );
137 43
		$members = \array_diff ( $metaDatas ["#fieldNames"], $metaDatas ["#notSerializable"] );
138 43
		$transformers = $metaDatas ["#transformers"] [self::$transformerOp] ?? [ ];
139 43
		$query = self::$db->prepareAndExecute ( $tableName, $condition, $members, $conditionParser->getParams (), $useCache );
140 43
		$oneToManyQueries = [ ];
141 43
		$manyToOneQueries = [ ];
142 43
		$manyToManyParsers = [ ];
143 43
		$propsKeys = OrmUtils::getPropKeys ( $className );
144 43
		$accessors = $metaDatas ["#accessors"];
145 43
		foreach ( $query as $row ) {
146 43
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers );
147 43
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
148 43
			$objects [$key] = $object;
149
		}
150 43
		if ($hasIncluded) {
151 27
			$classPropKey = OrmUtils::getFirstPropKey ( $className );
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
	/**
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 43
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$manyToOneQueries, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, &$accessors, &$transformers) {
168 43
		$o = new $className ();
169 43
		if (self::$useTransformers) {
170 5
			foreach ( $transformers as $field => $transformer ) {
171
				$transform = self::$transformerOp;
172
				$row [$field] = $transformer::$transform ( $row [$field] );
173
			}
174
		}
175 43
		foreach ( $row as $k => $v ) {
176 43
			if (isset ( $accessors [$k] )) {
177 43
				$accesseur = $accessors [$k];
178 43
				$o->$accesseur ( $v );
179
			}
180 43
			$o->_rest [$k] = $v;
181 43
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
182 17
				$fk = "_" . $k;
183 17
				$o->$fk = $v;
184 36
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
185
			}
186
		}
187 43
		if (isset ( $oneToManyFields )) {
188 25
			foreach ( $oneToManyFields as $k => $annot ) {
189 22
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
190
			}
191
		}
192 43
		if (isset ( $manyToManyFields )) {
193 19
			foreach ( $manyToManyFields as $k => $annot ) {
194 19
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
195
			}
196
		}
197 43
		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