Passed
Push — master ( 4bc519...a9f33f )
by Jonas
04:02
created

IsRecursiveRelation::getQualifiedLocalKeyName()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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 Staudenmeir\LaravelAdjacencyList\Eloquent\Relations;
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 $andSelf;
18
19
    /**
20
     * Create a new recursive relationship instance.
21
     *
22
     * @param \Illuminate\Database\Eloquent\Builder $query
23
     * @param \Illuminate\Database\Eloquent\Model $parent
24
     * @param string $foreignKey
25
     * @param string $localKey
26
     * @param bool $andSelf
27
     * @return void
28
     */
29 231
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
30
    {
31 231
        $this->andSelf = $andSelf;
32
33 231
        parent::__construct($query, $parent, $foreignKey, $localKey);
34 231
    }
35
36
    /**
37
     * Build model dictionary.
38
     *
39
     * @param \Illuminate\Database\Eloquent\Collection $results
40
     * @return array
41
     */
42 50
    protected function buildDictionary(Collection $results)
43
    {
44 50
        return $results->mapToDictionary(function (Model $result) {
45 50
            return [$result->getFirstPathSegment() => $result];
46 50
        })->all();
47
    }
48
49
    /**
50
     * Get the fully qualified local key name.
51
     *
52
     * @return string
53
     */
54 149
    public function getQualifiedLocalKeyName()
55
    {
56 149
        return $this->qualifyColumn($this->localKey);
0 ignored issues
show
Bug introduced by
It seems like qualifyColumn() 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

56
        return $this->/** @scrutinizer ignore-call */ qualifyColumn($this->localKey);
Loading history...
57
    }
58
59
    /**
60
     * Update records in the database.
61
     *
62
     * @param array $values
63
     * @return int
64
     */
65 8
    public function update(array $values)
66
    {
67 8
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
68
    }
69
70
    /**
71
     * Decrement a column's value by a given amount.
72
     *
73
     * @param string $column
74
     * @param float|int $amount
75
     * @param array $extra
76
     * @return int
77
     */
78 4
    public function increment($column, $amount = 1, array $extra = [])
79
    {
80 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
81
    }
82
83
    /**
84
     * Decrement a column's value by a given amount.
85
     *
86
     * @param string|\Illuminate\Database\Query\Expression $column
87
     * @param float|int $amount
88
     * @param array $extra
89
     * @return int
90
     */
91 4
    public function decrement($column, $amount = 1, array $extra = [])
92
    {
93 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__, func_get_args());
94
    }
95
96
    /**
97
     * Delete records from the database.
98
     *
99
     * @return mixed
100
     */
101 4
    public function delete()
102
    {
103 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
104
    }
105
106
    /**
107
     * Force a hard delete on a soft deleted model.
108
     *
109
     * @return bool|null
110
     */
111 4
    public function forceDelete()
112
    {
113 4
        return $this->executeUpdateOrDeleteQuery(__FUNCTION__);
114
    }
115
116
    /**
117
     * Execute an update or delete query after adding the necessary "where in" constraint.
118
     *
119
     * @param string $method
120
     * @param array $parameters
121
     * @return mixed
122
     */
123 24
    public function executeUpdateOrDeleteQuery($method, array $parameters = [])
124
    {
125 24
        $expression = $this->query->getQuery()->from;
126
127 24
        $table = $this->parent->getTable();
128
129 24
        $this->query->getQuery()->from = $table;
130
131 24
        $this->query->getModel()->setTable($table);
132
133 24
        $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey);
134
135 24
        return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters);
136
    }
137
138
    /**
139
     * Replace table hash with expression name in self-relation aggregate queries.
140
     *
141
     * @param \Illuminate\Database\Eloquent\Builder $query
142
     * @param \Illuminate\Database\Query\Expression $expression
143
     * @return \Illuminate\Database\Query\Expression
144
     */
145 21
    protected function replaceTableHash(Builder $query, Expression $expression)
146
    {
147 21
        return new Expression(
148 21
            str_replace(
149 21
                $query->getGrammar()->wrap(
150 21
                    $this->getRelationCountHash(false)
0 ignored issues
show
Bug introduced by
It seems like getRelationCountHash() 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
                    $this->/** @scrutinizer ignore-call */ 
151
                           getRelationCountHash(false)
Loading history...
151
                ),
152 21
                $query->getGrammar()->wrap(
153 21
                    $query->getModel()->getExpressionName()
154
                ),
155 21
                $expression->getValue(),
156
            )
157
        );
158
    }
159
}
160