Passed
Push — next ( ed4ce4...be2ec6 )
by Bas
12:50
created

IsAranguentModel::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
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\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 9
     */
25
    protected function insertAndSetId(\Illuminate\Database\Eloquent\Builder $query, $attributes)
26 9
    {
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
        $id = $query->insertGetId($attributes, $keyName);
29 9
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
        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 9
        }
37 9
        if ($keyName === 'id' || $keyName === '_key') {
38
            $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 9
        }
40
    }
41
42
    /**
43
     * @override
44
     * Create a new Eloquent query builder for the model.
45
     *
46
     * @param QueryBuilder $query
47
     *
48
     * @return Builder
49 137
     */
50
    public function newEloquentBuilder($query)
51 137
    {
52
        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 137
     */
60
    protected function newBaseQueryBuilder()
61 137
    {
62
        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 21
     */
72
    public function __set($key, $value)
73
    {
74 21
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
75 1
        if ($key === 'id') {
76
            $this->updateIdWithKey($value);
77
        }
78 21
79 1
        if ($key === '_id') {
80
            $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 21
83 21
        $this->setAttribute($key, $value);
84
    }
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 10
     */
103
    protected function updateIdWithKey(string $key)
104 10
    {
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
    }
107
108
    /**
109
     * Qualify the given column name by the model's table.
110
     *
111
     * @param string $column
112
     *
113
     * @return string
114 65
     */
115
    public function qualifyColumn($column)
116 65
    {
117
        $tableReferer = Str::singular($this->getTable()) . 'Doc';
118 65
119
        if (Str::startsWith($column, $tableReferer . '.')) {
120
            return $column;
121
        }
122 65
123
        return $tableReferer . '.' . $column;
124
    }
125
126
    /**
127
     * Get the default foreign key name for the model.
128
     *
129
     * @return string
130 8
     */
131
    public function getForeignKey()
132 8
    {
133
        $keyName = $this->getKeyName();
134 8
135 8
        if ($keyName[0] != '_') {
136
            $keyName = '_' . $keyName;
137
        }
138 8
139
        return Str::snake(class_basename($this)) . $keyName;
140
    }
141 2
142
    protected function fromAqb(ArangoQueryBuilder|Closure $aqb): Collection
143 2
    {
144
        if ($aqb instanceof Closure) {
145 1
            /** @phpstan-ignore-next-line */
146
            $aqb = $aqb(DB::aqb());
147 2
        }
148 2
        $connection = $this->getConnection();
149 2
        $results = $connection->execute($aqb->get());
150
        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
    public function getConnection()
159
    {
160
        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