Passed
Branch master (512d52)
by Bas
08:47
created

Builder::insert()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder;
6
7
class Builder extends IlluminateBuilder
8
{
9
    /**
10
     * The methods that should be returned from query builder.
11
     *
12
     * @var array
13
     */
14
    protected $passthru = [
15
        'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd',
16
        'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection',
17
    ];
18
19
    /**
20
     * Update a record in the database.
21
     *
22
     * @param  array  $values
23
     * @return int
24
     */
25
    public function update(array $values)
26
    {
27
        return $this->toBase()->update($this->updateTimestamps($values));
28
    }
29
30
    /**
31
     * Update a record in the database.
32
     *
33
     * @param  array  $values
34
     * @return int
35
     */
36
    public function insert(array $values)
37
    {
38
        // Since every insert gets treated like a batch insert, we will make sure the
39
        // bindings are structured in a way that is convenient when building these
40
        // inserts statements by verifying these elements are actually an array.
41
        if (empty($values)) {
42
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type integer.
Loading history...
43
        }
44
45
        if (! is_array(reset($values))) {
46
            $values = [$values];
47
        }
48
49
        // Here, we will sort the insert keys for every record so that each insert is
50
        // in the same order for the record. We need to make sure this is the case
51
        // so there are not any errors or problems when inserting these records.
52
        else {
53
            foreach ($values as $key => $value) {
54
                ksort($value);
55
56
                $values[$key] = $value;
57
            }
58
        }
59
60
        //Set timestamps
61
        foreach ($values as $key => $value) {
62
            $values[$key] = $this->updateTimestamps($value);
63
        }
64
65
        return $this->toBase()->insert($values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->toBase()->insert($values) returns the type boolean which is incompatible with the documented return type integer.
Loading history...
66
    }
67
68
    /**
69
     * Add the "updated at" column to an array of values.
70
     *
71
     * @param  array  $values
72
     * @return array
73
     */
74
    protected function updateTimestamps(array $values)
75
    {
76
        if (! $this->model->usesTimestamps() ||
77
            is_null($this->model->getUpdatedAtColumn()) ||
78
            is_null($this->model->getCreatedAtColumn())) {
79
            return $values;
80
        }
81
        $timestamp = $this->model->freshTimestampString();
82
        $updatedAtColumn = $this->model->getUpdatedAtColumn();
83
        $createdAtColumn = $this->model->getCreatedAtColumn();
84
        $timestamps[$updatedAtColumn] = $timestamp;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$timestamps was never initialized. Although not strictly required by PHP, it is generally a good practice to add $timestamps = array(); before regardless.
Loading history...
85
        if (! isset($values[$createdAtColumn]) && $this->model->$createdAtColumn == '') {
86
            $timestamps[$createdAtColumn] = $timestamp;
87
        }
88
        $values = array_merge(
89
            $timestamps,
90
            $values
91
        );
92
93
        return $values;
94
    }
95
}
96