Passed
Push — refactor/improve-static-analys... ( ed4ce4...8da3ef )
by Bas
05:52 queued 02:21
created

IsAranguentModel::getConnection()   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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\Connection;
10
use LaravelFreelancerNL\Aranguent\Eloquent\Builder;
11
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder;
12
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
13
14
trait IsAranguentModel
15
{
16
    use HasAranguentRelationships;
17
18
    /**
19
     * Insert the given attributes and set the ID on the model.
20
     *
21
     * @param  \Illuminate\Database\Eloquent\Builder  $query
22
     * @param  array  $attributes
23
     * @return void
24
     */
25 9
    protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes)
26
    {
27 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

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

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

33
            preg_match('/\/(.*)$/', /** @scrutinizer ignore-type */ $id, $matches);
Loading history...
34
35
            $this->setAttribute('id', $matches[1]);
36
        }
37 9
        if ($keyName === 'id' || $keyName === '_key') {
38 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

38
            $this->updateIdWithKey(/** @scrutinizer ignore-type */ $id);
Loading history...
39
        }
40 9
    }
41
42
    /**
43
     * @override
44
     * Create a new Eloquent query builder for the model.
45
     *
46
     * @param QueryBuilder $query
47
     *
48
     * @return Builder
49
     */
50 137
    public function newEloquentBuilder($query)
51
    {
52 137
        return new Builder($query);
53
    }
54
55
    /**
56
     * Get a new query builder instance for the connection.
57
     *
58
     * @return \Illuminate\Database\Query\Builder
59
     */
60 137
    protected function newBaseQueryBuilder()
61
    {
62 137
        return $this->getConnection()->query();
63
    }
64
65
    /**
66
     * Dynamically set attributes on the model.
67
     *
68
     * @param  string  $key
69
     * @param  mixed  $value
70
     * @return void
71
     */
72 21
    public function __set($key, $value)
73
    {
74
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
75 21
        if ($key === 'id') {
76 1
            $this->updateIdWithKey($value);
77
        }
78
79 21
        if ($key === '_id') {
80 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...
81
        }
82
83 21
        $this->setAttribute($key, $value);
84 21
    }
85
86
    /**
87
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
88
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
89
     *
90
     * @param  string  $value
91
     * @return void
92
     */
93
    public function setKeyAttribute($value)
94
    {
95
        $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...
96
97
        $this->updateIdWithKey($value);
98
    }
99
100
    /**
101
     * @param  string  $key
102
     */
103 10
    protected function updateIdWithKey(string $key)
104
    {
105 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

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

150
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
151
    }
152
153
    /**
154
     * Get the database connection for the model.
155
     *
156
     * @return Connection
157
     */
158 137
    public function getConnection()
159
    {
160 137
        return static::resolveConnection($this->getConnectionName());
0 ignored issues
show
Bug introduced by
The method getConnectionName() does not exist on LaravelFreelancerNL\Aran...ncerns\IsAranguentModel. Did you maybe mean getConnection()? ( Ignorable by Annotation )

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

160
        return static::resolveConnection($this->/** @scrutinizer ignore-call */ getConnectionName());

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...
161
    }
162
}
163