Passed
Push — experiment/id-key-conversion ( cdb360...206c4a )
by Bas
11:25 queued 15s
created

IsAranguentModel::__set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 4
nop 2
crap 3
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
     * Insert the given attributes and set the ID on the model.
19
     *
20
     * @param  \Illuminate\Database\Eloquent\Builder  $query
21
     * @param  array  $attributes
22
     * @return void
23
     */
24 9
    protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes)
25
    {
26 9
        $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

26
        /** @scrutinizer ignore-call */ 
27
        $keyName = $this->getKeyName();
Loading history...
27 9
        $id = $query->insertGetId($attributes, $keyName);
28
29 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

29
        $this->/** @scrutinizer ignore-call */ 
30
               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...
30 9
        if ($keyName === '_id') {
31
            $matches = [];
32
            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

32
            preg_match('/\/(.*)$/', /** @scrutinizer ignore-type */ $id, $matches);
Loading history...
33
34
            $this->setAttribute('id', $matches[1]);
35
        }
36 9
        if ($keyName === 'id' || $keyName === '_key') {
37 9
            $this->updateIdWithKey($id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $key of LaravelFreelancerNL\Aran...odel::updateIdWithKey() 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

37
            $this->updateIdWithKey(/** @scrutinizer ignore-type */ $id);
Loading history...
38
        }
39 9
    }
40
41
    /**
42
     * @override
43
     * Create a new Eloquent query builder for the model.
44
     *
45
     * @param QueryBuilder $query
46
     *
47
     * @return Builder
48
     */
49 137
    public function newEloquentBuilder($query)
50
    {
51 137
        return new Builder($query);
52
    }
53
54
    /**
55
     * Get a new query builder instance for the connection.
56
     *
57
     * @return \Illuminate\Database\Query\Builder
58
     */
59 137
    protected function newBaseQueryBuilder()
60
    {
61 137
        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

61
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
62
    }
63
64
    /**
65
     * Dynamically set attributes on the model.
66
     *
67
     * @param  string  $key
68
     * @param  mixed  $value
69
     * @return void
70
     */
71 21
    public function __set($key, $value)
72
    {
73
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
74 21
        if ($key === 'id') {
75 1
            $this->updateIdWithKey($value);
76
        }
77
78 21
        if ($key === '_id') {
79 1
            $this->attributes['id'] = explode('/', $value)[1];
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...
80
        }
81
82 21
        $this->setAttribute($key, $value);
83 21
    }
84
85
    /**
86
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
87
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
88
     *
89
     * @param  string  $value
90
     * @return void
91
     */
92
    public function setKeyAttribute($value)
93
    {
94
        $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...
95
96
        $this->updateIdWithKey($value);
97
    }
98
99
    /**
100
     * @param  string  $key
101
     */
102 10
    protected function updateIdWithKey(string $key)
103
    {
104 10
        $this->attributes['_id'] = $this->getTable() . '/' . $key;
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...
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

104
        $this->attributes['_id'] = $this->/** @scrutinizer ignore-call */ getTable() . '/' . $key;
Loading history...
105 10
    }
106
107
    /**
108
     * Qualify the given column name by the model's table.
109
     *
110
     * @param string $column
111
     *
112
     * @return string
113
     */
114 65
    public function qualifyColumn($column)
115
    {
116 65
        $tableReferer = Str::singular($this->getTable()) . 'Doc';
117
118 65
        if (Str::startsWith($column, $tableReferer . '.')) {
119
            return $column;
120
        }
121
122 65
        return $tableReferer . '.' . $column;
123
    }
124
125
    /**
126
     * Get the default foreign key name for the model.
127
     *
128
     * @return string
129
     */
130 8
    public function getForeignKey()
131
    {
132 8
        $keyName = $this->getKeyName();
133
134 8
        if ($keyName[0] != '_') {
135 8
            $keyName = '_' . $keyName;
136
        }
137
138 8
        return Str::snake(class_basename($this)) . $keyName;
139
    }
140
141 2
    protected function fromAqb(ArangoQueryBuilder|Closure $aqb): Collection
142
    {
143 2
        if ($aqb instanceof Closure) {
144
            /** @phpstan-ignore-next-line */
145 1
            $aqb = $aqb(DB::aqb());
146
        }
147 2
        $connection = $this->getConnection();
148 2
        $results = $connection->execute($aqb->get());
149 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

149
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
150
    }
151
}
152