Passed
Push — next ( 852a6b...09449f )
by Bas
12:11
created

IsAranguentModel::insertAndSetId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
4
5
use Illuminate\Support\Str;
6
use LaravelFreelancerNL\Aranguent\Eloquent\Builder;
7
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder;
8
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
9
10
trait IsAranguentModel
11
{
12
    use HasAranguentRelationships;
13
14
    /**
15
     * Get the route key for the model.
16
     *
17
     * @return string
18
     */
19
    public function getRouteKeyName(): string
20
    {
21
        return '_key';
22
    }
23
24
    /**
25
     * Insert the given attributes and set the ID on the model.
26
     *
27
     * @param  \Illuminate\Database\Eloquent\Builder  $query
28
     * @param  array  $attributes
29
     * @return void
30
     */
31 9
    protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes)
32
    {
33 9
        $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $id = $query->insertGetId($attributes, $keyName = $this->/** @scrutinizer ignore-call */ getKeyName());
Loading history...
34
35 9
        $this->setAttribute($keyName, $id);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Did you maybe mean setKeyAttribute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $this->/** @scrutinizer ignore-call */ 
36
               setAttribute($keyName, $id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36 9
        if ($keyName === '_id') {
37 9
            $matches = [];
38 9
            preg_match('/\/(.*)$/', $id, $matches);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            preg_match('/\/(.*)$/', /** @scrutinizer ignore-type */ $id, $matches);
Loading history...
39
40 9
            $this->setAttribute('_key', $matches[1]);
41
        }
42 9
    }
43
44
    /**
45
     * @override
46
     * Create a new Eloquent query builder for the model.
47
     *
48
     * @param QueryBuilder $query
49
     *
50
     * @return Builder
51
     */
52 83
    public function newEloquentBuilder($query)
53
    {
54 83
        return new Builder($query);
55
    }
56
57
    /**
58
     * Get a new query builder instance for the connection.
59
     *
60
     * @return \Illuminate\Database\Query\Builder
61
     */
62 83
    protected function newBaseQueryBuilder()
63
    {
64 83
        return $this->getConnection()->query();
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
65
    }
66
67
    /**
68
     * Dynamically retrieve attributes on the model.
69
     *
70
     * @param  string  $key
71
     * @return mixed
72
     */
73 45
    public function __get($key)
74
    {
75
        // Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here.
76 45
        if ($key === 'id') {
77 2
            return $this->attributes['_id'];
78
        }
79 44
        return $this->getAttribute($key);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        return $this->/** @scrutinizer ignore-call */ getAttribute($key);
Loading history...
80
    }
81
82
    /**
83
     * Dynamically set attributes on the model.
84
     *
85
     * @param  string  $key
86
     * @param  mixed  $value
87
     * @return void
88
     */
89 17
    public function __set($key, $value)
90
    {
91
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
92 17
        if ($key === 'id') {
93 1
            $this->attributes['_id'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94 1
            return;
95
        }
96
97 16
        $this->setAttribute($key, $value);
98 16
    }
99
100
    /**
101
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
102
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
103
     *
104
     * @param  string  $value
105
     * @return void
106
     */
107 10
    public function setKeyAttribute($value)
108
    {
109 10
        $this->attributes['_key'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
110
111 10
        $this->updateIdWithKey($value);
112 10
    }
113
114
    /**
115
     * @param  string  $key
116
     */
117 10
    protected function updateIdWithKey(string $key)
118
    {
119 10
        if (! isset($this->attributes['_id'])) {
120 9
            return;
121
        }
122
123 10
        $id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']);
124 10
        $this->attributes['_id'] = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
125 10
    }
126
127
    /**
128
     * Qualify the given column name by the model's table.
129
     *
130
     * @param string $column
131
     *
132
     * @return string
133
     */
134 39
    public function qualifyColumn($column)
135
    {
136 39
        $tableReferer = Str::singular($this->getTable()) . 'Doc';
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        $tableReferer = Str::singular($this->/** @scrutinizer ignore-call */ getTable()) . 'Doc';
Loading history...
137
138 39
        if (Str::startsWith($column, $tableReferer . '.')) {
139
            return $column;
140
        }
141
142 39
        return $tableReferer . '.' . $column;
143
    }
144
145
    /**
146
     * Get the default foreign key name for the model.
147
     *
148
     * @return string
149
     */
150 8
    public function getForeignKey()
151
    {
152 8
        $keyName = $this->getKeyName();
153
//
154
//        if ($keyName[0] != '_') {
155
//            $keyName = '_' . $keyName;
156
//        }
157
158 8
        return Str::snake(class_basename($this)) . $keyName;
159
    }
160
161 1
    protected function execute(ArangoQueryBuilder $aqb)
162
    {
163 1
        $connection = $this->getConnection();
164 1
        $results = $connection->execute($aqb->get());
165 1
        return $this->hydrate($results);
0 ignored issues
show
Bug introduced by
It seems like hydrate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
166
    }
167
}
168