Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

BuildsInserts::insertOrIgnore()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 18
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException;
8
9
trait BuildsInserts
10
{
11
    /**
12
     * Insert a new record into the database.
13
     *
14
     * @param array<mixed> $values
15
     * @throws BindException
16
     */
17
    public function insert(array $values): bool
18
    {
19
        if (!array_is_list($values)) {
20
            $values = [$values];
21
        }
22
23
        // Convert id to _key
24
        foreach ($values as $key => $value) {
25
            $values[$key] = $this->convertIdToKey($value);
0 ignored issues
show
Bug introduced by
It seems like convertIdToKey() 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

25
            /** @scrutinizer ignore-call */ 
26
            $values[$key] = $this->convertIdToKey($value);
Loading history...
26
        }
27
28
        $bindVar = $this->bindValue($values, 'insert');
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

28
        /** @scrutinizer ignore-call */ 
29
        $bindVar = $this->bindValue($values, 'insert');
Loading history...
29
30
        $aql = $this->grammar->compileInsert($this, $values, $bindVar);
31
        return $this->getConnection()->insert($aql, $this->getBindings());
0 ignored issues
show
Bug introduced by
It seems like getConnection() 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

31
        return $this->/** @scrutinizer ignore-call */ getConnection()->insert($aql, $this->getBindings());
Loading history...
Bug introduced by
It seems like getBindings() 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

31
        return $this->getConnection()->insert($aql, $this->/** @scrutinizer ignore-call */ getBindings());
Loading history...
32
    }
33
34
    /**
35
     * Insert a new record and get the value of the primary key.
36
     *
37
     * @param array<mixed> $values
38
     */
39
    public function insertGetId(array $values, $sequence = null)
40
    {
41
        if (!array_is_list($values)) {
42
            $values = [$values];
43
        }
44
45
        // Convert id to _key
46
        foreach ($values as $key => $value) {
47
            $values[$key] = $this->convertIdToKey($value);
48
        }
49
50
        $bindVar = $this->bindValue($values, 'insert');
51
52
        $aql = $this->grammar->compileInsertGetId($this, $values, $sequence, $bindVar);
53
        $response = $this->getConnection()->execute($aql, $this->getBindings());
54
55
        return (is_array($response)) ? end($response) : $response;
56
    }
57
58
    /**
59
     * Insert a new record into the database.
60
     *
61
     * @param array<mixed> $values
62
     *
63
     * @throws BindException
64
     */
65
    public function insertOrIgnore(array $values): bool
66
    {
67
        if (!array_is_list($values)) {
68
            $values = [$values];
69
        }
70
71
        // Convert id to _key
72
        foreach ($values as $key => $value) {
73
            $values[$key] = $this->convertIdToKey($value);
74
        }
75
76
        $bindVar = $this->bindValue($values, 'insert');
77
78
79
        $aql = $this->grammar->compileInsertOrIgnore($this, $values, $bindVar);
80
        $results = $this->getConnection()->insert($aql, $this->getBindings());
81
82
        return $results;
83
    }
84
}
85