Passed
Push — master ( 213516...f477f5 )
by Jean-Christophe
04:14
created

CRUDHelper::update()   B

Complexity

Conditions 10
Paths 56

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.3824

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
ccs 19
cts 25
cp 0.76
rs 7.6666
c 0
b 0
f 0
cc 10
nc 56
nop 2
crap 11.3824

How to fix   Complexity   

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:

1
<?php
2
3
namespace Ubiquity\controllers\crud;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\utils\http\URequest;
7
use Ubiquity\orm\DAO;
8
use Ubiquity\orm\parser\Reflexion;
9
use Ubiquity\cache\database\DbCache;
10
use Ubiquity\db\SqlUtils;
11
use Ubiquity\utils\base\UString;
12
13
class CRUDHelper {
14 2
	public static function getIdentifierFunction($model) {
15 2
		$pks = self::getPks ( $model );
16
		return function ($index, $instance) use ($pks) {
17 2
			$values = [ ];
18 2
			foreach ( $pks as $pk ) {
19 2
				$getter = "get" . ucfirst ( $pk );
20 2
				if (method_exists ( $instance, $getter )) {
21 2
					$values [] = $instance->{$getter} ();
22
				}
23
			}
24 2
			return implode ( "_", $values );
25 2
		};
26
	}
27
	
28
	public static function search($model,$search,$fields,$initialCondition="1=1"){
29
		$words=preg_split("@(\s*?(\(|\)|\|\||\&\&)\s*?)@", $search);
30
		if($words!==false){
31
			$words=array_filter($words,'strlen');
32
			$condition=$search;
33
			foreach ($words as $word){
34
				$word=trim($word);
35
				$condition=UString::replaceFirstOccurrence($word, "(".SqlUtils::getSearchWhere($fields,$word).")", $condition);
36
			}
37
			
38
			$condition=str_replace("||", " OR ", $condition);
39
			$condition=str_replace("&&", " AND ", $condition);
40
			$condition='('.$condition.') AND '.$initialCondition.'';
41
		}else{
42
			$condition=$initialCondition;
43
		}
44
		return DAO::getAll($model,$condition);
45
	}
46
	
47 1
	public static function update($instance,$values) {
48 1
		$className=\get_class($instance);
49 1
		$fieldsInRelationForUpdate=OrmUtils::getFieldsInRelationsForUpdate_($className);
50 1
		$manyToOneRelations=$fieldsInRelationForUpdate["manyToOne"];
51 1
		$manyToManyRelations=$fieldsInRelationForUpdate["manyToMany"];
52
		
53 1
		$members=array_keys($values);
54 1
		OrmUtils::setFieldToMemberNames($members, $fieldsInRelationForUpdate["relations"]);
55 1
		$update=false;
56
		
57 1
		$fieldTypes=OrmUtils::getFieldTypes($className);
58 1
		foreach ( $fieldTypes as $property => $type ) {
59 1
			if ($type == "tinyint(1)") {
60
				if (isset($values[$property])) {
61
					$values[$property]=1;
62
				} else {
63
					$values[$property]=0;
64
				}
65
			}
66
		}
67 1
		URequest::setValuesToObject($instance, $values);
68 1
		if($manyToOneRelations){
69
			self::updateManyToOne($manyToOneRelations, $members, $className, $instance, $values);
70
		}
71 1
		if (isset($instance)) {
72 1
			if ($instance->_new) {
73
				$update=DAO::insert($instance);
74
			} else {
75 1
				$update=DAO::update($instance);
76 1
				if (DbCache::$active) {
77
					// TODO update dbCache
78
				}
79
			}
80 1
			if ($update && $manyToManyRelations) {
81
				self::updateManyToMany($manyToManyRelations, $members, $className, $instance, $values);
82
			}
83
		}
84 1
		return $update;
85
	}
86
	
87
	protected static function updateManyToOne($manyToOneRelations,$members,$className,$instance,$values){
88
		foreach ( $manyToOneRelations as $member ) {
89
			if (array_search($member, $members)!==false) {
90
				$joinColumn=OrmUtils::getAnnotationInfoMember($className, "#joinColumn", $member);
91
				if ($joinColumn) {
92
					$fkClass=$joinColumn["className"];
93
					$fkField=$joinColumn["name"];
94
					if (isset($values[$fkField])) {
95
						$fkObject=DAO::getOne($fkClass, $values["$fkField"]);
96
						Reflexion::setMemberValue($instance, $member, $fkObject);
97
					}
98
				}
99
			}
100
		}
101
	}
102
	
103
	protected static function updateManyToMany($manyToManyRelations,$members,$className,$instance,$values){
104
		foreach ( $manyToManyRelations as $member ) {
105
			if(array_search($member, $members)!==false){
106
				if (($annot=OrmUtils::getAnnotationInfoMember($className, "#manyToMany", $member)) !== false) {
107
					$newField=$member . "Ids";
108
					$fkClass=$annot["targetEntity"];
109
					$fkObjects=DAO::getAll($fkClass, self::getMultiWhere($values[$newField], $className));
110
					if (Reflexion::setMemberValue($instance, $member, $fkObjects)) {
111
						DAO::insertOrUpdateManyToMany($instance, $member);
112
					}
113
				}
114
			}
115
		}
116
	}
117
	
118 2
	private static function getPks($model) {
119 2
		$instance=new $model();
120 2
		return OrmUtils::getKeyFields($instance);
121
	}
122
	
123
	private static function getMultiWhere($ids, $class) {
124
		$pk=OrmUtils::getFirstKey($class);
125
		$ids=explode(",", $ids);
126
		if (sizeof($ids) < 1)
127
			return "";
128
		$strs=[ ];
129
		$idCount=\sizeof($ids);
130
		for($i=0; $i < $idCount; $i++) {
131
			$strs[]=$pk . "='" . $ids[$i] . "'";
132
		}
133
		return implode(" OR ", $strs);
134
	}
135
	
136 1
	public static function getFkIntance($instance,$model,$member,$included=false){
137 1
		$result=[];
138 1
		if (($annot=OrmUtils::getAnnotationInfoMember($model, "#oneToMany", $member)) !== false) {
139 1
			$objectFK=DAO::getOneToMany($instance, $member,$included);
140 1
			$fkClass=$annot["className"];
141
		} elseif (($annot=OrmUtils::getAnnotationInfoMember($model, "#manyToMany", $member)) !== false) {
142
			$objectFK=DAO::getManyToMany($instance, $member);
143
			$fkClass=$annot["targetEntity"];
144
		} else {
145
			$objectFK=Reflexion::getMemberValue($instance, $member);
146
			if(!is_object($objectFK)){
147
				$objectFK=DAO::getManyToOne($instance, $member,$included);
148
			}
149
			if (isset($objectFK))
150
				$fkClass=\get_class($objectFK);
151
		}
152 1
		if(isset($fkClass)){
153 1
			$fkTable=OrmUtils::getTableName($fkClass);
154 1
			$result[$member]=compact("objectFK","fkClass","fkTable");
155
		}
156 1
		return $result;
157
	}
158
	
159 1
	public static function getFKIntances($instance,$model,$included=false){
160 1
		$result=[];
161 1
		$relations=OrmUtils::getFieldsInRelations($model);
162 1
		foreach ( $relations as $member ) {
163 1
			$fkInstance=self::getFkIntance($instance, $model, $member,$included);
164 1
			if(sizeof($fkInstance)>0){
165 1
				$result=array_merge($result,$fkInstance);
166
			}
167
		}
168 1
		return $result;
169
	}
170
}
171
172