OnDuplicateKeyUpdateTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildOnDuplicateKeyUpdateQueryPart() 0 5 2
A onDuplicateKeyUpdate() 0 31 2
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
6
7
trait OnDuplicateKeyUpdateTrait
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $onDuplicateKeyUpdate = [];
13
14
    /**
15
     * @param string $column
16
     * @param null|int|string|double $value
17
     *
18
     * @throws QueryBuilderException
19
     * @return $this
20
     */
21
    public function onDuplicateKeyUpdate(string $column, $value)
22
    {
23
        if (empty($column)) {
24
            throw new QueryBuilderException('You must pass $column to onDuplicateKeyUpdate function!');
25
        }
26
27
        $columnNormalised = \sprintf(
28
            '%s_OnDuplicateKeyUpdate',
29
            \implode(
30
                '_',
31
                \array_map(
32
                    function ($columnPart) {
33
                        return \mb_convert_case($columnPart, MB_CASE_TITLE);
34
                    },
35
                    \explode('.', $column)
36
                )
37
            )
38
        );
39
40
        $this->onDuplicateKeyUpdate = \array_merge(
41
            $this->onDuplicateKeyUpdate,
42
            [
43
                $column => \sprintf('%s = :%s', $column, $columnNormalised),
44
            ]
45
        );
46
47
        $this->bindValue([
0 ignored issues
show
Bug introduced by
It seems like bindValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $this->/** @scrutinizer ignore-call */ 
48
               bindValue([
Loading history...
48
            $columnNormalised => $value,
49
        ]);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return null|string
56
     */
57
    protected function buildOnDuplicateKeyUpdateQueryPart(): ?string
58
    {
59
        return empty($this->onDuplicateKeyUpdate)
60
            ? null
61
            : \sprintf('ON DUPLICATE KEY UPDATE %s', \implode(', ', $this->onDuplicateKeyUpdate))
62
        ;
63
    }
64
}
65