Test Setup Failed
Push — master ( 1fae15...0a0a56 )
by Bas
01:35
created

Builder::updateTimestamps()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 3
nop 1
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;
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);
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
Coding Style Comprehensibility introduced by
$timestamps was never initialized. Although not strictly required by PHP, it is generally a good practice to add $timestamps = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

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