Passed
Push — next ( 64a7ed...91e38a )
by Bas
03:25 queued 12s
created

IsAranguentModel::setKeyAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
     * @override
16
     * Create a new Eloquent query builder for the model.
17
     *
18
     * @param QueryBuilder $query
19
     *
20
     * @return Builder
21
     */
22 84
    public function newEloquentBuilder($query)
23
    {
24 84
        return new Builder($query);
25
    }
26
27
    /**
28
     * Get a new query builder instance for the connection.
29
     *
30
     * @return \Illuminate\Database\Query\Builder
31
     */
32 84
    protected function newBaseQueryBuilder()
33
    {
34 84
        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

34
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
35
    }
36
37
    /**
38
     * Dynamically retrieve attributes on the model.
39
     *
40
     * @param  string  $key
41
     * @return mixed
42
     */
43 46
    public function __get($key)
44
    {
45
        // Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here.
46 46
        if ($key === '_id') {
47 10
            return $this->attributes['_id'];
48
        }
49 44
        return $this->getAttribute($key);
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Did you maybe mean getIdAttribute()? ( Ignorable by Annotation )

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

49
        return $this->/** @scrutinizer ignore-call */ getAttribute($key);

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...
50
    }
51
52
    /**
53
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
54
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
55
     *
56
     * @return string
57
     */
58 1
    public function getIdAttribute()
59
    {
60 1
        return $this->{$this->primaryKey};
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
    }
62
63
    /**
64
     * Dynamically set attributes on the model.
65
     *
66
     * @param  string  $key
67
     * @param  mixed  $value
68
     * @return void
69
     */
70 18
    public function __set($key, $value)
71
    {
72
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
73 18
        if ($key === '_id') {
74 1
            $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...
75 1
            return;
76
        }
77
78 17
        $this->setAttribute($key, $value);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Did you maybe mean setIdAttribute()? ( Ignorable by Annotation )

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

78
        $this->/** @scrutinizer ignore-call */ 
79
               setAttribute($key, $value);

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...
79 17
    }
80
81
    /**
82
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
83
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
84
     *
85
     * @param  string  $value
86
     * @return void
87
     */
88 1
    public function setIdAttribute($value)
89
    {
90 1
        $this->attributes[$this->primaryKey] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
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...
91 1
        if ($this->primaryKey === '_key') {
92 1
            $this->updateIdWithKey($value);
93
        }
94 1
    }
95
96
    /**
97
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
98
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
99
     *
100
     * @param  string  $value
101
     * @return void
102
     */
103 10
    public function setKeyAttribute($value)
104
    {
105 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...
106
107 10
        $this->updateIdWithKey($value);
108 10
    }
109
110
    /**
111
     * @param  string  $key
112
     */
113 11
    protected function updateIdWithKey(string $key)
114
    {
115 11
        if (! isset($this->attributes['_id'])) {
116 9
            return;
117
        }
118
119 2
        $id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']);
120 2
        $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...
121 2
    }
122
123
    /**
124
     * Qualify the given column name by the model's table.
125
     *
126
     * @param string $column
127
     *
128
     * @return string
129
     */
130 39
    public function qualifyColumn($column)
131
    {
132 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

132
        $tableReferer = Str::singular($this->/** @scrutinizer ignore-call */ getTable()) . 'Doc';
Loading history...
133
134 39
        if (Str::startsWith($column, $tableReferer . '.')) {
135
            return $column;
136
        }
137
138 39
        return $tableReferer . '.' . $column;
139
    }
140
141
    /**
142
     * Get the default foreign key name for the model.
143
     *
144
     * @return string
145
     */
146 8
    public function getForeignKey()
147
    {
148 8
        $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

148
        /** @scrutinizer ignore-call */ 
149
        $keyName = $this->getKeyName();
Loading history...
149
150 8
        if ($keyName[0] != '_') {
151
            $keyName = '_' . $keyName;
152
        }
153
154 8
        return Str::snake(class_basename($this)) . $keyName;
155
    }
156
157 1
    protected function execute(ArangoQueryBuilder $aqb)
158
    {
159 1
        $connection = $this->getConnection();
160 1
        $results = $connection->execute($aqb->get());
161 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

161
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
162
    }
163
}
164