Passed
Push — next ( b7ca98...230762 )
by Bas
12:22
created

IsAranguentModel::fromAqb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Str;
9
use LaravelFreelancerNL\Aranguent\Eloquent\Builder;
10
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder;
11
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
12
13
trait IsAranguentModel
14
{
15
    use HasAranguentRelationships;
16
17
    /**
18
     * Get the route key for the model.
19
     *
20
     * @return string
21
     */
22
    public function getRouteKeyName(): string
23
    {
24
        return '_key';
25
    }
26
27
    /**
28
     * Insert the given attributes and set the ID on the model.
29
     *
30
     * @param  \Illuminate\Database\Eloquent\Builder  $query
31
     * @param  array  $attributes
32
     * @return void
33
     */
34 7
    protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes)
35
    {
36 7
        $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

36
        $id = $query->insertGetId($attributes, $keyName = $this->/** @scrutinizer ignore-call */ getKeyName());
Loading history...
37
38 7
        $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

38
        $this->/** @scrutinizer ignore-call */ 
39
               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...
39 7
        if ($keyName === '_id') {
40 7
            $matches = [];
41 7
            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

41
            preg_match('/\/(.*)$/', /** @scrutinizer ignore-type */ $id, $matches);
Loading history...
42
43 7
            $this->setAttribute('_key', $matches[1]);
44
        }
45 7
    }
46
47
    /**
48
     * @override
49
     * Create a new Eloquent query builder for the model.
50
     *
51
     * @param QueryBuilder $query
52
     *
53
     * @return Builder
54
     */
55 103
    public function newEloquentBuilder($query)
56
    {
57 103
        return new Builder($query);
58
    }
59
60
    /**
61
     * Get a new query builder instance for the connection.
62
     *
63
     * @return \Illuminate\Database\Query\Builder
64
     */
65 103
    protected function newBaseQueryBuilder()
66
    {
67 103
        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

67
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
68
    }
69
70
    /**
71
     * Dynamically retrieve attributes on the model.
72
     *
73
     * @param  string  $key
74
     * @return mixed
75
     */
76 46
    public function __get($key)
77
    {
78
        // Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here.
79 46
        if ($key === 'id') {
80 2
            return $this->attributes['_id'];
81
        }
82 45
        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

82
        return $this->/** @scrutinizer ignore-call */ getAttribute($key);
Loading history...
83
    }
84
85
    /**
86
     * Dynamically set attributes on the model.
87
     *
88
     * @param  string  $key
89
     * @param  mixed  $value
90
     * @return void
91
     */
92 17
    public function __set($key, $value)
93
    {
94
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
95 17
        if ($key === 'id') {
96 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...
97 1
            return;
98
        }
99
100 16
        $this->setAttribute($key, $value);
101 16
    }
102
103
    /**
104
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
105
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
106
     *
107
     * @param  string  $value
108
     * @return void
109
     */
110 8
    public function setKeyAttribute($value)
111
    {
112 8
        $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...
113
114 8
        $this->updateIdWithKey($value);
115 8
    }
116
117
    /**
118
     * @param  string  $key
119
     */
120 8
    protected function updateIdWithKey(string $key)
121
    {
122 8
        if (! isset($this->attributes['_id'])) {
123 7
            return;
124
        }
125
126 8
        $id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']);
127 8
        $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...
128 8
    }
129
130
    /**
131
     * Qualify the given column name by the model's table.
132
     *
133
     * @param string $column
134
     *
135
     * @return string
136
     */
137 59
    public function qualifyColumn($column)
138
    {
139 59
        $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

139
        $tableReferer = Str::singular($this->/** @scrutinizer ignore-call */ getTable()) . 'Doc';
Loading history...
140
141 59
        if (Str::startsWith($column, $tableReferer . '.')) {
142
            return $column;
143
        }
144
145 59
        return $tableReferer . '.' . $column;
146
    }
147
148
    /**
149
     * Get the default foreign key name for the model.
150
     *
151
     * @return string
152
     */
153 8
    public function getForeignKey()
154
    {
155 8
        $keyName = $this->getKeyName();
156
157 8
        if ($keyName[0] != '_') {
158
            $keyName = '_' . $keyName;
159
        }
160
161 8
        return Str::snake(class_basename($this)) . $keyName;
162
    }
163
164 2
    protected function fromAqb(ArangoQueryBuilder|Closure $aqb): Collection
165
    {
166 2
        if ($aqb instanceof Closure) {
167
            /** @phpstan-ignore-next-line */
168 1
            $aqb = $aqb(DB::aqb());
169
        }
170 2
        $connection = $this->getConnection();
171 2
        $results = $connection->execute($aqb->get());
172 2
        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

172
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
173
    }
174
}
175