Passed
Branch master (407c50)
by Jonas
02:53
created

IsRecursiveRelation::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Query\Expression;
9
10
trait IsRecursiveRelation
11
{
12
    /**
13
     * Whether to include the parent model.
14
     *
15
     * @var bool
16
     */
17
    protected bool $andSelf;
18
19
    /**
20
     * Create a new recursive pivot relationship instance.
21
     *
22
     * @param \Illuminate\Database\Eloquent\Builder $query
23
     * @param \Illuminate\Database\Eloquent\Model $parent
24
     * @param string $table
25
     * @param string $foreignPivotKey
26
     * @param string $relatedPivotKey
27
     * @param string $parentKey
28
     * @param string $relatedKey
29
     * @param bool $andSelf
30
     * @return void
31
     */
32 198
    public function __construct(
33
        Builder $query,
34
        Model $parent,
35
        string $table,
36
        string $foreignPivotKey,
37
        string $relatedPivotKey,
38
        string $parentKey,
39
        string $relatedKey,
40
        bool $andSelf
41
    ) {
42 198
        $this->andSelf = $andSelf;
43
44 198
        parent::__construct($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey);
45
    }
46
47
    /**
48
     * Build model dictionary.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Collection $results
51
     * @return array
52
     */
53 32
    protected function buildDictionary(Collection $results)
54
    {
55 32
        return $results->mapToDictionary(function (Model $result) {
56 32
            return [$result->getFirstPathSegment() => $result];
57 32
        })->all();
58
    }
59
60
//    /**
61
//     * Get the fully qualified local key name.
62
//     *
63
//     * @return string
64
//     */
65
//    public function getQualifiedLocalKeyName()
66
//    {
67
//        return $this->qualifyColumn($this->localKey);
68
//    }
69
//
70
//    /**
71
//     * Update records in the database.
72
//     *
73
//     * @param array $values
74
//     * @return int
75
//     */
76
//    public function update(array $values)
77
//    {
78
//        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
79
//    }
80
//
81
//    /**
82
//     * Decrement a column's value by a given amount.
83
//     *
84
//     * @param string $column
85
//     * @param float|int $amount
86
//     * @param array $extra
87
//     * @return int
88
//     */
89
//    public function increment($column, $amount = 1, array $extra = [])
90
//    {
91
//        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
92
//    }
93
//
94
//    /**
95
//     * Decrement a column's value by a given amount.
96
//     *
97
//     * @param string|\Illuminate\Database\Query\Expression $column
98
//     * @param float|int $amount
99
//     * @param array $extra
100
//     * @return int
101
//     */
102
//    public function decrement($column, $amount = 1, array $extra = [])
103
//    {
104
//        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
105
//    }
106
//
107
//    /**
108
//     * Delete records from the database.
109
//     *
110
//     * @return mixed
111
//     */
112
//    public function delete()
113
//    {
114
//        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
115
//    }
116
//
117
//    /**
118
//     * Force a hard delete on a soft deleted model.
119
//     *
120
//     * @return bool|null
121
//     */
122
//    public function forceDelete()
123
//    {
124
//        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
125
//    }
126
//
127
//    /**
128
//     * Execute an update or delete query after adding the necessary "where in" constraint.
129
//     *
130
//     * @param string $method
131
//     * @param array $parameters
132
//     * @return mixed
133
//     */
134
//    public function executeUpdateOrDeleteQuery($method, array $parameters = [])
135
//    {
136
//        $expression = $this->query->getQuery()->from;
137
//
138
//        $table = $this->parent->getTable();
139
//
140
//        $this->query->getQuery()->from = $table;
141
//
142
//        $this->query->getModel()->setTable($table);
143
//
144
//        $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey);
145
//
146
//        return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters);
147
//    }
148
//
149
150
    /**
151
     * Handle dynamic method calls to the relationship.
152
     *
153
     * @param string $method
154
     * @param array $parameters
155
     * @return mixed
156
     */
157 78
    public function __call($method, $parameters)
158
    {
159 78
        $methods = ['update', 'increment', 'decrement', 'delete', 'forceDelete'];
160
161 78
        if (in_array($method, $methods)) {
162 6
            $expression = $this->query->getQuery()->from;
163
164 6
            $table = $this->parent->getTable();
165
166 6
            $this->query->getQuery()->from = $table;
167
168 6
            $this->query->getModel()->setTable($table);
169
170 6
            $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->parentKey);
171
172 6
            return $this->query->whereIn($this->parentKey, $keys)->$method(...$parameters);
173
        }
174
175 72
        return parent::__call($method, $parameters);
176
    }
177
178
    /**
179
     * Replace table hash with expression name in self-relation aggregate queries.
180
     *
181
     * @param \Illuminate\Database\Eloquent\Builder $query
182
     * @param \Illuminate\Database\Query\Expression $expression
183
     * @return \Illuminate\Database\Query\Expression
184
     */
185 18
    protected function replaceTableHash(Builder $query, Expression $expression): Expression
186
    {
187 18
        return new Expression(
188 18
            str_replace(
189 18
                $query->getGrammar()->wrap(
0 ignored issues
show
Bug introduced by
It seems like $query->getGrammar()->wr...lationCountHash(false)) can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Grammars\Grammar; however, parameter $search of str_replace() does only seem to accept string|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

189
                /** @scrutinizer ignore-type */ $query->getGrammar()->wrap(
Loading history...
190 18
                    $this->getRelationCountHash(false)
0 ignored issues
show
Bug introduced by
The method getRelationCountHash() does not exist on Staudenmeir\LaravelAdjac...its\IsRecursiveRelation. 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

190
                    $this->/** @scrutinizer ignore-call */ 
191
                           getRelationCountHash(false)
Loading history...
191
                ),
192 18
                $query->getGrammar()->wrap(
0 ignored issues
show
Bug introduced by
It seems like $query->getGrammar()->wr...)->getExpressionName()) can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Grammars\Grammar; however, parameter $replace of str_replace() does only seem to accept string|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

192
                /** @scrutinizer ignore-type */ $query->getGrammar()->wrap(
Loading history...
193 18
                    $query->getModel()->getExpressionName()
194
                ),
195 18
                $expression->getValue(),
196
            )
197
        );
198
    }
199
200
    /**
201
     * Get the select columns for the relation query.
202
     *
203
     * @param array $columns
204
     * @return array
205
     */
206 168
    protected function shouldSelect(array $columns = ['*'])
207
    {
208 168
        return $columns;
209
    }
210
}
211