Passed
Push — dependabot/github_actions/depe... ( d1016c )
by
unknown
11:16
created

Builder::addUpdatedAtColumn()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

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