Passed
Push — master ( 6688c6...18b4d9 )
by Jonas
05:31
created

Builder::addUpdatedAtToColumns()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace Staudenmeir\LaravelUpsert\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as Base;
6
7
class Builder extends Base
8
{
9
    /**
10
     * Insert new records or update the existing ones.
11
     *
12
     * @param array $values
13
     * @param array|string $target
14
     * @param array|null $update
15
     * @return int
16
     */
17 28
    public function upsert(array $values, $target, array $update = null)
18
    {
19 28
        if (empty($values)) {
20 4
            return 0;
21
        }
22
23 24
        if (!is_array(reset($values))) {
24 12
            $values = [$values];
25
        }
26
27 24
        if (is_null($update)) {
28 12
            $update = array_keys(reset($values));
29
        }
30
31 24
        $values = $this->addTimestampsToValues($values);
32
33 24
        $update = $this->addUpdatedAtToColumns($update);
34
35 24
        return $this->query->upsert($values, $target, $update);
36
    }
37
38
    /**
39
     * Insert a new record into the database and ignore duplicate-key errors.
40
     *
41
     * @param array $values
42
     * @param array|string|null $target
43
     * @return int
44
     */
45 16
    public function insertIgnore(array $values, $target = null)
46
    {
47 16
        if (empty($values)) {
48 4
            return 0;
49
        }
50
51 12
        if (!is_array(reset($values))) {
52 8
            $values = [$values];
53
        }
54
55 12
        $values = $this->addTimestampsToValues($values);
56
57 12
        return $this->query->insertIgnore($values, $target);
58
    }
59
60
    /**
61
     * Add timestamps to the inserted values.
62
     *
63
     * @param array $values
64
     * @return array
65
     */
66 36
    protected function addTimestampsToValues(array $values)
67
    {
68 36
        if (!$this->model->usesTimestamps()) {
69 8
            return $values;
70
        }
71
72 28
        $timestamp = $this->model->freshTimestampString();
73
74 28
        $columns = array_filter([$this->model->getCreatedAtColumn(), $this->model->getUpdatedAtColumn()]);
75
76 28
        foreach ($columns as $column) {
77 28
            foreach ($values as &$row) {
78 28
                $row = array_merge([$column => $timestamp], $row);
79
            }
80
        }
81
82 28
        return $values;
83
    }
84
85
    /**
86
     * Add the "updated at" column to the updated columns.
87
     *
88
     * @param array $update
89
     * @return array
90
     */
91 24
    protected function addUpdatedAtToColumns(array $update)
92
    {
93 24
        if (!$this->model->usesTimestamps()) {
94 4
            return $update;
95
        }
96
97 20
        $column = $this->model->getUpdatedAtColumn();
98
99 20
        if (!is_null($column) && !array_key_exists($column, $update) && !in_array($column, $update)) {
0 ignored issues
show
introduced by
The condition is_null($column) is always false.
Loading history...
100 8
            $update[] = $column;
101
        }
102
103 20
        return $update;
104
    }
105
}
106