RunnableInsert::insertRows()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 1
b 0
f 1
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Kir\MySQL\Builder;
4
5
use BadMethodCallException;
6
use Kir\MySQL\Builder\Internal\DDLPreparable;
7
use Kir\MySQL\Builder\Internal\DDLRunnable;
8
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
9
10
/**
11
 * @implements DDLPreparable<int>
12
 */
13
class RunnableInsert extends Insert implements DDLPreparable {
14
	/** @use CreateDDLRunnable<int> */
15
	use CreateDDLRunnable;
16
17
	/**
18
	 * @inheritDoc
19
	 */
20
	public function insertRows(iterable $rows) {
21
		if(!is_iterable($rows)) {
22
			throw new BadMethodCallException('Expected $rows to by an iterable');
23
		}
24
		$result = [];
25
		$query = $this->__toString();
26
		$stmt = $this->db()->prepare($query);
27
		foreach($rows as $row) {
28
			$stmt->execute($row);
29
			$result[] = (int) $this->db()->getLastInsertId();
30
		}
31
		$stmt->closeCursor();
32
33
		return $result;
34
	}
35
36
	/**
37
	 * @inheritDoc
38
	 */
39
	public function run(array $params = []): int {
40
		return $this->prepare()->run($params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->prepare()->run($params) could return the type Kir\MySQL\Builder\Internal\T which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
41
	}
42
43
	/**
44
	 * @return DDLRunnable<int>
45
	 */
46
	public function prepare(): DDLRunnable {
47
		return $this->createPreparable(
48
			$this->db()->prepare($this),
49
			fn() => (int) $this->db()->getLastInsertId()
50
		);
51
	}
52
}
53