Passed
Push — master ( 0ba402...934d75 )
by Bas
05:33
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 insert(array $values)
26
    {
27
        // Since every insert gets treated like a batch insert, we will make sure the
28
        // bindings are structured in a way that is convenient when building these
29
        // inserts statements by verifying these elements are actually an array.
30
        if (empty($values)) {
31
            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...
32
        }
33
34
        if (! is_array(reset($values))) {
35
            $values = [$values];
36
        }
37
38
        // Here, we will sort the insert keys for every record so that each insert is
39
        // in the same order for the record. We need to make sure this is the case
40
        // so there are not any errors or problems when inserting these records.
41
        else {
42
            foreach ($values as $key => $value) {
43
                ksort($value);
44
45
                $values[$key] = $value;
46
            }
47
        }
48
49
        //Set timestamps
50
        foreach ($values as $key => $value) {
51
            $values[$key] = $this->updateTimestamps($value);
52
        }
53
54
        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...
55
    }
56
57
    /**
58
     * Add the "updated at" column to an array of values.
59
     *
60
     * @param  array  $values
61
     * @return array
62
     */
63
    protected function updateTimestamps(array $values)
64
    {
65
        if (! $this->model->usesTimestamps() ||
66
            is_null($this->model->getUpdatedAtColumn()) ||
67
            is_null($this->model->getCreatedAtColumn())) {
68
            return $values;
69
        }
70
        $timestamp = $this->model->freshTimestampString();
71
        $updatedAtColumn = $this->model->getUpdatedAtColumn();
72
        $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...
73
74
        $createdAtColumn = $this->model->getCreatedAtColumn();
75
        if (! isset($values[$createdAtColumn]) && ! isset($this->model->$createdAtColumn)) {
76
            $timestamps[$createdAtColumn] = $timestamp;
77
        }
78
79
        $values = array_merge(
80
            $timestamps,
81
            $values
82
        );
83
84
        return $values;
85
    }
86
}
87