Completed
Push — master ( 681f8a...cbe892 )
by Jean-Christophe
02:08
created

ManyToManyParser::init()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 8
nop 1
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * ManyToManyParser
9
 * @author jc
10
 * @version 1.1.0.0
11
 */
12
class ManyToManyParser {
13
	private $member;
14
	private $joinTable;
15
	private $myFkField;
16
	private $fkField;
17
	private $targetEntity;
18
	private $targetEntityClass;
19
	private $targetEntityTable;
20
	private $myPk;
21
	private $inversedBy;
22
	private $pk;
23
	private $instance;
24
	private $whereValues;
25
26
	public function __construct($instance, $member) {
27
		$this->instance=$instance;
28
		$this->member=$member;
29
		$this->whereValues=[];
30
	}
31
32
	public function init($annot=false) {
33
		$member=$this->member;
34
		$class=$this->instance;
35
		if(\is_string($class)===false){
36
			$class=get_class($class);
37
		}
38
		if($annot===false){
39
			$annot=OrmUtils::getAnnotationInfoMember($class, "#manyToMany", $member);
40
		}
41
		if ($annot !== false) {
42
			$this->_init($class,$annot);
43
			return true;
44
		}
45
		return false;
46
	}
47
	
48
	private function _init($class,$annot){
49
		$this->targetEntity=$annot["targetEntity"];
50
		$this->inversedBy=strtolower($this->targetEntity) . "s";
51
		if (!is_null($annot["inversedBy"]))
52
			$this->inversedBy=$annot["inversedBy"];
53
		$this->targetEntityClass=get_class(new $this->targetEntity());
54
		
55
		$annotJoinTable=OrmUtils::getAnnotationInfoMember($class, "#joinTable", $this->member);
56
		$this->joinTable=$annotJoinTable["name"];
57
		
58
		$this->myFkField=OrmUtils::getDefaultFk($class);
59
		$this->myPk=OrmUtils::getFirstKey($class);
60
		if(isset($annotJoinTable["joinColumns"])){
61
			$joinColumnsAnnot=$annotJoinTable["joinColumns"];
62
			if (!is_null($joinColumnsAnnot)) {
63
				$this->myFkField=$joinColumnsAnnot["name"];
64
				$this->myPk=$joinColumnsAnnot["referencedColumnName"];
65
			}
66
		}
67
		$this->targetEntityTable=OrmUtils::getTableName($this->targetEntity);
68
		$this->fkField=OrmUtils::getDefaultFk($this->targetEntityClass);
69
		$this->pk=OrmUtils::getFirstKey($this->targetEntityClass);
70
		if(isset($annotJoinTable["inverseJoinColumns"])){
71
			$inverseJoinColumnsAnnot=$annotJoinTable["inverseJoinColumns"];
72
			if (!is_null($inverseJoinColumnsAnnot)) {
73
				$this->fkField=$inverseJoinColumnsAnnot["name"];
74
				$this->pk=$inverseJoinColumnsAnnot["referencedColumnName"];
75
			}
76
		}
77
	}
78
79
	public function getMember() {
80
		return $this->member;
81
	}
82
83
	public function setMember($member) {
84
		$this->member=$member;
85
		return $this;
86
	}
87
88
	public function getJoinTable() {
89
		return $this->joinTable;
90
	}
91
92
	public function setJoinTable($joinTable) {
93
		$this->joinTable=$joinTable;
94
		return $this;
95
	}
96
97
	public function getMyFkField() {
98
		return $this->myFkField;
99
	}
100
101
	public function setMyFkField($myFkField) {
102
		$this->myFkField=$myFkField;
103
		return $this;
104
	}
105
106
	public function getFkField() {
107
		return $this->fkField;
108
	}
109
110
	public function setFkField($fkField) {
111
		$this->fkField=$fkField;
112
		return $this;
113
	}
114
115
	public function getTargetEntity() {
116
		return $this->targetEntity;
117
	}
118
119
	public function setTargetEntity($targetEntity) {
120
		$this->targetEntity=$targetEntity;
121
		return $this;
122
	}
123
124
	public function getTargetEntityClass() {
125
		return $this->targetEntityClass;
126
	}
127
128
	public function setTargetEntityClass($targetEntityClass) {
129
		$this->targetEntityClass=$targetEntityClass;
130
		return $this;
131
	}
132
133
	public function getTargetEntityTable() {
134
		return $this->targetEntityTable;
135
	}
136
137
	public function setTargetEntityTable($targetEntityTable) {
138
		$this->targetEntityTable=$targetEntityTable;
139
		return $this;
140
	}
141
142
	public function getMyPk() {
143
		return $this->myPk;
144
	}
145
146
	public function setMyPk($myPk) {
147
		$this->myPk=$myPk;
148
		return $this;
149
	}
150
151
	public function getPk() {
152
		return $this->pk;
153
	}
154
155
	public function setPk($pk) {
156
		$this->pk=$pk;
157
		return $this;
158
	}
159
160
	public function getInversedBy() {
161
		return $this->inversedBy;
162
	}
163
164
	public function setInversedBy($inversedBy) {
165
		$this->inversedBy=$inversedBy;
166
		return $this;
167
	}
168
169
	public function getInstance() {
170
		return $this->instance;
171
	}
172
173
	public function setInstance($instance) {
174
		$this->instance=$instance;
175
		return $this;
176
	}
177
	
178
	private function getSQL(){
179
		return " INNER JOIN `" . $this->getJoinTable() . "` on `".$this->getJoinTable()."`.`".$this->getFkField()."`=`".$this->getTargetEntityTable()."`.`".$this->getPk()."`";
180
	}
181
	
182
	public function getJoinSQL($value=" ?"){
183
		return "SELECT `".$this->fkField."`  FROM `".$this->joinTable."` WHERE `".$this->myFkField . "`=".$value;
184
	}
185
	
186
	private function getWhereMask($mask="'{value}'"){
187
		return "`".$this->getJoinTable()."`.`". $this->getMyFkField() . "`=".$mask;
188
	}
189
	
190
	private function getParserWhereMask($mask="'{value}'"){
191
		return "`".$this->getTargetEntityTable()."`.`". $this->getPk() . "`=".$mask;
192
	}
193
	
194
	private function generateWhereValues(){
195
		$mask=$this->getWhereMask("");
196
		$res=[];
197
		$values=array_keys($this->whereValues);
198
		foreach ($values as $value){
199
			$res[]=str_replace("{value}", $value, $mask);
200
		}
201
		return implode(" OR ", $res);
202
	}
203
	
204
	private function generateParserWhereValues(ConditionParser $cParser){
205
		$mask=$this->getParserWhereMask(" ?");
206
		$res=[];
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
207
		$values=array_keys($this->whereValues);
208
		foreach ($values as $value){
209
			$cParser->addPart($mask, $value);
210
		}
211
	}
212
	
213
	public function generate(){
214
		if(sizeof($this->whereValues)>0){
215
			return $this->getSQL()." WHERE ".$this->generateWhereValues();
216
		}
217
		return;
218
	}
219
	
220
	/**
221
	 * @return \Ubiquity\orm\parser\ConditionParser
222
	 */
223
	public function generateConditionParser(){
224
		$cParser=new ConditionParser();
225
		if(sizeof($this->whereValues)>0){
226
			$this->generateParserWhereValues($cParser);
227
			$cParser->compileParts();
228
		}
229
		return $cParser;
230
	}
231
	
232
	public function addValue($value){
233
		$this->whereValues[$value]=true;
234
	}
235
	
236
	public function addValues($values){
237
		foreach ($values as $value){
238
			$this->whereValues[$value]=true;
239
		}
240
	}
241
}
242