Test Failed
Push — master ( 87025b...f461b3 )
by Jean-Christophe
13:09
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 9
cts 9
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 2
	public function addInstance($instance, $id = null) {
18 2
		$id = $id ?? OrmUtils::getFirstKeyValue ( $instance );
19 2
		$this->instances [$id] = $instance;
20 2
	}
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
	public function groupOp($count = 5) {
31
		$quote = $this->db->quote;
32
		$groups = \array_chunk ( $this->instances, $count );
33
34
		$deleteTable = "DELETE FROM {$quote}{$this->tableName}{$quote} WHERE ";
35
		foreach ( $groups as $group ) {
36
			$sql = '';
37
			foreach ( $group as $instance ) {
38
				$kv = OrmUtils::getKeyFieldsAndValues ( $instance );
39
				$sql .= $deleteTable . $this->db->getCondition ( $kv ) . ';';
40
			}
41
			$this->execGroupTrans ( $sql );
42
		}
43
		$this->instances = [ ];
44
		$this->parameters = [ ];
45
	}
46
}
47
48