Passed
Push — next ( 0a206d...d05cf7 )
by Bas
03:59
created

IsAranguentModel::newEloquentBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
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
9
trait IsAranguentModel
10
{
11
    use HasAranguentRelationships;
12
13
    /**
14
     * @override
15
     * Create a new Eloquent query builder for the model.
16
     *
17
     * @param QueryBuilder $query
18
     *
19
     * @return Builder
20
     */
21 79
    public function newEloquentBuilder($query)
22
    {
23 79
        return new Builder($query);
24
    }
25
26
    /**
27
     * Get a new query builder instance for the connection.
28
     *
29
     * @return \Illuminate\Database\Query\Builder
30
     */
31 79
    protected function newBaseQueryBuilder()
32
    {
33 79
        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

33
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
34
    }
35
36
    /**
37
     * Dynamically retrieve attributes on the model.
38
     *
39
     * @param  string  $key
40
     * @return mixed
41
     */
42 46
    public function __get($key)
43
    {
44
        // Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here.
45 46
        if ($key === '_id') {
46 10
            return $this->attributes['_id'];
47
        }
48 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

48
        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...
49
    }
50
51
    /**
52
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
53
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
54
     *
55
     * @return string
56
     */
57 1
    public function getIdAttribute()
58
    {
59 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...
60
    }
61
62
    /**
63
     * Dynamically set attributes on the model.
64
     *
65
     * @param  string  $key
66
     * @param  mixed  $value
67
     * @return void
68
     */
69 18
    public function __set($key, $value)
70
    {
71
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
72 18
        if ($key === '_id') {
73 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...
74 1
            return;
75
        }
76
77 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

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

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

147
        /** @scrutinizer ignore-call */ 
148
        $keyName = $this->getKeyName();
Loading history...
148
149 8
        if ($keyName[0] != '_') {
150
            $keyName = '_' . $keyName;
151
        }
152
153 8
        return Str::snake(class_basename($this)) . $keyName;
154
    }
155
}
156