BuildsInserts::insertOrIgnore()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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

28
            /** @scrutinizer ignore-call */ 
29
            $values[$key] = $this->convertIdToKey($value);
Loading history...
29
        }
30
31 80
        $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

31
        /** @scrutinizer ignore-call */ 
32
        $bindVar = $this->bindValue($values, 'insert');
Loading history...
32
33 80
        $aql = $this->grammar->compileInsert($this, $values, $bindVar);
34 80
        return $this->getConnection()->insert($aql, $this->getBindings());
0 ignored issues
show
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

34
        return $this->getConnection()->insert($aql, $this->/** @scrutinizer ignore-call */ getBindings());
Loading history...
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

34
        return $this->/** @scrutinizer ignore-call */ getConnection()->insert($aql, $this->getBindings());
Loading history...
35
    }
36
37
    /**
38
     * Insert a new record and get the value of the primary key.
39
     *
40
     * @param  array<mixed>  $values
41
     * @param  string|null  $sequence
42
     * @return int|string
43
     */
44 32
    public function insertGetId(array $values, $sequence = null)
45
    {
46
        assert($this->grammar instanceof Grammar);
47
48 32
        if (!array_is_list($values)) {
49 32
            $values = [$values];
50
        }
51
52
        // Convert id to _key
53 32
        foreach ($values as $key => $value) {
54 32
            $values[$key] = $this->convertIdToKey($value);
55
        }
56
57 32
        $bindVar = $this->bindValue($values, 'insert');
58
59 32
        $aql = $this->grammar->compileInsertGetId($this, $values, $sequence, $bindVar);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...\Concerns\BuildsInserts is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $query of LaravelFreelancerNL\Aran...r::compileInsertGetId(). ( Ignorable by Annotation )

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

59
        $aql = $this->grammar->compileInsertGetId(/** @scrutinizer ignore-type */ $this, $values, $sequence, $bindVar);
Loading history...
60 32
        $response = $this->getConnection()->execute($aql, $this->getBindings());
61
62 32
        return (is_array($response)) ? end($response) : $response;
63
    }
64
65
    /**
66
     * Insert a new record into the database.
67
     *
68
     * @param array<mixed> $values
69
     *
70
     * @throws BindException
71
     */
72
    /** @phpstan-ignore-next-line */
73 64
    public function insertOrIgnore(array $values): bool
74
    {
75
        assert($this->grammar instanceof Grammar);
76
77 64
        if (!array_is_list($values)) {
78 64
            $values = [$values];
79
        }
80
81
        // Convert id to _key
82 64
        foreach ($values as $key => $value) {
83 64
            $values[$key] = $this->convertIdToKey($value);
84
        }
85
86 64
        $bindVar = $this->bindValue($values, 'insert');
87
88
89 64
        $aql = $this->grammar->compileInsertOrIgnore($this, $values, $bindVar);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...\Concerns\BuildsInserts is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $query of LaravelFreelancerNL\Aran...compileInsertOrIgnore(). ( Ignorable by Annotation )

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

89
        $aql = $this->grammar->compileInsertOrIgnore(/** @scrutinizer ignore-type */ $this, $values, $bindVar);
Loading history...
90 64
        return $this->getConnection()->insert($aql, $this->getBindings());
91
    }
92
}
93