BulkInsert::resetOnDuplicateUpdateStatement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Database\BulkSql;
4
5
use PDOStatement;
6
use League\Database\Traits\BulkSqlTrait;
7
use League\Database\Exceptions\LogicException;
8
9
class BulkInsert extends Base
10
{
11
    use BulkSqlTrait;
12
13
    private $onDuplicateUpdate = [];
14
15
    const QUERY_TEMPLATE = 'INSERT %s INTO %s (%s) VALUES %s %s';
16
17 View Code Duplication
    protected function buildQuery() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19
        $this->resetFields();
20
        $this->resetPreparedItems();
21
22
        $queryParams = $this->iterateOverItems($this->getPreparedItems(), function ($iteration) {
23
            $prepared = array_map(function ($field, $key) use ($iteration) {
24
                if (in_array($field, $this->getIgnoredColumn(), true)) {
25
                    return $this->getPreparedItems()[$iteration][$field] ?? $this->getPreparedItems()[$iteration][$key];
26
                }
27
28
                return ':'.$field.'_'.$iteration;
29
            }, $this->getFields(), array_keys($this->getFields()));
30
31
            return '('.implode(',', $prepared).')';
32
        });
33
34
        $fields = array_map(function ($field) {
35
            return "`{$field}`";
36
        }, $this->getFields());
37
38
        return sprintf(
39
            self::QUERY_TEMPLATE,
40
            ($this->isIgnoreUsed() ? 'IGNORE' : ''),
41
            $this->getTable(),
42
            implode(',', $fields),
43
            implode(',', $queryParams),
44
            $this->getOnDuplicateUpdateRow()
45
        );
46
    }
47
48 View Code Duplication
    protected function bindValues(PDOStatement $statement)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $this->resetFields();
51
        $this->resetPreparedItems();
52
53
        $this->iterateOverItems($this->getPreparedItems(), function ($iteration) use ($statement) {
54
            foreach ($this->getFields() as $key => $field) {
55
                if (in_array($field, $this->getIgnoredColumn(), true)) {
56
                    return;
57
                }
58
                $value = $this->getPreparedItems()[$iteration][$field] ?? $this->getPreparedItems()[$iteration][$key];
59
                $statement->bindValue(':'.$field.'_'.$iteration, $value);
60
            }
61
        });
62
    }
63
64
    public function addOnDuplicateUpdateStatement(array $array)
65
    {
66
        if (!array_is_assoc($array)) {
67
            throw new LogicException('ON DUPLICATE UPDATE setter should take associative array');
68
        } elseif (array_depth($array) !== 1) {
69
            throw new LogicException('ON DUPLICATE UPDATE setter should take one depth array');
70
        }
71
72
        $this->onDuplicateUpdate += $array;
73
    }
74
75
    public function resetOnDuplicateUpdateStatement() : self
76
    {
77
        $this->onDuplicateUpdate = [];
78
79
        return $this;
80
    }
81
82
    private function getOnDuplicateUpdateRow() : string
83
    {
84
        if (!$this->onDuplicateUpdate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->onDuplicateUpdate of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
            return '';
86
        }
87
88
        return ' ON DUPLICATE KEY UPDATE ' . implode(', ', array_map(function ($key, $value) {
89
            return "$key = $value";
90
        }, array_keys($this->onDuplicateUpdate), $this->onDuplicateUpdate));
91
    }
92
}
93