Passed
Push — fix/updated_at ( 18809c...230688 )
by Bas
15:08 queued 12:05
created

Builder::addUpdatedAtColumn()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 9.9666
cc 3
nc 2
nop 1
crap 3.0175
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder;
6
use Illuminate\Database\Query\Builder as QueryBuilder;
7
use Illuminate\Database\Query\Expression;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use LaravelFreelancerNL\Aranguent\Eloquent\Concerns\QueriesAranguentRelationships;
11
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
12
13
class Builder extends IlluminateBuilder
14
{
15
    use QueriesAranguentRelationships;
0 ignored issues
show
introduced by
The trait LaravelFreelancerNL\Aran...sAranguentRelationships requires some properties which are not provided by LaravelFreelancerNL\Aranguent\Eloquent\Builder: $grammar, $aqb, $from
Loading history...
16
17
    /**
18
     * The methods that should be returned from query builder.
19
     *
20
     * @var array
21
     */
22
    protected $passthru = [
23
        'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd',
24
        'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection',
25
    ];
26
27
    /**
28
     * Insert a record in the database.
29
     *
30
     * @param array $values
31
     *
32
     * @return int
33
     */
34 71
    public function insert(array $values)
35
    {
36
        // Since every insert gets treated like a batch insert, we will make sure the
37
        // bindings are structured in a way that is convenient when building these
38
        // inserts statements by verifying these elements are actually an array.
39 71
        if (empty($values)) {
40
            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...
41
        }
42
43 71
        if (Arr::isAssoc($values)) {
44 52
            $values = [$values];
45
        }
46 71
        if (! Arr::isAssoc($values)) {
47
            // Here, we will sort the insert keys for every record so that each insert is
48
            // in the same order for the record. We need to make sure this is the case
49
            // so there are not any errors or problems when inserting these records.
50 71
            foreach ($values as $key => $value) {
51 71
                ksort($value);
52
53 71
                $values[$key] = $value;
54
            }
55
        }
56
57
        //Set timestamps
58 71
        foreach ($values as $key => $value) {
59 71
            $values[$key] = $this->updateTimestamps($value);
60
        }
61
62 71
        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...
63
    }
64
65
    /**
66
     * Add the "updated at" column to an array of values.
67
     *
68
     * @param array $values
69
     *
70
     * @return array
71
     */
72 71
    protected function updateTimestamps(array $values)
73
    {
74
        if (
75 71
            !$this->model->usesTimestamps() ||
76 71
            is_null($this->model->getUpdatedAtColumn()) ||
77 71
            is_null($this->model->getCreatedAtColumn())
78
        ) {
79
            return $values;
80
        }
81
82 71
        $timestamp = $this->model->freshTimestampString();
83 71
        $updatedAtColumn = $this->model->getUpdatedAtColumn();
84
85 71
        $timestamps = [];
86 71
        $timestamps[$updatedAtColumn] = $timestamp;
87
88 71
        $createdAtColumn = $this->model->getCreatedAtColumn();
89 71
        if (!isset($values[$createdAtColumn]) && !isset($this->model->$createdAtColumn)) {
90 71
            $timestamps[$createdAtColumn] = $timestamp;
91
        }
92
93 71
        $values = array_merge(
94 71
            $timestamps,
95
            $values
96
        );
97 71
        return $values;
98
    }
99
100
101
    /**
102
     * Add the "updated at" column to an array of values.
103
     *
104
     * @param  array  $values
105
     * @return array
106
     */
107 15
    protected function addUpdatedAtColumn(array $values): array
108
    {
109
        if (
110 15
            ! $this->model->usesTimestamps() ||
111 15
            is_null($this->model->getUpdatedAtColumn())
112
        ) {
113
            return $values;
114
        }
115
116 15
        $column = $this->model->getUpdatedAtColumn();
117
118 15
        $values = array_merge(
119 15
            [$column => $this->model->freshTimestampString()],
120
            $values
121
        );
122
123 15
        return $values;
124
    }
125
}
126