Test Failed
Push — master ( 26590d...3034a5 )
by Jean-Christophe
14:48 queued 27s
created

SDAO   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 10
eloc 43
c 7
b 0
f 3
dl 0
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _getAll() 0 13 2
A sloadObjectFromRow() 0 7 2
A _getOne() 0 13 3
A update() 0 23 3
1
<?php
2
3
namespace Ubiquity\orm;
4
5
use Ubiquity\db\Database;
6
use Ubiquity\db\SqlUtils;
7
use Ubiquity\events\DAOEvents;
8
use Ubiquity\events\EventsManager;
9
use Ubiquity\log\Logger;
10
use Ubiquity\orm\parser\ConditionParser;
11
use Ubiquity\orm\parser\Reflexion;
12
13
/**
14
 * Ubiquity\orm$SDAO
15
 * This class is part of Ubiquity
16
 *
17
 * @author jcheron <[email protected]>
18
 * @version 1.0.0
19
 *
20
 */
21
class SDAO extends DAO {
22
23
	protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) {
1 ignored issue
show
Unused Code introduced by
The parameter $included is not used and could be removed. ( Ignorable by Annotation )

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

23
	protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, /** @scrutinizer ignore-unused */ $included, $useCache) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
		$conditionParser->limitOne ();
25
		$object = null;
26
27
		$metaDatas = OrmUtils::getModelMetadata ( $className );
28
		$tableName = $metaDatas ['#tableName'];
29
30
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
31
		if ($query && \sizeof ( $query ) > 0) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
			$object = self::sloadObjectFromRow ( \current ( $query ), $className );
33
			EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className );
34
		}
35
		return $object;
36
	}
37
38
	/**
39
	 *
40
	 * @param Database $db
41
	 * @param string $className
42
	 * @param ConditionParser $conditionParser
43
	 * @param boolean|array $included
44
	 * @param boolean|null $useCache
45
	 * @return array
46
	 */
47
	protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
1 ignored issue
show
Unused Code introduced by
The parameter $included is not used and could be removed. ( Ignorable by Annotation )

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

47
	protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, /** @scrutinizer ignore-unused */ $included = true, $useCache = NULL) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		$objects = array ();
49
50
		$metaDatas = OrmUtils::getModelMetadata ( $className );
51
		$tableName = $metaDatas ['#tableName'];
52
53
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
54
55
		foreach ( $query as $row ) {
56
			$objects [] = self::sloadObjectFromRow ( $row, $className );
57
		}
58
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
59
		return $objects;
60
	}
61
62
	private static function sloadObjectFromRow($row, $className) {
63
		$o = new $className ();
64
		foreach ( $row as $k => $v ) {
65
			$o->$k = $v;
66
		}
67
		$o->_rest = $row;
68
		return $o;
69
	}
70
71
	/**
72
	 * Updates an existing $instance in the database.
73
	 * Be careful not to modify the primary key
74
	 *
75
	 * @param object $instance instance to modify
76
	 * @param boolean $updateMany Adds or updates ManyToMany members
77
	 */
78
	public static function update($instance, $updateMany = false) {
1 ignored issue
show
Unused Code introduced by
The parameter $updateMany is not used and could be removed. ( Ignorable by Annotation )

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

78
	public static function update($instance, /** @scrutinizer ignore-unused */ $updateMany = false) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
		EventsManager::trigger ( 'dao.before.update', $instance );
80
		$className = \get_class ( $instance );
81
		$db = self::getDb ( $className );
82
		$quote = $db->quote;
83
		$tableName = OrmUtils::getTableName ( $className );
84
		$ColumnskeyAndValues = Reflexion::getPropertiesAndValues ( $instance );
85
		$keyFieldsAndValues = OrmUtils::getKeyFieldsAndValues ( $instance );
86
		$sql = "UPDATE {$quote}{$tableName}{$quote} SET " . SqlUtils::getUpdateFieldsKeyAndValues ( $ColumnskeyAndValues ) . ' WHERE ' . SqlUtils::getWhere ( $keyFieldsAndValues );
87
		if (Logger::isActive ()) {
88
			Logger::info ( "DAOUpdates", $sql, "update" );
89
			Logger::info ( "DAOUpdates", \json_encode ( $ColumnskeyAndValues ), "Key and values" );
90
		}
91
		$statement = $db->getUpdateStatement ( $sql );
92
		try {
93
			$result = $statement->execute ( $ColumnskeyAndValues );
94
			EventsManager::trigger ( DAOEvents::AFTER_UPDATE, $instance, $result );
95
			$instance->_rest = \array_merge ( $instance->_rest, $ColumnskeyAndValues );
96
			return $result;
97
		} catch ( \Exception $e ) {
98
			Logger::warn ( "DAOUpdates", $e->getMessage (), "update" );
99
		}
100
		return false;
101
	}
102
}
103
104