Passed
Push — master ( f461b3...0c0438 )
by Jean-Christophe
10:20
created

BulkDeletes   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 2
b 0
f 0
dl 0
loc 30
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createSQL() 0 6 1
A addInstance() 0 3 1
A groupOp() 0 15 3
1
<?php
2
3
namespace Ubiquity\orm\bulk;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * Ubiquity\orm\bulk$BulkDeletes
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 *
14
 */
15
class BulkDeletes extends AbstractBulks {
16
17 3
	public function addInstance($instance, $id = null) {
18 3
		$id = $id ?? OrmUtils::getFirstKeyValue ( $instance );
19 3
		$this->instances [$id] = $instance;
20 3
	}
21
22 2
	public function createSQL() {
23 2
		$quote = $this->db->quote;
24 2
		$this->parameters = \array_keys ( $this->instances );
25 2
		$count = \count ( $this->parameters );
26
27 2
		return "DELETE FROM {$quote}{$this->tableName}{$quote} WHERE {$quote}{$this->pkName}{$quote} IN (" . \implode ( ',', \array_fill ( 0, $count, '?' ) ) . ')';
28
	}
29
30 1
	public function groupOp($count = 5) {
31 1
		$quote = $this->db->quote;
32 1
		$groups = \array_chunk ( $this->instances, $count );
33
34 1
		$deleteTable = "DELETE FROM {$quote}{$this->tableName}{$quote} WHERE ";
35 1
		foreach ( $groups as $group ) {
36 1
			$sql = '';
37 1
			foreach ( $group as $instance ) {
38 1
				$kv = OrmUtils::getKeyFieldsAndValues ( $instance );
39 1
				$sql .= $deleteTable . $this->db->getCondition ( $kv ) . ';';
40
			}
41 1
			$this->execGroupTrans ( $sql );
42
		}
43 1
		$this->instances = [ ];
44 1
		$this->parameters = [ ];
45 1
	}
46
}
47
48