Completed
Push — master ( 89b781...ef8ca9 )
by Jonas
04:41
created

Builder::addTimestampsToValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
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 21
    public function upsert(array $values, $target, array $update = null)
18
    {
19 21
        if (empty($values)) {
20 3
            return 0;
21
        }
22
23 18
        if (!is_array(reset($values))) {
24 9
            $values = [$values];
25
        }
26
27 18
        if (is_null($update)) {
28 9
            $update = array_keys(reset($values));
29
        }
30
31 18
        $values = $this->addTimestampsToValues($values);
32
33 18
        $update = $this->addUpdatedAtToColumns($update);
34
35 18
        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 12
    public function insertIgnore(array $values, $target = null)
46
    {
47 12
        if (empty($values)) {
48 3
            return 0;
49
        }
50
51 9
        if (!is_array(reset($values))) {
52 6
            $values = [$values];
53
        }
54
55 9
        $values = $this->addTimestampsToValues($values);
56
57 9
        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 27
    protected function addTimestampsToValues(array $values)
67
    {
68 27
        if (!$this->model->usesTimestamps()) {
69 6
            return $values;
70
        }
71
72 21
        $timestamp = $this->model->freshTimestampString();
73
74 21
        $columns = array_filter([$this->model->getCreatedAtColumn(), $this->model->getUpdatedAtColumn()]);
75
76 21
        foreach ($columns as $column) {
77 21
            foreach ($values as &$row) {
78 21
                $row = array_merge([$column => $timestamp], $row);
79
            }
80
        }
81
82 21
        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 18
    protected function addUpdatedAtToColumns(array $update)
92
    {
93 18
        if (!$this->model->usesTimestamps()) {
94 3
            return $update;
95
        }
96
97 15
        $column = $this->model->getUpdatedAtColumn();
98
99 15
        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 6
            $update[] = $column;
101
        }
102
103 15
        return $update;
104
    }
105
}
106