Passed
Push — refactor/improve-static-analys... ( 46d631...2cb557 )
by Bas
08:38
created

IsAranguentModel::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
6
7
use Closure;
8
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder;
9
use Illuminate\Database\Eloquent\Collection;
10
use Illuminate\Support\Str;
11
use LaravelFreelancerNL\Aranguent\Connection;
12
use LaravelFreelancerNL\Aranguent\Eloquent\Builder;
13
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder;
14
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
15
16
trait IsAranguentModel
17
{
18
    use HasAranguentRelationships;
19
20
    /**
21
     * Insert the given attributes and set the ID on the model.
22
     *
23
     * @param  array<mixed>  $attributes
24
     * @return void
25 9
     */
26
    protected function insertAndSetId(IlluminateEloquentBuilder $query, $attributes)
27 9
    {
28 9
        assert($query instanceof Builder);
29
30 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

30
        /** @scrutinizer ignore-call */ 
31
        $keyName = $this->getKeyName();
Loading history...
31 9
32
        $id = (string) $query->insertGetId($attributes, $keyName);
0 ignored issues
show
Bug introduced by
The method insertGetId() does not exist on LaravelFreelancerNL\Aranguent\Eloquent\Builder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
        $id = (string) $query->/** @scrutinizer ignore-call */ insertGetId($attributes, $keyName);
Loading history...
33
34
        $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

34
        $this->/** @scrutinizer ignore-call */ 
35
               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...
35
        if ($keyName === '_id') {
36
            $matches = [];
37 9
            preg_match('/\/(.*)$/', $id, $matches);
38 9
39
            $this->setAttribute('id', $matches[1]);
40 9
        }
41
        if ($keyName === 'id' || $keyName === '_key') {
42
            $this->updateIdWithKey($id);
43
        }
44
    }
45
46
    /**
47
     * @override
48
     * Create a new Eloquent query builder for the model.
49
     *
50 137
     * @param  QueryBuilder  $query
51
     * @return Builder
52 137
     */
53
    public function newEloquentBuilder($query)
54
    {
55
        return new Builder($query);
56
    }
57
58
    /**
59
     * Get a new query builder instance for the connection.
60 137
     *
61
     * @return \Illuminate\Database\Query\Builder
62 137
     */
63
    protected function newBaseQueryBuilder()
64
    {
65
        return $this->getConnection()->query();
66
    }
67
68
    /**
69
     * Dynamically set attributes on the model.
70
     *
71
     * @param  string  $key
72 21
     * @param  mixed  $value
73
     * @return void
74
     */
75 21
    public function __set($key, $value)
76 1
    {
77
        // Laravel's mutators don't differentiate between id and _id, so we catch ArangoDB's _id here.
78
        if ($key === 'id') {
79 21
            $this->updateIdWithKey($value);
80 1
        }
81
82
        if ($key === '_id') {
83 21
            $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...
84 21
        }
85
86
        $this->setAttribute($key, $value);
87
    }
88
89
    /**
90
     * Map the id attribute commonly used in Laravel to the primary key for third-party compatibility.
91
     * In ArangoDB '_key' is the equivalent of 'id' in sql databases.
92
     *
93
     * @param  string  $value
94
     * @return void
95
     */
96
    public function setKeyAttribute($value)
97
    {
98
        $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...
99
100
        $this->updateIdWithKey($value);
101
    }
102
103 10
    protected function updateIdWithKey(string $key): void
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
     * @return string
113
     */
114
    public function qualifyColumn($column)
115 65
    {
116
        $tableReferer = Str::singular($this->getTable()) . 'Doc';
117 65
118
        if (Str::startsWith($column, $tableReferer . '.')) {
119 65
            return $column;
120
        }
121
122
        return $tableReferer . '.' . $column;
123 65
    }
124
125
    /**
126
     * Get the default foreign key name for the model.
127
     *
128
     * @return string
129
     */
130
    public function getForeignKey()
131 8
    {
132
        $keyName = $this->getKeyName();
133 8
134
        if ($keyName[0] != '_') {
135 8
            $keyName = '_' . $keyName;
136 8
        }
137
138
        return Str::snake(class_basename($this)) . $keyName;
139 8
    }
140
141
    // TODO: see if we can get this working.
142 2
    //    public static function fromAqb(ArangoQueryBuilder|Closure $aqb): Collection
143
    //    {
144 2
    //        if ($aqb instanceof Closure) {
145
    //            /** @phpstan-ignore-next-line */
146 1
    //            $aqb = $aqb(new ArangoQueryBuilder());
147
    //        }
148 2
    //        $connection = static::resolveConnection(self::$connection);
149 2
    //        $results = $connection->execute($aqb->get());
150 2
    //
151
    //        return self::hydrate($results);
152
    //    }
153
154
    /**
155
     * Get the database connection for the model.
156
     *
157
     * @return Connection
158 137
     */
159
    public function getConnection()
160 137
    {
161
        $connection = 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

161
        $connection = 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...
162
163
        assert($connection instanceof Connection);
164
165
        return $connection;
166
    }
167
}
168