Passed
Push — master ( 6bed03...9b7ff5 )
by Jean-Christophe
24:45
created

DAORelationsTrait::getIncludedForStep()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 9.2222
cc 6
nc 3
nop 1
crap 6.2163
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\parser\ManyToManyParser;
7
use Ubiquity\orm\parser\ConditionParser;
8
use Ubiquity\orm\parser\Reflexion;
9
use Ubiquity\db\Database;
10
use Ubiquity\log\Logger;
11
use Ubiquity\db\SqlUtils;
12
13
/**
14
 *
15
 * @author jc
16
 * @property \Ubiquity\db\Database $db
17
 */
18
trait DAORelationsTrait {
19
20
	abstract protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL);
21
22 23
	private static function generateManyToManyParser(ManyToManyParser $parser, &$myPkValues) {
23 23
		$sql = $parser->generateConcatSQL ();
24 23
		$result = self::getDb ( $parser->getTargetEntityClass () )->prepareAndFetchAll ( $sql, $parser->getWhereValues () );
25 23
		$condition = $parser->getParserWhereMask ( " ?" );
26 23
		$cParser = new ConditionParser ();
27 23
		foreach ( $result as $row ) {
28 23
			$values = explode ( ",", $row ["_concat"] );
29 23
			$myPkValues [$row ["_field"]] = $values;
30 23
			$cParser->addParts ( $condition, $values );
31
		}
32 23
		$cParser->compileParts ();
33 23
		return $cParser;
34
	}
35
36 5
	private static function _getIncludedNext($included, $member) {
37 5
		return (isset ( $included [$member] )) ? (\is_bool ( $included [$member] ) ? $included [$member] : [ $included [$member] ]) : false;
38
	}
39
40 23
	private static function getManyToManyFromArrayIds($objectClass, $relationObjects, $ids) {
41 23
		$ret = [ ];
42 23
		$prop = OrmUtils::getFirstPropKey ( $objectClass );
43 23
		foreach ( $relationObjects as $targetEntityInstance ) {
44 23
			$id = Reflexion::getPropValue ( $targetEntityInstance, $prop );
45 23
			if (\array_search ( $id, $ids ) !== false) {
46 23
				\array_push ( $ret, $targetEntityInstance );
47
			}
48
		}
49 23
		return $ret;
50
	}
51
52 61
	protected static function getIncludedForStep($included) {
53 61
		if (\is_bool ( $included )) {
54 61
			return $included;
55
		}
56 7
		$ret = [ ];
57 7
		if (\is_array ( $included )) {
58 7
			foreach ( $included as &$includedMember ) {
59 7
				if (\is_array ( $includedMember )) {
60
					foreach ( $includedMember as $iMember ) {
61
						self::parseEncludeMember ( $ret, $iMember );
62
					}
63
				} else {
64 7
					self::parseEncludeMember ( $ret, $includedMember );
65
				}
66
			}
67
		}
68 7
		return $ret;
69
	}
70
71 7
	private static function parseEncludeMember(&$ret, $includedMember) {
72 7
		$array = \explode ( ".", $includedMember );
73 7
		$member = \array_shift ( $array );
74 7
		if (\sizeof ( $array ) > 0) {
75
			$newValue = \implode ( ".", $array );
76
			if ($newValue === '*') {
77
				$newValue = true;
78
			}
79
			if (isset ( $ret [$member] )) {
80
				if (! \is_array ( $ret [$member] )) {
81
					$ret [$member] = [ $ret [$member] ];
82
				}
83
				$ret [$member] [] = $newValue;
84
			} else {
85
				$ret [$member] = $newValue;
86
			}
87
		} else {
88 7
			if (isset ( $member ) && "" != $member) {
89 7
				$ret [$member] = false;
90
			} else {
91
				return;
92
			}
93
		}
94 7
	}
95
96 3
	private static function getInvertedJoinColumns($included, &$invertedJoinColumns) {
97 3
		foreach ( $invertedJoinColumns as $column => &$annot ) {
98 3
			$member = $annot ["member"];
99 3
			if (isset ( $included [$member] ) === false) {
100 3
				unset ( $invertedJoinColumns [$column] );
101
			}
102
		}
103 3
	}
104
105 7
	private static function getToManyFields($included, &$toManyFields) {
106 7
		foreach ( $toManyFields as $member => $annotNotUsed ) {
107 7
			if (isset ( $included [$member] ) === false) {
108 7
				unset ( $toManyFields [$member] );
109
			}
110
		}
111 7
	}
112
113 45
	protected static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields) {
114 45
		if (isset ( $metaDatas ["#invertedJoinColumn"] )) {
115 26
			$invertedJoinColumns = $metaDatas ["#invertedJoinColumn"];
116
		}
117 45
		if (isset ( $metaDatas ["#oneToMany"] )) {
118 40
			$oneToManyFields = $metaDatas ["#oneToMany"];
119
		}
120 45
		if (isset ( $metaDatas ["#manyToMany"] )) {
121 23
			$manyToManyFields = $metaDatas ["#manyToMany"];
122
		}
123 45
		if (\is_array ( $included )) {
124 7
			if (isset ( $invertedJoinColumns )) {
125 3
				self::getInvertedJoinColumns ( $included, $invertedJoinColumns );
126
			}
127 7
			if (isset ( $oneToManyFields )) {
128 7
				self::getToManyFields ( $included, $oneToManyFields );
129
			}
130 7
			if (isset ( $manyToManyFields )) {
131 3
				self::getToManyFields ( $included, $manyToManyFields );
132
			}
133
		}
134 45
	}
135
136
	/**
137
	 * Loads member associated with $instance by a ManyToOne relationship
138
	 *
139
	 * @param object|array $instance The instance object or an array with [classname,id]
140
	 * @param string $member The member to load
141
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"]
142
	 * @param boolean|null $useCache
143
	 */
144 4
	public static function getManyToOne($instance, $member, $included = false, $useCache = NULL) {
145 4
		$classname = self::getClass_ ( $instance );
146 4
		if (is_array ( $instance )) {
147 1
			$instance = self::getById ( $classname, $instance [1], false, $useCache );
148
		}
149 4
		$fieldAnnot = OrmUtils::getMemberJoinColumns ( $classname, $member );
150 4
		if ($fieldAnnot !== null) {
151 4
			$annotationArray = $fieldAnnot [1];
152 4
			$member = $annotationArray ["member"];
153 4
			$value = Reflexion::getMemberValue ( $instance, $member );
154 4
			$key = OrmUtils::getFirstKey ( $annotationArray ["className"] );
155 4
			$kv = array ($key => $value );
156 4
			$obj = self::getById ( $annotationArray ["className"], $kv, $included, $useCache );
157 4
			if ($obj !== null) {
158 4
				Logger::info ( "DAO", "Loading the member " . $member . " for the object " . $classname, "getManyToOne" );
159 4
				$accesseur = "set" . ucfirst ( $member );
160 4
				if (\is_object ( $instance ) && \method_exists ( $instance, $accesseur )) {
161 4
					$instance->$accesseur ( $obj );
162 4
					$instance->_rest [$member] = $obj->_rest;
163
				}
164 4
				return $obj;
165
			}
166
		}
167
	}
168
169
	/**
170
	 * Assign / load the child records in the $member member of $instance.
171
	 *
172
	 * @param object|array $instance The instance object or an array with [classname,id]
173
	 * @param string $member Member on which a oneToMany annotation must be present
174
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"]
175
	 * @param boolean $useCache
176
	 * @param array $annot used internally
177
	 */
178 7
	public static function getOneToMany($instance, $member, $included = true, $useCache = NULL, $annot = null) {
179 7
		$ret = array ();
180 7
		$class = self::getClass_ ( $instance );
181 7
		if (! isset ( $annot )) {
182 7
			$annot = OrmUtils::getAnnotationInfoMember ( $class, "#oneToMany", $member );
183
		}
184 7
		if ($annot !== false) {
185 7
			$fkAnnot = OrmUtils::getAnnotationInfoMember ( $annot ["className"], "#joinColumn", $annot ["mappedBy"] );
186 7
			if ($fkAnnot !== false) {
187 7
				$fkv = self::getFirstKeyValue_ ( $instance );
188 7
				$db = self::getDb ( $annot ["className"] );
189 7
				$ret = self::_getAll ( $db, $annot ["className"], ConditionParser::simple ( $db->quote . $fkAnnot ["name"] . $db->quote . "= ?", $fkv ), $included, $useCache );
190 7
				if (is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getOneToMany' )) {
191 6
					self::setToMember ( $member, $instance, $ret, $modifier );
192
				}
193
			}
194
		}
195 7
		return $ret;
196
	}
197
198
	/**
199
	 * Assigns / loads the child records in the $member member of $instance.
200
	 * If $array is null, the records are loaded from the database
201
	 *
202
	 * @param object|array $instance The instance object or an array with [classname,id]
203
	 * @param string $member Member on which a ManyToMany annotation must be present
204
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"]
205
	 * @param array $array optional parameter containing the list of possible child records
206
	 * @param boolean $useCache
207
	 */
208 4
	public static function getManyToMany($instance, $member, $included = false, $array = null, $useCache = NULL) {
209 4
		$ret = [ ];
210 4
		$class = self::getClass_ ( $instance );
211 4
		$parser = new ManyToManyParser ( $class, $member );
212 4
		if ($parser->init ()) {
213 4
			if (\is_null ( $array )) {
214 4
				$pk = self::getFirstKeyValue_ ( $instance );
215 4
				$quote = SqlUtils::$quote;
216 4
				$condition = " INNER JOIN " . $quote . $parser->getJoinTable () . $quote . " on " . $quote . $parser->getJoinTable () . $quote . "." . $quote . $parser->getFkField () . $quote . "=" . $quote . $parser->getTargetEntityTable () . $quote . "." . $quote . $parser->getPk () . $quote . " WHERE " . $quote . $parser->getJoinTable () . $quote . "." . $quote . $parser->getMyFkField () . $quote . "= ?";
217 4
				$targetEntityClass = $parser->getTargetEntityClass ();
218 4
				$ret = self::_getAll ( self::getDb ( $targetEntityClass ), $targetEntityClass, ConditionParser::simple ( $condition, $pk ), $included, $useCache );
219
			} else {
220
				$ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser );
0 ignored issues
show
Bug introduced by
The method getManyToManyFromArray() does not exist on Ubiquity\orm\traits\DAORelationsTrait. Did you maybe mean getManyToManyFromArrayIds()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
				/** @scrutinizer ignore-call */ 
221
    $ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
			}
222 4
			if (\is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getManyToMany' )) {
223 3
				self::setToMember ( $member, $instance, $ret, $modifier );
224
			}
225
		}
226 4
		return $ret;
227
	}
228
229
	/**
230
	 *
231
	 * @param object $instance
232
	 * @param array $array
233
	 * @param boolean $useCache
234
	 */
235
	public static function affectsManyToManys($instance, $array = NULL, $useCache = NULL) {
236
		$metaDatas = OrmUtils::getModelMetadata ( \get_class ( $instance ) );
237
		$manyToManyFields = $metaDatas ["#manyToMany"];
238
		if (\sizeof ( $manyToManyFields ) > 0) {
239
			foreach ( $manyToManyFields as $member ) {
240
				self::getManyToMany ( $instance, $member, false, $array, $useCache );
241
			}
242
		}
243
	}
244
}
245