Passed
Push — master ( 9b7ff5...a0d60e )
by Jean-Christophe
10:08
created

DAOCoreTrait::_getAll()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6

Importance

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