Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

AbstractBulks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
eloc 39
c 5
b 0
f 0
dl 0
loc 65
ccs 0
cts 47
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B flush() 0 22 8
A getQuotedKeys() 0 6 2
A updateInstanceRest() 0 4 2
A __construct() 0 6 1
A addInstances() 0 3 2
1
<?php
2
3
namespace Ubiquity\orm\bulk;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\log\Logger;
8
use Ubiquity\controllers\Startup;
9
10
/**
11
 * Ubiquity\orm\bulk$AbstractBulks
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.0
16
 *
17
 */
18
abstract class AbstractBulks {
19
	protected $class;
20
	protected $pkName;
21
	protected $fields;
22
	protected $db;
23
	protected $instances = [ ];
24
	protected $parameters;
25
	protected $dbType;
26
	protected $insertFields;
27
28
	protected function getQuotedKeys($fields, $quote) {
29
		$ret = array ();
30
		foreach ( $fields as $field ) {
31
			$ret [] = $quote . $field . $quote;
32
		}
33
		return $ret;
34
	}
35
36
	protected function updateInstanceRest($instance) {
37
		foreach ( $this->fields as $field ) {
38
			$accessor = "get" . \ucfirst ( $field );
39
			$instance->_rest [$field] = $instance->$accessor ();
40
		}
41
	}
42
43
	public function __construct($className) {
44
		$this->class = $className;
45
		$this->pkName = OrmUtils::getFirstKey ( $className );
46
		$this->fields = OrmUtils::getSerializableFields ( $className );
47
		$this->db = DAO::getDb ( $className );
48
		$this->dbType = $this->db->getDbType ();
49
	}
50
51
	public function addInstances($instances) {
52
		foreach ( $instances as $instance ) {
53
			$this->addInstance ( $instance );
54
		}
55
	}
56
57
	public abstract function addInstance($instance);
58
59
	public abstract function createSQL();
60
61
	public function flush() {
62
		$statement = $this->db->getUpdateStatement ( $this->createSQL () );
63
		while ( true ) {
64
			try {
65
				$result = $statement->execute ( $this->parameters );
66
				if ($result !== false) {
67
					$this->instances = [ ];
68
					$this->parameters = [ ];
69
					return $result;
70
				}
71
			} catch ( \Exception $e ) {
72
				Logger::warn ( "DAOBulkUpdates", $e->getMessage (), \get_class ( $this ) );
73
				if ($e->errorInfo [0] == 40001 && $this->db->getDbType () == 'mysql' && $e->errorInfo [1] == 1213) {
74
					echo "deadlock";
75
				} else {
76
					if (Startup::$config ['debug']) {
77
						throw $e;
78
					}
79
				}
80
			}
81
		}
82
		return false;
83
	}
84
}
85
86