Passed
Push — master ( b9acc3...1dafdc )
by Jean-Christophe
03:25
created

DAORelationsPrepareTrait::prepareManyToMany()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 10
nop 4
dl 0
loc 16
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\core\PendingRelationsRequest;
7
use Ubiquity\orm\parser\ManyToManyParser;
8
9
trait DAORelationsPrepareTrait {
10
	
11
	/**
12
	 * Prepares members associated with $instance with a ManyToMany type relationship
13
	 * @param $ret array of sql conditions
14
	 * @param object $instance
15
	 * @param string $member Member on which a ManyToMany annotation must be present
16
	 * @param array $annot used internally
17
	 */
18 5
	protected static function prepareManyToMany(&$ret,$instance, $member, $annot=null) {
19 5
		$class=get_class($instance);
20 5
		if (!isset($annot)){
21
			$annot=OrmUtils::getAnnotationInfoMember($class, "#ManyToMany", $member);
22
		}
23 5
		if ($annot !== false) {
24 5
			$key=$annot["targetEntity"]."|".$member."|".$annot["inversedBy"];
25 5
			if(!isset($ret[$key])){
26 5
				$parser=new ManyToManyParser($instance, $member);
27 5
				$parser->init($annot);
28 5
				$ret[$key]=$parser;
29
			}
30 5
			$accessor="get" . ucfirst($ret[$key]->getMyPk());
31 5
			if(method_exists($instance, $accessor)){
32 5
				$fkv=$instance->$accessor();
33 5
				$ret[$key]->addValue($fkv);
34
			}
35
		}
36 5
	}
37
	
38
	/**
39
	 * Prepares members associated with $instance with a oneToMany type relationship
40
	 * @param $ret array of sql conditions
41
	 * @param object $instance
42
	 * @param string $member Member on which a OneToMany annotation must be present
43
	 * @param array $annot used internally
44
	 */
45 1
	protected static function prepareOneToMany(&$ret,$instance, $member, $annot=null) {
46 1
		$class=get_class($instance);
47 1
		if (!isset($annot))
48
			$annot=OrmUtils::getAnnotationInfoMember($class, "#oneToMany", $member);
49 1
			if ($annot !== false) {
50 1
				$fkAnnot=OrmUtils::getAnnotationInfoMember($annot["className"], "#joinColumn", $annot["mappedBy"]);
51 1
				if ($fkAnnot !== false) {
52
					$fkv=OrmUtils::getFirstKeyValue($instance);
53
					$key=$annot["className"]."|".$member."|".$annot["mappedBy"];
54
					if(!isset($ret[$key])){
55
						$ret[$key]=new PendingRelationsRequest();
56
					}
57
					$ret[$key]->addPartObject($instance,$fkAnnot["name"] . "= ?",$fkv);
58
				}
59
			}
60 1
	}
61
	
62
	/**
63
	 * Prepares members associated with $instance with a manyToOne type relationship
64
	 * @param $ret array of sql conditions
65
	 * @param object $instance
66
	 * @param mixed $value
67
	 * @param string $fkField
68
	 * @param array $annotationArray
69
	 */
70 6
	protected static function prepareManyToOne(&$ret, $instance,$value, $fkField,$annotationArray) {
71 6
		$member=$annotationArray["member"];
72 6
		$fk=OrmUtils::getFirstKey($annotationArray["className"]);
73 6
		$key=$annotationArray["className"]."|".$member."|".$fkField;
74 6
		if(!isset($ret[$key])){
75 6
			$ret[$key]=new PendingRelationsRequest();
76
		}
77 6
		$ret[$key]->addPartObject($instance,$fk . "= ?",$value);
78 6
	}
79
}
80