Passed
Push — next ( 91e38a...852a6b )
by Bas
12:12
created

IsAranguentModel::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
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
     * Get the route key for the model.
16
     *
17
     * @return string
18
     */
19
    public function getRouteKeyName(): string
20
    {
21
        return '_key';
22
    }
23
24
    /**
25
     * @override
26
     * Create a new Eloquent query builder for the model.
27
     *
28
     * @param QueryBuilder $query
29
     *
30
     * @return Builder
31
     */
32 83
    public function newEloquentBuilder($query)
33
    {
34 83
        return new Builder($query);
35
    }
36
37
    /**
38
     * Get a new query builder instance for the connection.
39
     *
40
     * @return \Illuminate\Database\Query\Builder
41
     */
42 83
    protected function newBaseQueryBuilder()
43
    {
44 83
        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

44
        return $this->/** @scrutinizer ignore-call */ getConnection()->query();
Loading history...
45
    }
46
47
    /**
48
     * Dynamically retrieve attributes on the model.
49
     *
50
     * @param  string  $key
51
     * @return mixed
52
     */
53 45
    public function __get($key)
54
    {
55
        // Laravel's accessors don't differentiate between id and _id, so we catch ArangoDB's _id here.
56 45
        if ($key === 'id') {
57 2
            return $this->attributes['_id'];
58
        }
59 44
        return $this->getAttribute($key);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

59
        return $this->/** @scrutinizer ignore-call */ getAttribute($key);
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 17
    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 17
        if ($key === 'id') {
73 1
            $this->attributes['_id'] = $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 16
        $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 setKeyAttribute()? ( 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 16
    }
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 10
    public function setKeyAttribute($value)
88
    {
89 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...
90
91 10
        $this->updateIdWithKey($value);
92 10
    }
93
94
    /**
95
     * @param  string  $key
96
     */
97 10
    protected function updateIdWithKey(string $key)
98
    {
99 10
        if (! isset($this->attributes['_id'])) {
100 9
            return;
101
        }
102
103 1
        $id = preg_replace("/[a-zA-Z0-9_-]+\/\K.+/i", $key, $this->attributes['_id']);
104 1
        $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...
105 1
    }
106
107
    /**
108
     * Qualify the given column name by the model's table.
109
     *
110
     * @param string $column
111
     *
112
     * @return string
113
     */
114 39
    public function qualifyColumn($column)
115
    {
116 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

116
        $tableReferer = Str::singular($this->/** @scrutinizer ignore-call */ getTable()) . 'Doc';
Loading history...
117
118 39
        if (Str::startsWith($column, $tableReferer . '.')) {
119
            return $column;
120
        }
121
122 39
        return $tableReferer . '.' . $column;
123
    }
124
125
    /**
126
     * Get the default foreign key name for the model.
127
     *
128
     * @return string
129
     */
130 8
    public function getForeignKey()
131
    {
132 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

132
        /** @scrutinizer ignore-call */ 
133
        $keyName = $this->getKeyName();
Loading history...
133
//
134
//        if ($keyName[0] != '_') {
135
//            $keyName = '_' . $keyName;
136
//        }
137
138 8
        return Str::snake(class_basename($this)) . $keyName;
139
    }
140
141 1
    protected function execute(ArangoQueryBuilder $aqb)
142
    {
143 1
        $connection = $this->getConnection();
144 1
        $results = $connection->execute($aqb->get());
145 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

145
        return $this->/** @scrutinizer ignore-call */ hydrate($results);
Loading history...
146
    }
147
}
148