Completed
Push — master ( 89deac...cf3c32 )
by Bas
02:49
created

Builder::updateTimestamps()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 3
nop 1
dl 0
loc 20
rs 9.4888
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
    /**
11
     * The methods that should be returned from query builder.
12
     *
13
     * @var array
14
     */
15
    protected $passthru = [
16
        'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd',
17
        'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection',
18
    ];
19
20
21
    /**
22
     * Update a record in the database.
23
     *
24
     * @param  array  $values
25
     * @return int
26
     */
27
    public function update(array $values)
28
    {
29
        return $this->toBase()->update($this->updateTimestamps($values));
30
    }
31
32
    /**
33
     * Update a record in the database.
34
     *
35
     * @param  array  $values
36
     * @return int
37
     */
38
    public function insert(array $values)
39
    {
40
        // Since every insert gets treated like a batch insert, we will make sure the
41
        // bindings are structured in a way that is convenient when building these
42
        // inserts statements by verifying these elements are actually an array.
43
        if (empty($values)) {
44
            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...
45
        }
46
47
        if (! is_array(reset($values))) {
48
            $values = [$values];
49
        }
50
51
        // Here, we will sort the insert keys for every record so that each insert is
52
        // in the same order for the record. We need to make sure this is the case
53
        // so there are not any errors or problems when inserting these records.
54
        else {
55
            foreach ($values as $key => $value) {
56
                ksort($value);
57
58
                $values[$key] = $value;
59
            }
60
        }
61
62
        //Set timestamps
63
        foreach ($values as $key => $value) {
64
            $values[$key] = $this->updateTimestamps($value);
65
        }
66
67
        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...
68
    }
69
70
    /**
71
     * Add the "updated at" column to an array of values.
72
     *
73
     * @param  array  $values
74
     * @return array
75
     */
76
    protected function updateTimestamps(array $values)
77
    {
78
        if (! $this->model->usesTimestamps() ||
79
            is_null($this->model->getUpdatedAtColumn()) ||
80
            is_null($this->model->getCreatedAtColumn())) {
81
            return $values;
82
        }
83
        $timestamp =  $this->model->freshTimestampString();
84
        $updatedAtColumn = $this->model->getUpdatedAtColumn();
85
        $createdAtColumn = $this->model->getCreatedAtColumn();
86
        $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...
87
        if (! isset($values[$createdAtColumn])) {
88
            $timestamps[$createdAtColumn] = $timestamp;
89
        }
90
        $values = array_merge(
91
            $timestamps,
92
            $values
93
        );
94
95
        return $values;
96
    }
97
98
99
}
100