BulkDelete   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildQuery() 0 20 1
A bindValues() 0 12 1
1
<?php
2
3
namespace League\Database\BulkSql;
4
5
use PDOStatement;
6
use League\Database\Traits\BulkSqlTrait;
7
8
class BulkDelete extends Base
9
{
10
    use BulkSqlTrait;
11
12
    const QUERY_TEMPLATE = 'DELETE FROM %s WHERE %s IN (%s)';
13
14
    protected function buildQuery(): string
15
    {
16
        $this->resetFields();
17
        $this->resetPreparedItems();
18
19
        $fields = array_map(function ($field) {
20
            return "`{$field}`";
21
        }, $this->getFields());
22
23
        $queryParams = $this->iterateOverItems($this->getPreparedItems(), function ($iteration) use ($fields) {
24
            return ':'.current($fields).'_'.$iteration;
25
        });
26
27
        return sprintf(
28
            self::QUERY_TEMPLATE,
29
            $this->getTable(),
30
            current($fields),
31
            implode(',', $queryParams)
32
        );
33
    }
34
35
    protected function bindValues(PDOStatement $statement)
36
    {
37
        $this->resetFields();
38
        $this->resetPreparedItems();
39
        $fields = $this->getFields();
40
41
        $this->iterateOverItems($this->getPreparedItems(), function ($iteration) use ($statement, $fields) {
42
            $field = current($fields);
43
            $value = $this->getPreparedItems()[$iteration][$field] ?? current($this->getPreparedItems()[$iteration]);
44
            $statement->bindValue(':'.$field.'_'.$iteration, $value);
45
        });
46
    }
47
}
48